转轮机密码加密解密窗口

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()

该代码实现了以下功能:

  1. 使用转轮机密码对文件进行加密/解密操作
  2. 支持选择输入文件和输出文件路径
  3. 支持自定义密钥
  4. 可以加密/解密 Word 和 Excel 文件,并保持文件类型不变

使用说明:

  1. 运行代码
  2. 点击 "选择文件" 按钮选择要加密/解密的文件
  3. 点击 "选择路径" 按钮选择输出文件路径
  4. 在 "密钥" 输入框中输入密钥
  5. 点击 "加密/解密" 按钮进行加密/解密操作

注意:

  • 密钥必须为字母,且区分大小写
  • 加密/解密后的文件会覆盖原文件

希望本工具可以帮助您保护您的敏感数据安全。

转轮机密码加密解密工具 - 在线加密解密 Word 和 Excel 文件

原文地址: http://www.cveoy.top/t/topic/oFVQ 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录