4

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?

Manaal Soni
  • 73
  • 12
  • As you did not show the initialization vector IV) nor the key it's not easy to help, but the IV has to a fixed length of 16 (after Base64 decoding) and the AES key needs to be exact 16 or 24 or 32 bytes long (after Base64 decoding). – Michael Fehr Mar 29 '21 at 15:22
  • Thank you @MichaelFehr before creating the AES.new object I b64decode the key and iv, which made the length of key = 32 and iv=16. After that, I successfully got the decrypted message. – Manaal Soni Mar 30 '21 at 05:54

1 Answers1

3
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.

Manaal Soni
  • 73
  • 12