python实现文件加密aes加解密二进制文件加解密

本文详细介绍了如何使用Python的cryptography库,特别是AES加密算法和CFB模式,实现对二进制文件的加密和解密过程,包括随机密钥和初始向量的生成,以及文件I/O操作的处理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

前言

有个二进制文件需要进行加解密的需求,选用aes进行加解密,简单记录下实现,python编码

实现步骤

from cryptography.hazmat.backends import default_backend
from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes
import os

def encrypt_file(input_file_path, output_file_path, key):
    chunk_size = 16 * 1024  # 16 KB
    iv = os.urandom(16)
    cipher = Cipher(algorithms.AES(key), modes.CFB(iv), backend=default_backend())

    with open(input_file_path, 'rb') as infile, open(output_file_path, 'wb') as outfile:
        outfile.write(iv)  # 写入 IV 到输出文件
        while True:
            encryptor = cipher.encryptor()  # 在每个块的循环中重新创建 encryptor
            chunk = infile.read(chunk_size)
            if not chunk:
                break

            encrypted_chunk = encryptor.update(chunk) + encryptor.finalize()
            outfile.write(encrypted_chunk)

def decrypt_file(input_file_path, output_file_path, key):
    chunk_size = 16 * 1024  # 16 KB
    with open(input_file_path, 'rb') as infile, open(output_file_path, 'wb') as outfile:
        iv = infile.read(16)  # 从输入文件读取 IV
        cipher = Cipher(algorithms.AES(key), modes.CFB(iv), backend=default_backend())
        while True:
            decryptor = cipher.decryptor()  # 在每个块的循环中重新创建 decryptor
            chunk = infile.read(chunk_size)
            if not chunk:
                break

            decrypted_chunk = decryptor.update(chunk) + decryptor.finalize()
            outfile.write(decrypted_chunk)


if __name__ == "__main__":
    # 生成随机的128位密钥(16字节)
    key = os.urandom(16)

    # 你的二进制文件路径
    input_file_path = r'C:\Users\admin\Desktop\fsdownload\test.txt'

    # 加密文件
    encrypt_file(input_file_path, r'C:\Users\admin\Desktop\fsdownload\test_aes.txt', key)

    # 解密文件
    decrypt_file(r'C:\Users\admin\Desktop\fsdownload\test_aes.txt', r'C:\Users\admin\Desktop\fsdownload\test_dec.txt', key)

这段代码使用了 Python 的 cryptography 库来实现对文件的加密和解密。下面是代码的实现思路和过程,以及其中涉及的知识点:

  1. 导入模块:

    • from cryptography.hazmat.backends import default_backend: 导入默认的密码学后端。
    • from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes: 导入 Cipher 对象、AES 加密算法和加密模式。
  2. 生成随机密钥:

    • key = os.urandom(16): 使用 os.urandom 生成一个随机的 128 位(16 字节)密钥。
  3. 加密函数 encrypt_file

    • iv = os.urandom(16): 生成一个随机的 128 位(16 字节)初始向量(IV)。
    • cipher = Cipher(algorithms.AES(key), modes.CFB(iv), backend=default_backend()): 创建 AES 加密算法的 Cipher 对象,使用 CFB 加密模式和随机生成的 IV。
    • with open(input_file_path, 'rb') as infile, open(output_file_path, 'wb') as outfile:: 打开输入文件和输出文件,使用二进制模式。
    • outfile.write(iv): 将 IV 写入输出文件。
    • while True:: 开始一个循环,每次读取文件的一个块(16 KB)。
    • encryptor = cipher.encryptor(): 在每个块的循环中重新创建 encryptor。
    • encrypted_chunk = encryptor.update(chunk) + encryptor.finalize(): 使用 encryptor 更新块并进行加密,然后写入输出文件。
    • 循环直到文件末尾。
  4. 解密函数 decrypt_file

    • 与加密过程类似,不同之处在于解密函数使用相同的 IV 来解密文件。
    • 在每个块的循环中重新创建 decryptor,并使用 decryptor 更新块并进行解密。
  5. 文件路径和调用:

    • if __name__ == "__main__":: 这个条件确保代码只在直接运行而非被导入时执行。
    • input_file_path = r'C:\Users\admin\Desktop\fsdownload\test.txt': 指定待加密的文件路径。
    • encrypt_file(input_file_path, r'C:\Users\admin\Desktop\fsdownload\test_aes.txt', key): 调用加密函数,加密文件。
    • decrypt_file(r'C:\Users\admin\Desktop\fsdownload\test_aes.txt', r'C:\Users\admin\Desktop\fsdownload\test_dec.txt', key): 调用解密函数,解密文件。

涉及的知识点:

  • AES 加密算法和 CFB 加密模式: 使用 cryptography 库中的 Cipher 对象、AES 加密算法和 CFB 加密模式来实现对文件的加密和解密。
  • 初始向量(IV): 在加密和解密中使用随机生成的 IV,确保每次加密的结果都是不同的,增强安全性。
  • 文件 I/O: 使用 open 函数以二进制模式打开文件,并使用 writeread 方法进行文件的写入和读取。
  • 随机数生成: 使用 os.urandom 生成随机的密钥和初始向量。
  • 循环处理文件块: 通过循环处理文件的每个块,实现对大文件的逐块加密和解密。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

莫忘初心丶

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值