M2Crypto使用AES256加密/解密

2024-05-29 10:54:50 发布

您现在位置:Python中文网/ 问答频道 /正文

有人能给我提供使用m2cypto aes256 CBC使用Python加密/解密的代码吗


Tags: 代码aes256cbcm2cypto
3条回答

M2Crypto的文档很糟糕。有时OpenSSL文档(m2cypto包装OpenSSL)可以提供帮助。最好的方法是查看M2Crypto单元测试——https://gitlab.com/m2crypto/m2crypto/blob/master/tests/test_evp.py——查找test_AES()方法。

def encrypt_file(key, in_filename, out_filename,iv):
    cipher=M2Crypto.EVP.Cipher('aes_256_cfb',key,iv, op=1)
    with open(in_filename, 'rb') as infile:
        with open(out_filename, 'wb') as outfile:
          outfile.write(b)
          while True:
            buf = infile.read(1024)
            if not buf:
                break
            outfile.write(cipher.update(buf))

          outfile.write( cipher.final() )  
          outfile.close()
        infile.close()

def decrypt_file(key, in_filename, out_filename,iv):
    cipher = M2Crypto.EVP.Cipher("aes_256_cfb",key , iv, op = 0)
    with open(in_filename, 'rb') as infile: 
        with open(out_filename, 'wb') as outfile:
          while True:
            buf = infile.read(1024)
            if not buf:
                break
            try:
                outfile.write(cipher.update(buf))
            except:
                print "here"
          outfile.write( cipher.final() )  
          outfile.close()
        infile.close()

看看m2secret

Small utility and module for encrypting and decrypting data using symmetric-key algorithms. By default uses 256-bit AES (Rijndael) using CBC, but some options are configurable. PBKDF2 algorithm used to derive key from password.

相关问题 更多 >

    热门问题