Pycrypto : AES 解密

Pycrypto : AES Decryption

为什么 Pycrypto AES 解密在使用用于加密的 AES 对象解密时给出不同的输出,而在使用仅用于解密的 AES 对象解密时给出正确的输出?

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

根据派生 AES class 的 BlockAlgo#encrypt

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.

所以你的问题实际上直接记录在 class.