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 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…