Decrypt MCRYPT_RIJNDAEL_256 with 32-byte initialization vectors with PyCrypto

て烟熏妆下的殇ゞ 提交于 2019-12-23 20:58:46

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!