[英]Encrypting a file with RSA in Python
我正在使用 PyCrypto 使用 RSA 实现文件加密。
我知道这有点不对,首先是因为 RSA 非常慢,其次是因为 PyCrypto RSA 只能加密 128 个字符,因此您必须将文件分解为 128 个字符块。
这是到目前为止的代码:
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,但我不知道它们是否可以加密文件。
公钥加密通常仅用于少量数据。 它很慢,而且很难正确使用。 通常的做法是使用其他方法将非对称问题减少到由共享密钥提供安全性的问题,然后使用公钥密码术来保护该共享密钥。 例如:
所以这里有一个加密看起来像的草图(警告,未经测试的代码,直接在浏览器中输入):
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
是CBC操作模式的初始化向量。 每条消息的每个密钥都必须是唯一的。 通常,它与数据一起以明文形式发送。 在这里,由于密钥只使用一次,您可以使用已知的 IV。
PEP 272中描述了分组密码的 API。 不幸的是,它只支持一次性加密。 对于大文件,最好逐块加密; 您一次只能加密一个块(AES 为 16 个字节),但您需要一个更好的加密库。
请注意,一般情况下,您不应直接使用 RSA 加密数据。 最明显的担心是攻击者知道公钥,因此可以尝试猜测明文(如果攻击者认为明文可能是swordfish
,那么攻击者可以用RSA公钥加密swordfish
,并与output比较结果RSA 加密)。 如果您想将文件发送给多个收件人,另一个问题是,如果 RSA 加密步骤是确定性的,那么攻击者可以判断明文是相同的,因为密文是相同的。 针对这些问题的正常防御是使用填充方案,其中包括在明文中添加一些随机秘密数据; 该数据称为填充。 然后,攻击者无法猜测随机数据,并且每次加密都会看到不同的结果,因为相同的明文永远不会被加密两次; 就合法接收者而言,填充只是可以丢弃的数据。
在这里,上述问题似乎不适用于这种情况。 但是,使用不受保护的 RSA 可能会产生其他弱点。 特别是,如果公共指数非常小(这里不是 PyCrypto 使用 65537 的情况)或者您为许多不同的接收者加密相同的材料(同样,这里可能不是这种情况,因为每条消息都有自己的密钥),然后简单的数学计算将允许攻击者恢复 RSA 明文。 为了避免这种攻击,使用 RSA 加密的值需要与 RSA 模数“足够接近”,以便加密操作实际上执行模幂运算。 我建议的填充确保通过制作适合 0xff 的最高字节; 这被认为是安全的,尽管在现实世界中您应该使用经过批准的填充模式 ( OAEP )。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.