问题
I have data that was encrypted in PHP as follows:
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 32 byte block size? Alternatively, is there a different library that I can use to decrypt the data?
回答1:
Thanks to the comments I implemented a suitable solution. I modified rijndael.py
in the linked duplicate question to accept bytes rather than strings. I then use it as follows to decrypt 32-byte blocks with the 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 rather than PyCrypto is only necessary because libmcrypt incorrectly sets the data block sizes, and thus the initialization vector sizes, to be equal to the key sizes. As far as I understand, data block sizes should always be 128 bits for AES-Rijndael.
来源:https://stackoverflow.com/questions/27334093/decrypt-mcrypt-rijndael-256-with-32-byte-initialization-vectors-with-pycrypto