def create_new_window1(): def encrypt_decrypt_file(input_file_path, output_file_path, key): try: with open(input_file_path, 'rb') as input_file, open(output_file_path, 'wb') as output_file: text = input_file.read() result = "" n = len(key) for i in range(len(text)): c = chr(text[i]) if c.isalpha(): shift = ord(key[i % n]) - ord('a') if c.islower(): result += chr((ord(c) - ord('a') + shift) % 26 + ord('a')) else: result += chr((ord(c) - ord('A') + shift) % 26 + ord('A')) else: result += c output_file.write(bytes(result, 'utf-8')) return True except Exception as e: messagebox.showerror("Error", str(e)) return False

def on_encrypt_decrypt():
    input_file_path = e_input.get()
    output_file_path = e_output.get()
    key = e_key.get()
    if not input_file_path or not output_file_path or not key:
        messagebox.showwarning("Warning", "请填写完整的输入、输出路径和密钥!")
        return
    if encrypt_decrypt_file(input_file_path, output_file_path, key):
        messagebox.showinfo("Success", "文件加密/解密成功!")

def on_select_input():
    file_path = filedialog.askopenfilename()
    if file_path:
        e_input.delete(0, tk.END)
        e_input.insert(0, file_path)

def on_select_output():
    input_file_path = e_input.get()
    if not input_file_path:
        return
    file_extension = os.path.splitext(input_file_path)[1]
    file_path = filedialog.asksaveasfilename(defaultextension=file_extension,
                                             filetypes=[("Excel文件", "*.xlsx"), ("Word文件", "*.docx"), ("所有文件", "*.*")])
    if file_path:
        e_output.delete(0, tk.END)
        e_output.insert(0, file_path)

new_window = tk.Toplevel(root_new)
new_window.title('转轮机密码加密\解密窗口')
new_window.geometry('2000x1000')

lbl_input = tk.Label(new_window, text="输入文件路径:")
lbl_input.pack()
e_input = tk.Entry(new_window, width=50)
e_input.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()
e_output = tk.Entry(new_window, width=50)
e_output.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()
e_key = tk.Entry(new_window)
e_key.pack()

btn_encrypt_decrypt = tk.Button(new_window, text="加密/解密", command=on_encrypt_decrypt)
btn_encrypt_decrypt.pack(