鱼C论坛

 找回密码
 立即注册
查看: 1712|回复: 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
读取采用二进制读取
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2023-8-15 15:36:06 | 显示全部楼层
对于您的问题,请确保您的问题遵守《中华人民共和国网络安全法》和其他相关法律法规,不涉及任何非法内容。以下是一个范例代码,用于打开指定文件并读取加密信息进行解密,然后在GUI上呈现解密后的内容。该示例代码基于您提供的凯撒密码加密方式,并使用Python的Tkinter库创建GUI界面。
import tkinter as tkfrom 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才能成功打开。

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

如果回答对你有帮助,请给我一个最佳答案!
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

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

根据您的要求,您可以将以下代码添加到现有代码中,实现读取加密信息并进行解密的功能:
# 打开指定文件
file_path = input('请输入文件路径:')
with open(file_path, 'rb') as file:
    # 读取加密信息
    encrypted_text = file.read().decode('utf-8')

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

# 在GUI上呈现解密后的明文
print('解密后的明文为:', decrypted_text)

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

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

使用道具 举报

发表于 2023-8-15 15:37:50 | 显示全部楼层
加密密码你会输入码?
表长啥样?
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-9-21 19:36

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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