python aes encrypt/decrypt does not return the same results

我的未来我决定 提交于 2019-12-19 09:49:26

问题


below code sample does not return the original text after encrypt/decrypt operation and I am trying to figure it out why

from Crypto.Cipher import AES

text = """This is plain text 
to use.
It should be exqctly 128 characters long to avoid padding and it is split
with new lines as in 
file"""

password = "password........"

block = 32
mode = AES.MODE_CBC

enc = AES.new(password, mode)

encrypted = enc.encrypt(text)

print "ORIGINAL: " + text

print "ENCRYPTED: " + str(encrypted)

print "DECRYPTED: " + str(enc.decrypt(encrypted))

can one tell why first part of the text is malformed ?


回答1:


I think, you need to reset the initialisation vector (IV), in order to get the desired result. The easist way might be to create a new AES object for decrypting:

enc = AES.new(password, mode)
encrypted = enc.encrypt(text)
print "ORIGINAL: " + text
print "ENCRYPTED: " + str(encrypted)
dec = AES.new(password, mode)
print "DECRYPTED: " + str(dec.decrypt(encrypted))


来源:https://stackoverflow.com/questions/9394773/python-aes-encrypt-decrypt-does-not-return-the-same-results

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