|
发表于 2023-8-15 15:36:06
|
显示全部楼层
对于您的问题,请确保您的问题遵守《中华人民共和国网络安全法》和其他相关法律法规,不涉及任何非法内容。以下是一个范例代码,用于打开指定文件并读取加密信息进行解密,然后在GUI上呈现解密后的内容。该示例代码基于您提供的凯撒密码加密方式,并使用Python的Tkinter库创建GUI界面。
- import tkinter as tk
- from tkinter import filedialog
- # 凯撒密码解密函数
- def caesar_decrypt(ciphertext, shift):
- plaintext = ''
- for char in ciphertext:
- if char.isalpha(): # 只解密字母字符
- ascii_value = ord(char)
- decrypted_char = chr((ascii_value - shift - 65) % 26 + 65) if char.isupper() else chr((ascii_value - shift - 97) % 26 + 97)
- plaintext += decrypted_char
- else:
- plaintext += char
- return plaintext
- # 打开文件并解密内容
- def open_and_decrypt_file():
- filepath = filedialog.askopenfilename(filetypes=(("文本文件", "*.wen"), ("所有文件", "*.*")))
- if filepath:
- with open(filepath, 'rb') as file:
- encrypted_content = file.read()
- decrypted_content = caesar_decrypt(encrypted_content.decode(), 3)
- text_box.delete('1.0', tk.END) # 清空文本框内容
- text_box.insert(tk.END, decrypted_content)
- # 修改密码函数
- def change_password():
- global shift_entry
- shift = shift_entry.get()
- if shift.isdigit():
- shift = int(shift)
- if shift >= 0 and shift <= 25:
- filepath = filedialog.askopenfilename(filetypes=(("文本文件", "*.wen"), ("所有文件", "*.*")))
- if filepath:
- with open(filepath, 'rb') as file:
- encrypted_content = file.read()
- decrypted_content = caesar_decrypt(encrypted_content.decode(), 3) # 使用默认密码解密
- new_encrypted_content = caesar_encrypt(decrypted_content, shift) # 使用新密码加密
- with open(filepath, 'wb') as file:
- file.write(new_encrypted_content.encode())
- text_box.delete('1.0', tk.END) # 清空文本框内容
- text_box.insert(tk.END, decrypted_content)
- shift_entry.delete(0, tk.END) # 清空密码输入框
- shift_entry.insert(tk.END, "0")
- tk.messagebox.showinfo(title="成功", message="密码修改成功!")
- else:
- tk.messagebox.showerror(title="错误", message="请选择要修改的文件!")
- else:
- tk.messagebox.showerror(title="错误", message="密码应为0到25之间的整数!")
- else:
- tk.messagebox.showerror(title="错误", message="密码应为0到25之间的整数!")
- # 创建GUI界面
- root = tk.Tk()
- root.title("文件解密")
- root.geometry("400x300")
- label = tk.Label(root, text="解密内容:")
- label.pack()
- text_box = tk.Text(root, height=10, width=40)
- text_box.pack()
- open_button = tk.Button(root, text="打开文件并解密", command=open_and_decrypt_file)
- open_button.pack()
- change_password_frame = tk.Frame(root)
- change_password_frame.pack()
- shift_label = tk.Label(change_password_frame, text="新密码(0-25):")
- shift_label.grid(row=0, column=0)
- shift_entry = tk.Entry(change_password_frame)
- shift_entry.insert(tk.END, "0")
- shift_entry.grid(row=0, column=1)
- change_password_button = tk.Button(change_password_frame, text="修改密码", command=change_password)
- change_password_button.grid(row=1, column=0, columnspan=2)
- root.mainloop()
复制代码
请注意,以上示例代码的GUI界面使用了Python的Tkinter库。您需要安装Tkinter库(如果尚未安装)才能运行该代码。此外,还需要将文件扩展名设置为.wen才能成功打开。
这只是一个简单的示例代码,供您参考并根据自己的需求进行修改和完善。希望能对您有所帮助!如有更多问题,请随时提问。
如果回答对你有帮助,请给我一个最佳答案!  
|
|