python - 在 Python 中使用 RSA 加密文件

标签 python encryption public-key-encryption

我正在使用 PyCrypto 通过 RSA 实现文件加密。

我知道这有点不对,首先是因为 RSA 非常慢,其次是因为 PyCrypto RSA 只能加密 128 个字符,因此您必须将文件分解为 128 个字符的 block 。

这是目前的代码:

from Crypto.PublicKey import RSA

file_to_encrypt = open('my_file.ext', 'rb').read()
pub_key = open('my_pub_key.pem', 'rb').read()
o = RSA.importKey(pub_key)

to_join = []
step = 0

while 1:
    # Read 128 characters at a time.
    s = file_to_encrypt[step*128:(step+1)*128]
    if not s: break
    # Encrypt with RSA and append the result to list.
    # RSA encryption returns a tuple containing 1 string, so i fetch the string.
    to_join.append(o.encrypt(s, 0)[0])
    step += 1

# Join the results.
# I hope the \r\r\r sequence won't appear in the encrypted result,
# when i explode the string back for decryption.
encrypted = '\r\r\r'.join(to_join)
# Write the encrypted file.
open('encrypted_file.ext', 'wb').write(encrypted)

所以我的问题是:是否有更好的方法在文件上使用私钥/公钥加密?

听说过Mcrypt和OpenSSL,不知道能不能加密文件。

最佳答案

公钥加密通常只用于少量数据。它很慢,而且很难正确使用。通常的做法是使用其他方法将不对称问题减少到由共享 key 提供安全性的问题,然后使用公钥密码术来保护该共享 key 。例如:

  • 要加密文件,请随机生成 block 密码或流密码(例如 AES)的 key 。存储使用此密码加密的数据,并将使用公钥加密的 key 与加密的有效负载一起存储。
  • 要签署文件,请计算加密摘要(例如 SHA-256)。使用私钥签署文件摘要并将其与文件一起存储。

下面是加密的草图(警告,未经测试的代码,直接在浏览器中输入):

import os
from Crypto.Cipher import AES
from Crypto.PublicKey import RSA
import Crypto.Util.number
def encrypt_file(rsa, input, output):
    # Generate secret key
    secret_key = os.urandom(16)
    # Padding (see explanations below)
    plaintext_length = (Crypto.Util.number.size(rsa.n) - 2) / 8
    padding = '\xff' + os.urandom(16)
    padding += '\0' * (plaintext_length - len(padding) - len(secret_key))
    # Encrypt the secret key with RSA
    encrypted_secret_key = rsa.encrypt(padding + secret_key, None)
    # Write out the encrypted secret key, preceded by a length indication
    output.write(str(len(encrypted_secret_key)) + '\n')
    output.write(encrypted_secret_key)
    # Encrypt the file (see below regarding iv)
    iv = '\x00' * 16
    aes_engine = AES.new(secret_key, AES.MODE_CBC, iv)
    output.write(aes_engine.encrypt(input.read()))

iv 是一个 initialization vector 对于 CBC mode of operation .每条消息的每个 key 都必须是唯一的。通常,它以明文形式与数据一起发送。在这里,由于 key 只使用过一次,因此您可以使用已知的 IV。

block 密码的 API 在 PEP 272 中描述。 .不幸的是,它只支持一次性加密。对于大文件,最好逐 block 加密;您一次可以加密小至一个 block (AES 为 16 字节),但为此您需要一个更好的加密库。

请注意,一般情况下,您不应直接使用 RSA 加密数据。最明显的问题是攻击者知道公钥,因此可以尝试猜测明文(如果攻击者认为明文可能是 swordfish,则攻击者可以加密 swordfish 使用 RSA 公钥,并将结果与​​ RSA 加密的输出进行比较)。如果您想将文件发送给多个收件人,可能会出现的另一个问题是,如果 RSA 加密步骤是确定性的,那么攻击者可以判断明文是相同的,因为密文是相同的。针对这些问题的正常防御是使用 padding scheme ,包括向明文添加一些随机 secret 数据;此数据称为填充。然后攻击者无法猜测随机数据,并且每次加密都会看到不同的结果,因为相同的明文永远不会被加密两次;就合法接收者而言,填充只是可以丢弃的数据。

在这里,上述问题似乎不适用于这种情况。但是,使用未 protected RSA 可能会带来其他弱点。特别是,如果公共(public)指数非常小(这里不是 PyCrypto 使用 65537 的情况)或者您为许多不同的收件人加密相同的 Material (同样,这里可能不是这种情况,因为每条消息都有自己的 key ),那么simple mathematical calculation would allow the attacker to recover the RSA plaintext .为避免这种攻击,使用 RSA 加密的值需要“足够接近”RSA 模数,以便加密操作实际上执行模幂运算。我建议的填充确保通过制作适合 0xff 的最高位字节; this is believed to be safe ,虽然在现实世界中你应该使用批准的填充模式(OAEP)。

关于python - 在 Python 中使用 RSA 加密文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6309958/

相关文章:

python - 序列化包含 Pandas 数据帧的字典 (Python)

python - B 列中具有匹配 A 列值的行的总和值

python - 在 python __init__ 中捕获多个参数之一

c - openSSL:如何初始化公钥加密的 key ?

c# - 在 C# 中使用 RSACryptoServiceProvider 加密/解密 excel 文件?

python - 在执行 python 脚本之前切换用户

security - 解密文件时索引超出范围

ios - 验证来自 iOS transactionReceipt 的签名

java - 如何在Java中比较sha1加密的密码?

google-cloud-platform - 如何在 Google Cloud Platform 中获取用户托管服务帐户的公钥