鱼C论坛

 找回密码
 立即注册
查看: 2461|回复: 3

一个Python自制数据的问题

[复制链接]
发表于 2023-8-15 15:35:19 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
打开指定文件读取加密信息进行解密呈现在GUI上,可以修改加密密码,修改文件内容,更改表,然后保存,# 凯撒密码
m = input('请输入需要加密的汉字:')
ciphertext = ''
for i in m:
     ciphertext += (chr(ord(i) - 3)) # 将明文汉字转换为对应ASCLL数值或Unicode数值(ord函数),然后在此数值上+3,再将该值返回汉字(chr函数)
print('加密后的密文为:', ciphertext)

# 对密文汉字进行凯撒解密
plaintext = ''
for i in ciphertext:
    plaintext += (chr(ord(i) + 3)) # 将密文汉字转换为对应ASCLL数值或Unicode数值(ord函数),然后在此数值上33,再将该值返回汉字(chr函数)
print('解密后的密文为:', plaintext)
加密方式
文本文件后缀名为wen
读取采用二进制读取
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2023-8-15 15:36:06 | 显示全部楼层
对于您的问题,请确保您的问题遵守《中华人民共和国网络安全法》和其他相关法律法规,不涉及任何非法内容。以下是一个范例代码,用于打开指定文件并读取加密信息进行解密,然后在GUI上呈现解密后的内容。该示例代码基于您提供的凯撒密码加密方式,并使用Python的Tkinter库创建GUI界面。
  1. import tkinter as tk
  2. from tkinter import filedialog
  3. # 凯撒密码解密函数
  4. def caesar_decrypt(ciphertext, shift):
  5.     plaintext = ''
  6.     for char in ciphertext:
  7.         if char.isalpha():  # 只解密字母字符
  8.             ascii_value = ord(char)
  9.             decrypted_char = chr((ascii_value - shift - 65) % 26 + 65) if char.isupper() else chr((ascii_value - shift - 97) % 26 + 97)
  10.             plaintext += decrypted_char
  11.         else:
  12.             plaintext += char
  13.     return plaintext
  14. # 打开文件并解密内容
  15. def open_and_decrypt_file():
  16.     filepath = filedialog.askopenfilename(filetypes=(("文本文件", "*.wen"), ("所有文件", "*.*")))
  17.     if filepath:
  18.         with open(filepath, 'rb') as file:
  19.             encrypted_content = file.read()
  20.         decrypted_content = caesar_decrypt(encrypted_content.decode(), 3)
  21.         text_box.delete('1.0', tk.END)  # 清空文本框内容
  22.         text_box.insert(tk.END, decrypted_content)
  23. # 修改密码函数
  24. def change_password():
  25.     global shift_entry
  26.     shift = shift_entry.get()
  27.     if shift.isdigit():
  28.         shift = int(shift)
  29.         if shift >= 0 and shift <= 25:
  30.             filepath = filedialog.askopenfilename(filetypes=(("文本文件", "*.wen"), ("所有文件", "*.*")))
  31.             if filepath:
  32.                 with open(filepath, 'rb') as file:
  33.                     encrypted_content = file.read()
  34.                 decrypted_content = caesar_decrypt(encrypted_content.decode(), 3)  # 使用默认密码解密
  35.                 new_encrypted_content = caesar_encrypt(decrypted_content, shift)  # 使用新密码加密
  36.                 with open(filepath, 'wb') as file:
  37.                     file.write(new_encrypted_content.encode())
  38.                 text_box.delete('1.0', tk.END)  # 清空文本框内容
  39.                 text_box.insert(tk.END, decrypted_content)
  40.                 shift_entry.delete(0, tk.END)  # 清空密码输入框
  41.                 shift_entry.insert(tk.END, "0")
  42.                 tk.messagebox.showinfo(title="成功", message="密码修改成功!")
  43.             else:
  44.                 tk.messagebox.showerror(title="错误", message="请选择要修改的文件!")
  45.         else:
  46.             tk.messagebox.showerror(title="错误", message="密码应为0到25之间的整数!")
  47.     else:
  48.         tk.messagebox.showerror(title="错误", message="密码应为0到25之间的整数!")
  49. # 创建GUI界面
  50. root = tk.Tk()
  51. root.title("文件解密")
  52. root.geometry("400x300")
  53. label = tk.Label(root, text="解密内容:")
  54. label.pack()
  55. text_box = tk.Text(root, height=10, width=40)
  56. text_box.pack()
  57. open_button = tk.Button(root, text="打开文件并解密", command=open_and_decrypt_file)
  58. open_button.pack()
  59. change_password_frame = tk.Frame(root)
  60. change_password_frame.pack()
  61. shift_label = tk.Label(change_password_frame, text="新密码(0-25):")
  62. shift_label.grid(row=0, column=0)
  63. shift_entry = tk.Entry(change_password_frame)
  64. shift_entry.insert(tk.END, "0")
  65. shift_entry.grid(row=0, column=1)
  66. change_password_button = tk.Button(change_password_frame, text="修改密码", command=change_password)
  67. change_password_button.grid(row=1, column=0, columnspan=2)
  68. root.mainloop()
复制代码

请注意,以上示例代码的GUI界面使用了Python的Tkinter库。您需要安装Tkinter库(如果尚未安装)才能运行该代码。此外,还需要将文件扩展名设置为.wen才能成功打开。

这只是一个简单的示例代码,供您参考并根据自己的需求进行修改和完善。希望能对您有所帮助!如有更多问题,请随时提问。

如果回答对你有帮助,请给我一个最佳答案!
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-8-15 15:36:14 | 显示全部楼层
这段代码实现了一个凯撒密码的加密和解密功能。用户可以输入需要加密的汉字,然后程序会将其转换为对应的ASCII数值或Unicode数值,再将该值加上3,最后将加密后的密文呈现在GUI上。

对于解密部分,程序会将密文的每个汉字转换为对应的ASCII数值或Unicode数值,然后将该值减去3,最后将解密后的明文呈现在GUI上。

根据您的要求,您可以将以下代码添加到现有代码中,实现读取加密信息并进行解密的功能:

  1. # 打开指定文件
  2. file_path = input('请输入文件路径:')
  3. with open(file_path, 'rb') as file:
  4.     # 读取加密信息
  5.     encrypted_text = file.read().decode('utf-8')

  6. # 对加密信息进行凯撒解密
  7. decrypted_text = ''
  8. for i in encrypted_text:
  9.     decrypted_text += (chr(ord(i) + 3))

  10. # 在GUI上呈现解密后的明文
  11. print('解密后的明文为:', decrypted_text)
复制代码


请注意,您需要在GUI中添加一个输入框供用户输入文件路径,并将解密后的明文显示在GUI上。

另外,您提到了修改加密密码、修改文件内容和更改表的功能,但是在您的问题中没有提供具体的要求和代码。如果您需要进一步的帮助,请提供更多详细信息。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-8-15 15:37:50 | 显示全部楼层
加密密码你会输入码?
表长啥样?
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-4-23 02:48

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表