Pycrypto: AES decryption

Why does Pycrypto AES decryption give different results when decrypted with the AES object used for encryption and correct output when decrypted with the AES object used exclusively 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

      

+3


source to share


1 answer


According to BlockAlgo#encrypt

which the AES class is derived from:

Encrypt data using the key and parameters set during initialization.

Encryption object state ; encrypting a long block of data can be split into two or more calls to encrypt (). That is, the statement:

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

      

is always equivalent to:

c.encrypt(a+b)

      

This also means that you cannot reuse an object to encrypt or decrypt other data with the same key.



So your problem is actually documented in the class.

+3


source







All Articles