from tkinter import filedialog
from Crypto.Cipher import AES
from Crypto.Util.Padding import pad, unpad
import os
def encrypt_file(input_file, output_file, key):
cipher = AES.new(key, AES.MODE_CBC)
with open(input_file, 'rb') as file_in:
plaintext = file_in.read()
ciphertext = cipher.encrypt(pad(plaintext, AES.block_size))
with open(output_file, 'wb') as file_out:
file_out.write(cipher.iv)
file_out.write(ciphertext)
def decrypt_file(input_file, output_file, key):
with open(input_file, 'rb') as file_in:
iv = file_in.read(AES.block_size)
ciphertext = file_in.read()
cipher = AES.new(key, AES.MODE_CBC, iv)
plaintext = unpad(cipher.decrypt(ciphertext), AES.block_size)
with open(output_file, 'wb') as file_out:
file_out.write(plaintext)
def browse_input_file():
input_file = filedialog.askopenfilename()
if input_file:
input_entry.delete(0, tk.END)
input_entry.insert(0, input_file)
def browse_output_file():
output_file = filedialog.asksaveasfilename()
if output_file:
output_entry.delete(0, tk.END)
output_entry.insert(0, output_file)
# 对文件进行16位字符加密操作
def encrypt():
key = key_entry.get().encode()
input_file = input_entry.get()
output_folder = filedialog.askdirectory()
if output_folder:
encrypt_file(input_file, output_folder, key)
result_label.config(text="Encryption completed.")
def encrypt_file(input_file, output_folder, key):
cipher = AES.new(key, AES.MODE_CBC)
with open(input_file, 'rb') as file_in:
plaintext = file_in.read()
ciphertext = cipher.encrypt(pad(plaintext, AES.block_size))
input_filename = os.path.basename(input_file)
output_filename = input_filename + ".encrypted"
output_file_path = os.path.join(output_folder, output_filename)
with open(output_file_path, 'wb') as file_out:
file_out.write(cipher.iv)
file_out.write(ciphertext)
# 对文件进行16位字符解密
def decrypt():
key = key_entry.get().encode()
input_file = input_entry.get()
output_folder = os.path.dirname(input_file)
decrypt_file(input_file, output_folder, key)
result_label.config(text="Decryption completed.")
def encrypt_file(input_file, output_folder, key):
cipher = AES.new(key, AES.MODE_CBC)
with open(input_file, 'rb') as file_in:
plaintext = file_in.read()
ciphertext = cipher.encrypt(pad(plaintext, AES.block_size))
output_filename = os.path.basename(input_file) + ".encrypted"
output_file_path = os.path.join(output_folder, output_filename)
with open(output_file_path, 'wb') as file_out:
file_out.write(cipher.iv)
file_out.write(ciphertext)
def decrypt_file(input_file, output_folder, key):
with open(input_file, 'rb') as file_in:
iv = file_in.read(16) # 读取 IV(前16字节数据)
ciphertext = file_in.read()
cipher = AES.new(key, AES.MODE_CBC, iv)
plaintext = unpad(cipher.decrypt(ciphertext), AES.block_size)
output_filename = os.path.splitext(os.path.basename(input_file))[0] # 去除 ".encrypted" 扩展名
output_file_path = os.path.join(output_folder, output_filename)
with open(output_file_path, 'wb') as file_out:
file_out.write(plaintext)
# 创建主窗口
window = tk.Tk()
window.title("文件加密解密工具")
# 输入密钥
key_label = tk.Label(window, text="密钥:")
key_label.pack()
key_entry = tk.Entry(window, show="*")
key_entry.pack()
# 输入文件
input_frame = tk.Frame(window)
input_frame.pack()
input_label = tk.Label(input_frame, text="输入文件:")
input_label.pack(side=tk.LEFT)
input_entry = tk.Entry(input_frame, width=40)
input_entry.pack(side=tk.LEFT)
input_button = tk.Button(input_frame, text="浏览", command=browse_input_file)
input_button.pack(side=tk.LEFT)
# 输出文件
output_frame = tk.Frame(window)
output_frame.pack()
output_label = tk.Label(output_frame, text="输出文件:")
output_label.pack(side=tk.LEFT)
output_entry = tk.Entry(output_frame, width=40)
output_entry.pack(side=tk.LEFT)
output_button = tk.Button(output_frame, text="浏览", command=browse_output_file)
output_button.pack(side=tk.LEFT)
# 加密按钮
encrypt_button = tk.Button(window, text="加密", command=encrypt)
encrypt_button.pack()
# 解密按钮
decrypt_button = tk.Button(window, text="解密", command=decrypt)
decrypt_button.pack()
# 结果标签
result_label = tk.Label(window, text="")
result_label.pack()
# 运行主循环
window.mainloop()