设计一个具有图形用户界面的文件加密和解密程序。
(1)设计一个加密类,使用 Caesar 算法,实现文本文件的加解密功能。
(2)设计一个图形化用户界面的 Python GUI 程序,实现文件的读取、写入。并调用设计的类,对文件内容进行加密解密。
(3)测试程序,验证程序功能。运行程序,例如打开一个文本文件 A,对该文件进行加密生成文件 B,再对文件 B 进行 解密生成文件 C,对比文件 A 与文件 C 是否相同,并检查文件 B 是否被加密。
(4)完善程序功能,对可能出现问题进行处理,使程序可以友好提示并正常结束。
GUI 程序,实现文件读取写入以及加密和解密
from tkinter.filedialog import askopenfilename, asksaveasfilename
from tkinter import *
from tkinter import messagebox
from tkinter import simpledialog
from Caesar import Caesar
class FileEncryption:
def __init__(self):
window = Tk()
window.title("文件加密")
menubar = Menu(window)
window.config(menu=menubar)
frame0 = Frame(window)
frame0.grid(row=1, column=1, sticky=W)
newImage = PhotoImage(file="new.gif")
openImage = PhotoImage(file="open.gif")
saveImage = PhotoImage(file="save.gif")
Button(frame0, image=newImage, command=self.newFile).grid(row=1, column=1, sticky=W)
Button(frame0, image=openImage, command=self.openFile).grid(row=1, column=2)
Button(frame0, image=saveImage, command=self.saveFile).grid(row=1, column=3)
Button(frame0, text="加密", command=self.EncryptText).grid(row=1, column=4)
Button(frame0, text="解密", command=self.DecryptText).grid(row=1, column=5)
frame1 = Frame(window)
frame1.grid(row=2, column=1, sticky=W)
lblFile = Label(frame1, text="文件名").pack(side=LEFT)
self.filename = StringVar()
self.filename.set('')
enfile = Entry(frame1, textvariable=self.filename).pack(side=LEFT)
frame2 = Frame(window)
frame2.grid(row=3, column=1)
scrollbar = Scrollbar(frame2)
scrollbar.pack(side="right", fill=Y)
self.text = Text(frame2, width=40, height=20,wrap=WORD, yscrollcommand=scrollbar.set)
self.text.pack()
scrollbar.config(command=self.text.yview)
window.mainloop()
def newFile(self):
self.text.delete(1.0, END)
self.filename.set('')
def openFile(self):
self.text.delete(1.0, END)
filename = askopenfilename()
if filename != '':
infile = open(filename, "r", encoding=" utf-8")
try:
for line in infile:
self.text.insert(END, line)
except Exception as e:
messagebox.showerror("错误", f"读取文件时出错: {e}")
else:
infile.close()
self.filename.set(filename)
def saveFile(self):
filename = asksaveasfilename()
try:
if filename == '':
raise ValueError
outfile = open(filename, 'w', encoding="utf-8")
outfile.write(self.text.get(1.0, END))
except FileNotFoundError:
messagebox.showerror("错误", "文件不存在或已被删除")
except Exception:
messagebox.showinfo("提示", "你取消了对话框")
else:
messagebox.showinfo("提示", "保存成功!")
outfile.close()
self.filename.set(filename)
def EncryptText(self):
key = simpledialog.askstring("输入密钥", "请输入Caesar密钥!")
try:
if not key:
raise ValueError("密钥不能为空!")
key = int(key)
except ValueError as e:
messagebox.showerror("错误", f"输入的密钥无效: {e}")
else:
c = Caesar(key)
cipertext = c.getEncryptedText(self.text.get(1.0, END))
self.text.delete(1.0, END)
self.text.insert(END, cipertext)
messagebox.showinfo("提示", "加密完成")
def DecryptText(self):
key = simpledialog.askstring("输入密钥", "请输入Caesar密钥!")
try:
if not key:
raise ValueError("密钥不能为空!")
key = int(key)
except ValueError as e:
messagebox.showerror("错误", f"输入的密钥无效: {e}")
else:
c = Caesar(key)
text = c.getDecryptedText(self.text.get(1.0, END))
self.text.delete(1.0, END)
self.text.insert(END, text)
messagebox.showinfo("提示", "解密完成")
FileEncryption()
Caesar 加密
class Caesar:
def __init__(self,key = 3):
self.__key = key
self.__code="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrst"\
"uvwxyz 1234567890~!@#$%^&*()_+=[]{}|\\::\'\"<>,.?/"
def getDecryptedText(self,ciperText):
clearText = ""
j = 0
for i in ciperText:
if i in self.__code:
j = (self.__code.find(i)-self.__key)%len(self.__code)
clearText += self.__code[j]
else:
clearText += i
return clearText
def getEncryptedText(self,clearText):
ciperText = ""
j = 0
for i in clearText:
if i in self.__code:
j = (self.__code.find(i)+self.__key)%len(self.__code)
ciperText += self.__code[j]
else:
ciperText += i
return ciperText
def getKey(self):
return self.__key
def setKey(self,key):
self.__key = key
def getCode(self):
return self.__code
def setCode(self,code):
self.__code = code