Manaal Soni
Manaal Soni

Reputation: 93

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?

Upvotes: 4

Views: 1736

Answers (1)

Manaal Soni
Manaal Soni

Reputation: 93

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.

Upvotes: 3

Related Questions