Can someone please point out mistakes in this code:
__author__="gaurav"
__date__ ="$15 Feb, 2011 5:10:59 PM$"
import M2Crypto
from base64 import b64encode, b64decode
ENC=1
DEC=0
def AES_build_cipher(key, iv, op=ENC):
""""""""
return M2Crypto.EVP.Cipher(alg='aes_128_cbc', key=key, iv=iv, op=op)
def AES_encryptor(key,msg, iv=None):
""""""
#Decode the key and iv
key = b64decode(key)
if iv is None:
iv = '\0' * 16
else:
iv = b64decode(iv)
# Return the encryption function
def encrypt(data):
cipher = AES_build_cipher(key, iv, ENC)
v = cipher.update(data)
v = v + cipher.final()
del cipher
v = b64encode(v)
return v
print "AES encryption successful\n"
return encrypt(msg)
def AES_decryptor(key,msg, iv=None):
""""""
#Decode the key and iv
key = b64decode(key)
if iv is None:
iv = '\0' * 16
else:
iv = b64decode(iv)
# Return the decryption function
def decrypt(data):
data = b64decode(data)
cipher = AES_build_cipher(key, iv, DEC)
v = cipher.update(data)
v = v + cipher.final()
del cipher
return v
print "AES dencryption successful\n"
return decrypt(msg)
if __name__ == "__main__":
msg=AES_encryptor(b64encode("123452345"),msg=b64encode("qwrtttrtyutyyyyy"))
print AES_decryptor(b64encode("123452345"),msg=msg)
Error:
AES encryption successful
AES dencryption successful
Traceback (most recent call last):
File "/home/gaurav/NetBeansProjects/temp/src/temp.py", line 54, in <module>
print AES_decryptor(b64encode("123452345"),msg)
File "/home/gaurav/NetBeansProjects/temp/src/temp.py", line 51, in AES_decryptor
return decrypt(iv)
File "/home/gaurav/NetBeansProjects/temp/src/temp.py", line 47, in decrypt
v = v + cipher.final()
File "/usr/local/lib/python2.6/dist-packages/M2Crypto-0.21.1-py2.6-linux-i686.egg/M2Crypto/EVP.py", line 128, in final
return m2.cipher_final(self.ctx)
M2Crypto.EVP.EVPError: wrong final block length