开发者

simple example aes256 crypt

Why this example doesn't work ?

from Crypto.Cipher import AES
x = AES.new("sdsfdsafs开发者_JAVA技巧adfdsafasdfdsarwe876539", AES.MODE_CBC, "2324234342342342")
print x.decrypt(x.encrypt('abcdfghkbhgjrdfs'))


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'
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜