前言
有个二进制文件需要进行加解密的需求,选用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
库来实现对文件的加密和解密。下面是代码的实现思路和过程,以及其中涉及的知识点:
-
导入模块:
from cryptography.hazmat.backends import default_backend
: 导入默认的密码学后端。from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes
: 导入Cipher
对象、AES 加密算法和加密模式。
-
生成随机密钥:
key = os.urandom(16)
: 使用os.urandom
生成一个随机的 128 位(16 字节)密钥。
-
加密函数
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 更新块并进行加密,然后写入输出文件。- 循环直到文件末尾。
-
解密函数
decrypt_file
:- 与加密过程类似,不同之处在于解密函数使用相同的 IV 来解密文件。
- 在每个块的循环中重新创建 decryptor,并使用 decryptor 更新块并进行解密。
-
文件路径和调用:
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
函数以二进制模式打开文件,并使用write
和read
方法进行文件的写入和读取。 - 随机数生成: 使用
os.urandom
生成随机的密钥和初始向量。 - 循环处理文件块: 通过循环处理文件的每个块,实现对大文件的逐块加密和解密。