简体   繁体   中英

Decrypting Amazon SP API Report Document using python. AES, CBC, base64

I am trying to decrypt the report document. I have the following details for decryption:

{
"payload": {
    "reportDocumentId": "XXXX",
    "encryptionDetails": {
        "standard": "AES",
        "initializationVector": "XXXX",
        "key": "XXXX"
    },
    "url": "https://XXXXX"
}}

Using these details I tried writing various codes giving different errors 1.

from base64 import b64encode 
import hashlib 
import pyaes 
import os
from sys import getsizeof

content = requests.get(url)
ciphertext = content.text
#ciphertext = b64encode(bytes(content.text))
print(getsizeof(key))
print(getsizeof(iv))
decrypter = pyaes.Decrypter(pyaes.AESModeOfOperationCBC(key, iv)) 
decryptedData = decrypter.feed(ciphertext) 
decryptedData += decrypter.feed()
print(decryptedData)

This shows the following error: ValueError: initialization vector must be 16 bytes My initialization vector and key are in base64. Their size is 73 and 93 respectively

2.

content = requests.get(url)

message = content.text
print(len(message))

obj = AES.new(key, AES.MODE_CBC, iv)
print(obj.decrypt(message))

This gives the following error: ValueError: Incorrect AES key length (44 bytes)

How do I solve this issue? Any approach other than this will also be very helpful

Does AWS KMS help in decrypting such data?

content = requests.get(url)
message = content.content
dec_key = b64decode(key)
dec_iv = b64decode(iv)
obj = AES.new(dec_key, AES.MODE_CBC, dec_iv)
decrypt_text = obj.decrypt(message)

Modified code gives the desired output. Decode the key and iv. This answer is not by me, I have gathered from various questions on stackoverflow. Writing it for anyone who might need it.

I also wasted a long time finding a solution. The solution above is not fully. The right solution is described here:

But also don't forget to decompress your report. Example here:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM