欢迎访问宙启技术站
智能推送

使用Python进行文件的RSA加解密

发布时间:2023-12-27 15:52:36

RSA是一种非对称加密算法,可以用于文件的加密和解密。在Python中,我们可以使用cryptography库来实现RSA加解密功能。

首先,我们需要安装cryptography库。可以使用pip命令在终端中执行以下命令进行安装:

pip install cryptography

安装完毕后,我们可以开始使用RSA进行文件的加密和解密。

下面是一个例子,演示了如何使用RSA加密和解密一个文本文件。

from cryptography.fernet import Fernet
from cryptography.hazmat.backends import default_backend
from cryptography.hazmat.primitives.asymmetric import rsa, padding
from cryptography.hazmat.primitives import serialization, hashes

def generate_rsa_key_pair():
    # 生成RSA密钥对
    private_key = rsa.generate_private_key(
        public_exponent=65537,
        key_size=2048,
        backend=default_backend()
    )
    public_key = private_key.public_key()
    
    # 将私钥和公钥保存到文件中(PEM格式)
    pem_private_key = private_key.private_bytes(
        encoding=serialization.Encoding.PEM,
        format=serialization.PrivateFormat.PKCS8,
        encryption_algorithm=serialization.NoEncryption()
    )
    with open('private_key.pem', 'wb') as f:
        f.write(pem_private_key)
    
    pem_public_key = public_key.public_bytes(
        encoding=serialization.Encoding.PEM,
        format=serialization.PublicFormat.SubjectPublicKeyInfo
    )
    with open('public_key.pem', 'wb') as f:
        f.write(pem_public_key)

def encrypt_file(input_file_path, output_file_path, public_key_path):
    # 从文件中加载公钥
    with open(public_key_path, 'rb') as f:
        public_key = serialization.load_pem_public_key(f.read(), backend=default_backend())
    
    # 读取要加密的文件
    with open(input_file_path, 'rb') as f:
        data = f.read()
    
    # 使用公钥对数据进行加密
    encrypted_data = public_key.encrypt(
        data,
        padding.OAEP(
            mgf=padding.MGF1(algorithm=hashes.SHA256()),
            algorithm=hashes.SHA256(),
            label=None
        )
    )
    
    # 将加密后的数据保存到文件中
    with open(output_file_path, 'wb') as f:
        f.write(encrypted_data)

def decrypt_file(input_file_path, output_file_path, private_key_path):
    # 从文件中加载私钥
    with open(private_key_path, 'rb') as f:
        private_key = serialization.load_pem_private_key(f.read(), password=None, backend=default_backend())
    
    # 读取要解密的文件
    with open(input_file_path, 'rb') as f:
        encrypted_data = f.read()
    
    # 使用私钥对数据进行解密
    decrypted_data = private_key.decrypt(
        encrypted_data,
        padding.OAEP(
            mgf=padding.MGF1(algorithm=hashes.SHA256()),
            algorithm=hashes.SHA256(),
            label=None
        )
    )
    
    # 将解密后的数据保存到文件中
    with open(output_file_path, 'wb') as f:
        f.write(decrypted_data)

# 生成RSA密钥对,保存到文件中
generate_rsa_key_pair()

# 加密文件
encrypt_file('input.txt', 'encrypted.txt', 'public_key.pem')

# 解密文件
decrypt_file('encrypted.txt', 'decrypted.txt', 'private_key.pem')

在上述代码中,我们定义了三个函数:

- generate_rsa_key_pair:用于生成RSA密钥对,并将私钥和公钥保存到文件中。

- encrypt_file:用于加密文件。

- decrypt_file:用于解密文件。

我们首先调用generate_rsa_key_pair函数生成RSA密钥对,并将私钥保存到private_key.pem文件,公钥保存到public_key.pem文件。

然后,我们调用encrypt_file函数对input.txt文件进行加密,并将加密后的数据保存到encrypted.txt文件中。

最后,我们调用decrypt_file函数对encrypted.txt文件进行解密,并将解密后的数据保存到decrypted.txt文件中。

使用以上代码,你可以很方便地使用RSA算法对文件进行加密和解密。请注意,为了保证安全性,你应该妥善保存私钥文件,不要将其泄露给他人。