问题
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