Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jun 2nd, 2012  |  syntax: None  |  size: 1.34 KB  |  hits: 29  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Decrypt in Python an encrypted message in Java
  2. EVPError: 'wrong final block length'
  3.        
  4. import M2Crypto
  5. from base64 import b64encode, b64decode
  6.  
  7. ENC=1
  8. DEC=0
  9.  
  10. def AES_build_cipher(key, iv, op=ENC):
  11.     """"""""
  12.     return M2Crypto.EVP.Cipher(alg='aes_128_cbc', key=key, iv=iv, op=op)
  13.  
  14. def AES_encryptor(key,msg, iv=None):
  15.     """"""
  16.     #Decode the key and iv
  17.     key = b64decode(key)
  18.     if iv is None:
  19.         iv = '' * 16
  20.     else:
  21.         iv = b64decode(iv)
  22.  
  23.    # Return the encryption function
  24.     def encrypt(data):
  25.         cipher = AES_build_cipher(key, iv, ENC)
  26.         v = cipher.update(data)
  27.         v = v + cipher.final()
  28.         del cipher
  29.         v = b64encode(v)
  30.         return v
  31.     print "AES encryption successfuln"
  32.     return encrypt(msg)
  33.  
  34. def AES_decryptor(key,msg, iv=None):
  35.     """"""
  36.     #Decode the key and iv
  37.     key = b64decode(key)
  38.     print key
  39.     print
  40.     if iv is None:
  41.         iv = '' * 16
  42.     else:
  43.         iv = b64decode(iv)
  44.  
  45.    # Return the decryption function
  46.     def decrypt(data):
  47.         data = b64decode(data)
  48.         cipher = AES_build_cipher(key, iv, DEC)
  49.         v = cipher.update(data)
  50.         v = v + cipher.final()
  51.         del cipher
  52.         return v
  53.     print "AES dencryption successfuln"
  54.     return decrypt(msg)
  55.  
  56. if __name__ == "__main__":
  57.     result = AES_decryptor(b64encode(SECRET_KEY), msg=encrypted_message)