实验6需要实现的功能:
测试计划应包含以下内容。
第一, 文件打开后,显示的文件名和文件内容是否正确。
第二, 对照原始文件,测试加密文件是否正确。
第三, 对照原始文件,测试解密文件是否一致。
综合报告需要实现的功能:
设计并实现一个具有图形用户界面的文本文件加密工具。
实验基本要求如下。
(1)使用块加密算法对文件进行加密,如:DES算法。
(2)界面应具有主菜单。
(3)界面应具有工具栏。
(4)对运行时错误进行异常处理。
(5)还可以实现更多升级功能。
本篇文章主要实现的时实验6中的升级功能,需要的同学可以直接复制代码即可,把实验的运行结果修改为自己的即可,代码可以使用,结果图要使用自己的!!!
实验代码如下:
from tkinter import *
from tkinter.filedialog import askopenfilename, asksaveasfilename
import os
class FileEncryptWindow():
def __init__(self):
self.window = Tk()
frame1 = Frame(self.window)
frame1.pack()
labelFileName = Label(frame1, text="文件名:")
labelFileName.grid(row=0, column=0)
self.fileName = StringVar()
entryFileName = Entry(frame1, width=100, textvariable=self.fileName)
entryFileName.grid(row=0, column=1)
btnOpen = Button(frame1, text="打开", command=self.openFile)
btnOpen.grid(row=0, column=2)
frame2 = Frame(self.window)
frame2.pack()
labelKey = Label(frame2, text="密钥:")
labelKey.grid(row=1, column=0)
self.key = StringVar()
entryKey = Entry(frame2, textvariable=self.key)
self.key.set("3")
entryKey.grid(row=1, column=1)
btnEncrypt = Button(frame2, text="加密", command=self.encryptFile)
btnEncrypt.grid(row=1, column=2)
btnDecrypt = Button(frame2, text="解密", command=self.decryptFile)
btnDecrypt.grid(row=1, column=3)
self.text = Text(self.window)
self.text.pack()
def openFile(self):
inFileName = askopenfilename()
if inFileName:
with open(inFileName, "rb") as inFile:
try:
self.fileContext = inFile.read().decode("utf-8")
self.fileName.set(inFileName)
self.text.delete(1.0, END)
self.text.insert(END, self.fileContext)
except UnicodeDecodeError:
self.alert("无法解码文件,请确保文件是文本文件且使用了正确的字符编码。")
def encryptFile(self):
if not hasattr(self, 'fileContext'):
self.alert("请先打开文件!")
return
key = self.validateKey()
if key is None:
return
caesar = Caesar(key)
encryptStr = caesar.encrypt(self.fileContext)
outFileName = asksaveasfilename()
if outFileName:
try:
with open(outFileName, "w") as outFile:
outFile.write(encryptStr)
except PermissionError:
self.alert("无法写入文件,请检查文件是否已被其他程序打开。")
def decryptFile(self):
if not hasattr(self, 'fileContext'):
self.alert("请先打开文件!")
return
key = self.validateKey()
if key is None:
return
caesar = Caesar(key)
decryptStr = caesar.decrypt(self.fileContext)
outFileName = asksaveasfilename()
if outFileName:
try:
with open(outFileName, "w") as outFile:
outFile.write(decryptStr)
except PermissionError:
self.alert("无法写入文件,请检查文件是否已被其他程序打开。")
def validateKey(self):
try:
key = int(self.key.get())
if key <= 0:
self.alert("请输入大于零的整数作为密钥!")
return None
return key
except ValueError:
self.alert("密钥必须是整数!")
return None
def alert(self, message):
top = Toplevel(self.window)
top.title("警告")
msg = Message(top, text=message)
msg.pack()
button = Button(top, text="OK", command=top.destroy)
button.pack()
def run(self):
self.window.mainloop()
class Caesar:
def __init__(self, key):
self.key = key
self.SYMBOLS = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890 `~!@#$%^&*()_+-=[]{}|\\;:\'\",<.>?/'
def encrypt(self, clearText):
cipherText = ""
for char in clearText:
if char in self.SYMBOLS:
index = self.SYMBOLS.find(char)
cipherIndex = (index + self.key) % len(self.SYMBOLS)
cipherText += self.SYMBOLS[cipherIndex]
return cipherText
def decrypt(self, cipherText):
clearText = ""
for char in cipherText:
if char in self.SYMBOLS:
index = self.SYMBOLS.find(char)
clearIndex = (index - self.key) % len(self.SYMBOLS)
clearText += self.SYMBOLS[clearIndex]
return clearText
if __name__ == "__main__":
FileEncryptWindow().run()
实验运行结果图如下:
1.没有打开文件直接进行加密,程序不应崩溃。
2.没有打开文件直接进行解密,程序不应崩溃。
3.没有设定秘钥直接进行加密,程序不应崩溃。
4.没有设定秘钥直接进行解密,程序不应崩溃。
5.秘钥设定错误,进行加密,程序不应崩溃。
6.秘钥设定错误,进行解密,程序不应崩溃。
7.加密时保存的文件已经被 word 打开,程序不应崩溃。
8.解密时保存的文件已经被
word
打开,程序不应崩溃。

如果感觉代码很不错,请免费给个赞吧,哈哈哈哈哈哈哈哈哈