众所周知,我可以读取内存中的整个文件内容,并使用以下代码对其进行加密.
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() )