from CryptoCipher import IDEA# 密钥为16字节key = b1234567890abcdefcipher = IDEAnewkey IDEAMODE_ECB # 创建加密器# 待加密的数据为8字节data = babcdefghencrypt_data = cipherencryptdataprint加密后的数据: encrypt_data# 解密decrypt_d
import tkinter as tk from tkinter import filedialog, messagebox from Crypto.Cipher import IDEA from docx import Document import openpyxl
def IDEA_encrypt_decrypt(key, data): # 创建 IDEA 加密器 cipher = IDEA.new(key, IDEA.MODE_ECB)
# 判断数据类型并加密/解密
if isinstance(data, Document):
for paragraph in data.paragraphs:
encrypted_text = cipher.encrypt(paragraph.text.encode('utf-8'))
paragraph.text = encrypted_text.decode('utf-8')
return data
elif isinstance(data, openpyxl.Workbook):
for sheetname in data.sheetnames:
sheet = data[sheetname]
for row in sheet.iter_rows():
for cell in row:
encrypted_value = cipher.encrypt(str(cell.value).encode('utf-8'))
cell.value = encrypted_value.decode('utf-8')
return data
else:
raise ValueError("Unsupported data type")
def create_new_window3(): def encrypt_decrypt_file(): input_file_path = input_file_entry.get() output_file_path = output_file_entry.get() key = key_entry.get()
try:
# 读取输入文件
if input_file_path.endswith('.docx'): # Word 文档
document = Document(input_file_path)
data = document
elif input_file_path.endswith('.xlsx'): # Excel 文件
workbook = openpyxl.load_workbook(input_file_path)
data = workbook
else:
raise ValueError("Unsupported file type")
# 加密/解密数据
encrypted_data = IDEA_encrypt_decrypt(key, data)
# 写入输出文件
if isinstance(encrypted_data, Document):
output_file_path = output_file_path.rstrip('.xlsx') + '.docx'
encrypted_data.save(output_file_path)
elif isinstance(encrypted_data, openpyxl.Workbook):
output_file_path = output_file_path.rstrip('.docx') + '.xlsx'
encrypted_data.save(output_file_path)
else:
raise ValueError("Unsupported file type")
messagebox.showinfo("Success", "文件加密/解密完成")
except Exception as e:
messagebox.showerror("Error", str(e))
#选择文件函数
def on_select_input():
file_path = filedialog.askopenfilename()
if file_path:
input_file_entry.delete(0, tk.END)
input_file_entry.insert(0, file_path)
#选择路径函数
def on_select_output():
file_path = filedialog.asksaveasfilename()
if file_path:
output_file_entry.delete(0, tk.END)
output_file_entry.insert(0, file_path)
new_window = tk.Toplevel(root_new)
new_window.title('IDEA算法加密\解密窗口')
new_window.geometry('2000x1000')
# 输入文件路径
lbl_input = tk.Label(new_window, text="输入文件路径:")
lbl_input.pack()
input_file_entry = tk.Entry(new_window, width=50)
input_file_entry.pack()
btn_select_input = tk.Button(new_window, text="选择文件", command=on_select_input)
btn_select_input.pack()
# 输出文件路径
lbl_output = tk.Label(new_window, text="输出文件路径:")
lbl_output.pack()
output_file_entry = tk.Entry(new_window, width=50)
output_file_entry.pack()
btn_select_output = tk.Button(new_window, text="选择路径", command=on_select_output)
btn_select_output.pack()
# 密钥
lbl_key = tk.Label(new_window, text="密钥:")
lbl_key.pack()
key_entry = tk.Entry(new_window)
key_entry.pack()
# 加密/解密按钮
btn_encrypt_decrypt = tk.Button(new_window, text="加密/解密", command=encrypt_decrypt_file)
btn_encrypt_decrypt.pack()
root_new = tk.Tk() root_new.title("加密/解密工具") root_new.geometry("300x100")
btn_idea = tk.Button(root_new, text="IDEA算法", command=create_new_window3) btn_idea.pack(pady=20)
root_new.mainloop(
AI 优化版
原文地址:https://www.cveoy.top/t/topic/hc4o 著作权归作者所有。请勿转载和采集!