如何使用 Python 实现二进制文件的加密和解密?请设计一个基于 AES 或 RSA 算法的完整流程。

以下是一个基于 AES 算法的二进制文件加密和解密的完整流程实现示例。AES(Advanced Encryption Standard)是一种对称加密算法,适合用于文件加密。本示例将展示如何生成密钥、加密文件和解密文件的全过程。

1. 安装依赖库

首先需要安装 pycryptodome 库,它是 Python 中用于密码学操作的常用库。

pip install pycryptodome

2. 生成 AES 密钥

AES 需要一个 16、24 或 32 字节的密钥。可以通过以下方式生成一个随机密钥:

from Crypto.Random import get_random_bytes

def generate_aes_key():
    key = get_random_bytes(16)  # 16 bytes for AES-128
    return key

key = generate_aes_key()
print(f"AES Key: {key.hex()}")

3. 加密文件

使用生成的密钥对二进制文件进行加密。加密过程包括以下步骤:

  • 读取文件内容。
  • 使用密钥和初始化向量(IV)加密数据。
  • 将 IV 和加密数据写入新文件。
from Crypto.Cipher import AES
from Crypto.Util.Padding import pad

def encrypt_file(input_file, output_file, key):
    cipher = AES.new(key, AES.MODE_CBC)
    iv = cipher.iv

    with open(input_file, 'rb') as f_in:
        data = f_in.read()
        padded_data = pad(data, AES.block_size)  # 填充数据
        encrypted_data = cipher.encrypt(padded_data)

    with open(output_file, 'wb') as f_out:
        f_out.write(iv)
        f_out.write(encrypted_data)

# 示例:加密文件
input_file = 'example.bin'
output_file = 'encrypted.bin'
encrypt_file(input_file, output_file, key)
print(f"文件 {input_file} 已加密为 {output_file}")

4. 解密文件

解密过程是对加密操作的逆操作:

  • 读取加密文件中的 IV 和加密数据。
  • 使用密钥和 IV 解密数据。
  • 去除填充后保存为原始文件。
from Crypto.Util.Padding import unpad

def decrypt_file(input_file, output_file, key):
    with open(input_file, 'rb') as f_in:
        iv = f_in.read(16)
        encrypted_data = f_in.read()

    cipher = AES.new(key, AES.MODE_CBC, iv=iv)
    decrypted_data = unpad(cipher.decrypt(encrypted_data), AES.block_size)

    with open(output_file, 'wb') as f_out:
        f_out.write(decrypted_data)

# 示例:解密文件
decrypted_file = 'decrypted.bin'
decrypt_file(output_file, decrypted_file, key)
print(f"文件 {output_file} 已解密为 {decrypted_file}")

5. 完整示例代码

以下是将上述代码整合为一个完整的示例:

from Crypto.Random import get_random_bytes
from Crypto.Cipher import AES
from Crypto.Util.Padding import pad, unpad

# 生成 AES 密钥
def generate_aes_key():
    return get_random_bytes(16)

# 加密文件
def encrypt_file(input_file, output_file, key):
    cipher = AES.new(key, AES.MODE_CBC)
    iv = cipher.iv

    with open(input_file, 'rb') as f_in:
        data = f_in.read()
        padded_data = pad(data, AES.block_size)
        encrypted_data = cipher.encrypt(padded_data)

    with open(output_file, 'wb') as f_out:
        f_out.write(iv)
        f_out.write(encrypted_data)

# 解密文件
def decrypt_file(input_file, output_file, key):
    with open(input_file, 'rb') as f_in:
        iv = f_in.read(16)
        encrypted_data = f_in.read()

    cipher = AES.new(key, AES.MODE_CBC, iv=iv)
    decrypted_data = unpad(cipher.decrypt(encrypted_data), AES.block_size)

    with open(output_file, 'wb') as f_out:
        f_out.write(decrypted_data)

# 主函数
if __name__ == "__main__":
    key = generate_aes_key()
    print(f"AES Key: {key.hex()}")

    input_file = 'example.bin'
    encrypted_file = 'encrypted.bin'
    decrypted_file = 'decrypted.bin'

    encrypt_file(input_file, encrypted_file, key)
    print(f"文件 {input_file} 已加密为 {encrypted_file}")

    decrypt_file(encrypted_file, decrypted_file, key)
    print(f"文件 {encrypted_file} 已解密为 {decrypted_file}")

6. 注意事项

  1. 密钥管理:AES 是对称加密算法,密钥的安全性非常重要。务必妥善保存密钥,避免泄露。
  2. IV 的作用:IV 用于确保相同明文在不同加密操作中生成不同的密文,每次加密时都应使用随机 IV。
  3. 填充:AES 要求明文长度为块大小的整数倍,因此需要对数据进行填充。pad 和 unpad 函数用于实现这一功能。
  4. 文件大小限制:由于内存限制,此示例适合小型文件。对于大文件,建议分块读取和加密以避免内存溢出。

通过以上步骤,可以实现二进制文件的 AES 加密和解密功能,确保数据的安全性。