Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python aes encrypt/decrypt does not return the same results

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 ?

like image 353
m1k3y3 Avatar asked Feb 22 '12 12:02

m1k3y3


1 Answers

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))
like image 188
MartinStettner Avatar answered Nov 15 '22 05:11

MartinStettner