M2Crypto的AES问题

时间:2011-02-15 12:27:25

标签: python m2crypto

有人可以指出这段代码中的错误:

        __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)

错误:

        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

1 个答案:

答案 0 :(得分:2)

在更正缩进和__main__中的细微更改后,您的代码似乎与Python 2.7.3M2Crypto-0.21.1一起使用:

__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 decryption successful\n"
   return decrypt(msg)

if __name__ == "__main__":
   key="123452345"
   msg="qwrtttrtyutyyyyy"
   encrypted_msg=AES_encryptor(b64encode(key),b64encode(msg))
   print b64decode(AES_decryptor(b64encode(key),encrypted_msg))