Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
342 views
in Technique[技术] by (71.8m points)

python - Download and Decrypt file from AMAZON Selling Partner API

I am new to python and want to know how can I download and decrypt file in python from below response code. The payload response from the service will be as below. We need to download the file using the URL in the response and decrypt the file using the initialization vector and key.

    {
    "payload": {
    "reportDocumentId": "DOC-b8b0-4226-b4b9-0ee058ea5760",
    "url": "https://d34o8swod1owfl.cloudfront.net/SampleResult%2BKey%3DSample%2BINITVEC%3D58+fa+bf+a7+08+11+95+0f+c1+a8+c6+e0+d5+6f+ae+c8",
    "encryptionDetails": {
    "standard": "AES",
    "initializationVector": "58 fa bf a7 08 11 95 0f c1 a8 c6 e0 d5 6f ae c8",
    "key": "Sample"
    }
    }
    }

> I tried downloading file manually and decrypted using below code, but got error:  IV must be 16 bytes long

```python

from Crypto.Cipher import AES
import base64
# open file
input_file = open('amazon', 'rb')
output_file = open('amazon_decrypted', 'wb')
key_raw= "Sample"
key = key_raw.encode('utf-8')
base64_bytes = base64.b64encode(key)
buffer_size = 65536 # 64kb

# Read in the iv
#iv = input_file.read(16)
iv ='58 fa bf a7 08 11 95 0f c1 a8 c6 e0 d5 6f ae c8'
# Create the cipher object and encrypt the data
cipher = AES.new(key_raw, AES.MODE_CBC, iv)
# Keep reading the file into the buffer, decrypting then writing to the new file
buffer = input_file.read(buffer_size)
while len(buffer) > 0:
    decrypted_bytes= cipher.decrypt(buffer)
    output_file.write(decrypted_bytes)
    buffer = input_file.read(buffer_size)
#Close the input and output files
input_file.close()
output_file.close()
question from:https://stackoverflow.com/questions/65836931/download-and-decrypt-file-from-amazon-selling-partner-api

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...