def create_new_window1(): 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)
    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()

def encrypt_text(text, key):
    rotor1 = list(string.ascii_lowercase)
    rotor2 = list(string.ascii_lowercase)
    rotor3 = list(string.ascii_lowercase)

    for i in range(key[0]):
        rotor1.append(rotor1.pop(0))
    for i in range(key[1]):
        rotor2.append(rotor2.pop(0))
    for i in range(key[2]):
        rotor3.append(rotor3.pop(0))

    result_data = []
    for character in text:
        if character.isalpha():
            index = rotor1.index(character.lower())
            index = rotor2[index]
            index = rotor3[index]
            result_data.append(rotor1[index])
        else:
            result_data.append(character)

    return ''.join(result_data)

def decrypt_text(text, key):
    rotor1 = list(string.ascii_lowercase)
    rotor2 = list(string.ascii_lowercase)
    rotor3 = list(string.ascii_lowercase)

    for i in range(key[0]):
        rotor1.append(rotor1.pop(0))
    for i in range(key[1]):
        rotor2.append(rotor2.pop(0))
    for i in range(key[2]):
        rotor3.append(rotor3.pop(0))

    result_data = []
    for character in text:
        if character.isalpha():
            index = rotor3.index(rotor2.index(rotor1.index(character.lower())))
            result_data.append(rotor1[index])
        else:
            result_data.append(character)

    return ''.join(result_data)

def encrypt_decrypt_file(input_file_path, output_file_path, key):
    file_extension = os.path.splitext(input_file_path)[1]
    if file_extension == ".docx":
        document = Document(input_file_path)
        for paragraph in document.paragraphs:
            text = paragraph.text
            if decrypt:
                encrypted_text = decrypt_text(text, key)
            else:
                encrypted_text = encrypt_text(text, key)
            paragraph.text = encrypted_text

        document.save(output_file_path)
        return True

    elif file_extension == ".xlsx":
        workbook = load_workbook(input_file_path)
        for sheet in workbook.sheetnames:
            worksheet = workbook[sheet]
            for row in worksheet.iter_rows():
                for cell in row:
                    if cell.value is None:
                        continue
                    if isinstance(cell.value, str):
                        if decrypt:
                            encrypted_value = decrypt_text(cell.value, key)
                        else:
                            encrypted_value = encrypt_text(cell.value, key)
                        cell.value = encrypted_value

        workbook.save(output_file_path)
        return True

    else:
        with open(input_file_path, 'r') as file:
            data = file.read()
            if decrypt:
                result_data = decrypt_text(data, key)
            else:
                result_data = encrypt_text(data, key)
        with open(output_file_path, 'w') as file:
            file.write(result_data)
        return True

return new_windo
def create_new_window1#此函数可以检查输入框是否填写完整然后调用encrypt_decrypt_file函数进行加密解密操作 def on_encrypt_decrypt input_file_path = e_inputget output_file_path = e_outputget key

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

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