有人可以提供我使用 Python 使用 m2crypto aes256 CBC 加密/解密的代码吗
mike
问问题
10469 次
5 回答
14
M2Crypto 的文档很糟糕。有时 OpenSSL 文档(m2crypto 包装 OpenSSL)可以提供帮助。你最好的办法是查看 M2Crypto 单元测试——https://gitlab.com/m2crypto/m2crypto/blob/master/tests/test_evp.py——寻找方法test_AES()
。
于 2009-05-12T19:24:30.113 回答
3
看看m2secret:
使用对称密钥算法加密和解密数据的小型实用程序和模块。默认情况下使用 CBC 的 256 位 AES (Rijndael),但有些选项是可配置的。用于从密码派生密钥的 PBKDF2 算法。
于 2009-05-12T21:48:36.367 回答
3
我在 M2Crypto 周围使用以下包装器(从cryptography.io借来):
import os
import base64
import M2Crypto
class SymmetricEncryption(object):
@staticmethod
def generate_key():
return base64.b64encode(os.urandom(48))
def __init__(self, key):
key = base64.b64decode(key)
self.iv = key[:16]
self.key = key[16:]
def encrypt(self, plaintext):
ENCRYPT = 1
cipher = M2Crypto.EVP.Cipher(alg='aes_256_cbc', key=self.key, iv=self.iv, op=ENCRYPT)
ciphertext = cipher.update(plaintext) + cipher.final()
return base64.b64encode(ciphertext)
def decrypt(self, cyphertext):
DECRYPT = 0
cipher = M2Crypto.EVP.Cipher(alg='aes_256_cbc', key=self.key, iv=self.iv, op=DECRYPT)
plaintext = cipher.update(base64.b64decode(cyphertext)) + cipher.final()
return plaintext
于 2016-06-21T12:58:51.133 回答
2
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()
于 2013-09-25T10:26:01.170 回答
-1
在安全性方面,没有什么比阅读文档更好的了。
http://chandlerproject.org/bin/view/Projects/MeTooCrypto
即使我花时间理解并制作完美的代码供您复制和粘贴,您也不知道我是否做得好。我知道这不是很有帮助,但我祝你好运并保护数据。
于 2009-05-12T15:56:19.440 回答