Decode MCRYPT_RIJNDAEL_256 with 32-byte initialization vectors with PyCrypto

I have data that was encrypted in PHP like this:

mcrypt_encrypt(MCRYPT_RIJNDAEL_256, SECRET, $data, MCRYPT_MODE_CBC, $iv)

      

I need to decrypt this data in a Python 3 application. I am trying to use PyCrypto, but I am open to other libraries. I expect the following to work:

decryptor = AES.new(key, mode, IV=IV)
plain = decryptor.decrypt(ciphertext)

      

My initialization vector is 32 bytes and the following exception is thrown:

ValueError: IV must be 16 bytes long

      

How can I set PyCrypto to use a 32 byte initialization vector and a 32 byte block size? Also, is there another library I can use to decrypt the data?

+3


source to share


1 answer


Thanks to the comments, I have implemented a suitable solution. I modified rijndael.py

in the linked duplicate question to accept bytes and not strings. Then I use it in the following way to decrypt 32 byte blocks with 32 byte initialization vectors.

from rijndael import rijndael

iv = b'myInitializationVectorfoobarfoob'
key = b'myKeyfoobarfoobarfoobarfoobarfoo'
text = b'myCipherTextFoobarfoobarfoobarfo'

r = rijndael(key, block_size=32)
plaintext = r.decrypt(text)
l = ''.join([chr(a ^ b) for a, b in zip(plaintext.encode('latin-1'), iv)])
print(l)

      



Note that using this and not PyCrypto is only necessary because libmcrypt incorrectly sets the sizes of the data blocks and therefore the sizes of the initialization vector equal to the sizes of the keys. As far as I understand, data block sizes should always be 128 bits for AES-Rijndael.

0


source







All Articles