使用M2Cryp加密文件

2024-10-01 00:16:54 发布

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

众所周知,我可以读取内存中的整个文件内容,并使用以下代码对其进行加密。在

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,而不是先读取整个文件呢?在


Tags: 文件key内存代码内容readcontentsaes
1条回答
网友
1楼 · 发布于 2024-10-01 00:16:54

我知道你可以多次打电话更新数据。在

为了最大限度地减少内存使用并使用文件进行输出,您应该能够做到:

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() )

相关问题 更多 >