Pycrypto : AES Decryption

橙三吉。 提交于 2019-12-07 03:49:55

问题


Why Pycrypto AES decryption gives different output when decrypted with AES object used for encryption and right output when decrypted with AES object used solely for decryption?

from Crypto.Cipher import AES
obj = AES.new('0123456789012345', AES.MODE_CBC, '0123456789012345')
message = '0123456789012345'
ciphertext = obj.encrypt(message)
plaintext = obj.decrypt(ciphertext)
# plaintext here is byte array
obj2 = AES.new('0123456789012345', AES.MODE_CBC, '0123456789012345')
plaintext = obj2.decrypt(ciphertext)
# plaintext here is 0123456789012345

回答1:


According to BlockAlgo#encrypt from which the AES class is derived:

Encrypt data with the key and the parameters set at initialization.

The cipher object is stateful; encryption of a long block of data can be broken up in two or more calls to encrypt(). That is, the statement:

c.encrypt(a) + c.encrypt(b)

is always equivalent to:

c.encrypt(a+b)

That also means that you cannot reuse an object for encrypting or decrypting other data with the same key.

So your problem is actually directly documented in the class.



来源:https://stackoverflow.com/questions/43199234/pycrypto-aes-decryption

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