Python M2Crypto使用AES256加密/解密
有人能给我提供使用m2crypto aes256 CBC加密/解密的代码吗?使用Python当涉及到安全性时,没有什么比阅读文档更好的了Python M2Crypto使用AES256加密/解密,python,aes,m2crypto,Python,Aes,M2crypto,有人能给我提供使用m2crypto aes256 CBC加密/解密的代码吗?使用Python当涉及到安全性时,没有什么比阅读文档更好的了 即使我花时间去理解并制作完美的代码供您复制和粘贴,您也不会知道我是否做得很好。我知道这不是很有帮助,但我祝你好运,数据安全。M2Crypto的文档非常糟糕。有时OpenSSL文档(m2crypto包装OpenSSL)会有所帮助。您最好的选择是查看M2Crypto单元测试-----查找test\u AES()方法。查看: 小型实用程序和模块 使用 对称密钥算法
即使我花时间去理解并制作完美的代码供您复制和粘贴,您也不会知道我是否做得很好。我知道这不是很有帮助,但我祝你好运,数据安全。M2Crypto的文档非常糟糕。有时OpenSSL文档(m2crypto包装OpenSSL)会有所帮助。您最好的选择是查看M2Crypto单元测试-----查找
test\u AES()
方法。查看:
小型实用程序和模块
使用
对称密钥算法。默认情况下
使用CBC使用256位AES(Rijndael),
但有些选项是可配置的。
用于派生密钥的PBKDF2算法
从密码
我在M2Crypto(借用自)周围使用以下包装器:
+1... 乔,今天当我在做一个宠物项目的时候,我非常感激这个答案。当我看到M2Crypto文档时,我开始觉得自己疯了,而且,嗯,对epydoc API的评论很少;当然,我错过了一些明显的东西!谢谢你恢复我的信仰。那个链接不再有效。你可以看看[在这篇文章][1]。[1]:
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()
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