python-使用M2Crypto加密文件
作者:互联网
众所周知,我可以读取内存中的整个文件内容,并使用以下代码对其进行加密.
contents = fin.read()
cipher = M2Crypto.EVP.Cipher(alg="aes_128_cbc", key = aes_key, iv = aes_iv, op = 1)
encryptedContents = cipher.update(contents)
encryptedContents += cipher.final()
但是,如果文件很大,该如何将输入流传递给M2Crypto而不是先读取整个文件呢?
解决方法:
我知道您可以多次调用.update(data).
为了最大程度地减少内存使用并将文件用于输出,您应该能够:
cipher = M2Crypto.EVP.Cipher(alg="aes_128_cbc", key = aes_key, iv = aes_iv, op = 1)
encrypted_contents_file = open(outfile_name, "w")
while True:
buf = fin.read(1024)
if not buf:
break
encrypted_contents_file.write(cipher.update(buf))
encrypted_contents_file.write( cipher.final() )
标签:m2crypto,encryption,python 来源: https://codeday.me/bug/20191127/2075138.html