Bdfy
Bdfy

Reputation: 24621

simple example aes256 crypt

Why this example doesn't work ?

from Crypto.Cipher import AES
x = AES.new("sdsfdsafsadfdsafasdfdsarwe876539", AES.MODE_CBC, "2324234342342342")
print x.decrypt(x.encrypt('abcdfghkbhgjrdfs'))

Upvotes: 1

Views: 2258

Answers (1)

bobince
bobince

Reputation: 536379

Because x is an object with state. Using it to encrypt a string changes the state; using it again will generate different output.

Use a new AES cipher with the same initial state as you had when encrypting:

>>> from Crypto.Cipher import AES
>>> key= "sdsfdsafsadfdsafasdfdsarwe876539"
>>> prefix= '2324234342342342'
>>> AES.new(key, AES.MODE_CBC, prefix).encrypt('abcdfghkbhgjrdfs')
'\xf4\xd9\xd1B8\xc1\x16\xe1\x9b~\xd0\x99\x1c\xf8\xdfn'
>>> AES.new(key, AES.MODE_CBC, prefix).decrypt(_)
'abcdfghkbhgjrdfs'

Upvotes: 7

Related Questions