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 ?
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))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With