教你用python实现一个加密的文字处理器
这是一个类似于记事本的文字处理器。与正常的记事本不同的是,它会将文本文档进行加密,确保无法被常规的程序打开。
由于本人是一位业余编程爱好者,对于“python之禅”之类的规则比较不以为然,因此本程序代码也许有些许凌乱(当然不利于后期修改)。
这篇文章我早已发布过,但当时只给出了代码,并加了一些注释。现在,我希望在这里详细解释这个程序。我会分一个月或更久更新这篇文章,请读者耐心等待。
首先,对于一个适合我们广大中华儿女使用的程序,我们不可避免地要使用中文。这就需要对编码进行声明:
# coding:utf-8
然后,就到了模块导入的环节了。我们这里需要tkinter,windnd,os和sys。windnd可以用pip安装,tkinter则需要在安装python时勾选:
from tkinter import * from tkinter.filedialog import * from tkinter.messagebox import * import windnd import os import sys
接下来,我们会看到这样一段代码:
try: filename = sys.argv[1] except: filename = ""
这是干什么的呢?我们知道,对于exe程序,有一个“Open with”功能,也就是可以将拖动到exe文件或其快捷方式的文件打开。事实上,py文件也有这个功能,但是多数情况下这样操作后只能使程序正常运行,而不能对文件进行任何操作。而“sys.argv[1]”的作用就是,读取这个文件的路径信息。这样,我们就可以用open或sys库里的一些指令进行对文件的操作了。
def encryption(c, d): c = list(c + d) g=list(d) d=0 for i in g: d*=ord(i) d=round(abs(d)**0.5) f="0x" for i in c: e=str(ord(i)+d) d=round(300*(d**0.5)) f=f+e+"a" f=eval(f[:-1]) return (f) def decrypt(c,d): c=hex(int(c)) print(c) c=c[2:].split("a") z=d g = list(d) d = 0 for i in g: d *= ord(i) d = round(abs(d) ** 0.5) f="" for i in c: e = chr(int(i)-d) d = round(300 * (d ** 0.5)) f = f + e if f[-len(z):]==z: f=f[:-len(z)] return (f) else: c="bbc"+12
这一段就比较劲爆了。它定义了两个函数,一个用于加密,另外一个用于解密(顺便说一句,我英语不太好,有的函数或变量的名字可能比较古怪,请见谅)。
encryption是加密的函数(呃,这个应该叫做encrypt,我编这个程序时大脑有些短路,但既然已经这样了,也就不改了),它会将明文(输入的c)通过与密钥d有关的某些运算,得出一个十六进制数,然后将其转化为十进制。同时,为了确保解密结果唯一,将密钥一起连接在明文上,起校验作用。在这里,对于明文的每一个字符都会将加密过程中实际使用的密钥进行变动,因此基本是不可能通过字符出现频率的规律来破解的。由于密钥是字符,暴力拆解也基本不可能。
decrypt是解密,它大致就是encryption的逆操作,同时如果校验的结果有误,或是遇到其他解密失败的情况它会产生错误(因此使用时需要try-except,来确保程序不会退出,同时对解密失败的情况进行处理)。
接下来是基本的文件操作部分:
def mynew(aaa=1): global top, filename, textPad top.title("无标题 - 加密文本编辑器") filename = None textPad.delete(1.0, END) textPad.insert(1.0, "") def myopen(aaa=1): global filename,kkk filename = askopenfilename() if filename == "": filename = None else: top.title(os.path.basename(filename)+" - 加密文本编辑器") format=os.path.basename(filename)[os.path.basename(filename).find(".")+1:] textPad.delete(1.0, END) try: f = open(filename, 'rb') c = f.read() ccc = str(c)[2:-1] ccc.replace("\\","") textPad.insert(1.0,decrypt(ccc,format)) kkk=1 except: f.close() try: f = open(filename, 'r', encoding="gbk") try: c = f.read() except: f.close() f = open(filename, 'r', encoding="utf-8") c = f.read() textPad.insert(1.0, c) kkk = 0 except: f.close() f = open(filename, 'rb') c = f.read() textPad.insert(1.0, c) kkk=0 f.close() def mysave(aaa=1): global filename,kkk try: msg = textPad.get(1.0, 'end')[0:-1] if kkk==0: f = open(filename, 'w', encoding="utf-8") f.write(msg) else: f = open(filename, 'wb') fffff = str(encryption(msg, format)) fffff = eval("b'{}'".format(fffff)) print(fffff) f.write(fffff) f.close() except: mysaveas() def mysaveas(aaa=1): global filename f = asksaveasfilename(initialfile="无标题.txt") filename = f format = os.path.basename(filename)[os.path.basename(filename).find(".") + 1:] msg = textPad.get(1.0, 'end')[0:-1] if not format == "py" and not format=="bat" and not format == "pyw"and not format == "cmd": fh = open(filename, 'wb') fffff = str(encryption(msg, format)) fffff = eval("b'{}'".format(fffff)) print(fffff) fh.write(fffff) else: fh = open(filename, 'w', encoding="utf-8") fh.write(msg) fh.close() top.title(os.path.basename(f)+" - 加密文本编辑器") def opened(files): global filename,kkk ff = '\n'.join((item.decode('gbk') for item in files)) filename = ff if filename == "": filename = None else: top.title(os.path.basename(filename) + " - 加密文本编辑器") format = os.path.basename(filename)[os.path.basename(filename).find(".") + 1:] textPad.delete(1.0, END) try: f = open(filename, 'rb') c = f.read() ccc = str(c)[2:-1] ccc.replace("\\", "") textPad.insert(1.0, decrypt(ccc, format)) kkk = 1 except: f.close() try: f = open(filename, 'r', encoding="gbk") try: c = f.read() except: f.close() f = open(filename, 'r', encoding="utf-8") c = f.read() textPad.insert(1.0, c) kkk = 0 except: f.close() f = open(filename, 'rb') c = f.read() textPad.insert(1.0, c) kkk = 0 f.close() def mynew(aaa=1): global top, filename, textPad top.title("无标题 - 加密文本编辑器") filename = None textPad.delete(1.0, END) textPad.insert(1.0, "")
首先说mynew。这里有一个可选参数a,这是因为通过bind激活函数会输入一个参数(这点我非常反感),为了不让程序运行出错,就添加了一个可选参数。这个其实没啥好说。global后面是所需的全局变量,然后就是设置窗口名称、文件名称,并清空输入框,很简单,很明了。
def myopen(aaa=1): global filename,kkk filename = askopenfilename() if filename == "": filename = None else: top.title(os.path.basename(filename)+" - 加密文本编辑器") format=os.path.basename(filename)[os.path.basename(filename).find(".")+1:] textPad.delete(1.0, END) try: f = open(filename, 'rb') c = f.read() ccc = str(c)[2:-1] ccc.replace("\\","") textPad.insert(1.0,decrypt(ccc,format)) kkk=1 except: f.close() try: f = open(filename, 'r', encoding="gbk") try: c = f.read() except: f.close() f = open(filename, 'r', encoding="utf-8") c = f.read() textPad.insert(1.0, c) kkk = 0 except: f.close() f = open(filename, 'rb') c = f.read() textPad.insert(1.0, c) kkk=0 f.close()
然后就是myopen。“filename=askopenfilename()”就是,弹出一个选择打开的文件的窗口,并将结果存入filename。然后我们获取文件的后缀信息,作为解密所需的密钥。接下来,我们先尝试解密文件。如果执行中有错误,也就是无法解密,则用gbk编码正常打开,仍然打开失败,则使用utf-8。当然,如果都失败了,就用rb模式,也就是字节流的形式打开。
啊,越讲越上头,超过了我预期的篇幅……好了,现在先讲到这里,等待下一次更新吧(本人初二,因此接触电脑机会不多,更新嘛,自然是比较慢的。耐心等待!耐心等待!!!)!
完整的代码:
# coding:utf-8 from tkinter import * from tkinter.filedialog import * from tkinter.messagebox import * import windnd import os import sys kkk=1 try: filename = sys.argv[1] except: filename = "" def encryption(c, d): c = list(c + d) g=list(d) d=0 for i in g: d*=ord(i) d=round(abs(d)**0.5) f="0x" for i in c: e=str(ord(i)+d) d=round(300*(d**0.5)) f=f+e+"a" f=eval(f[:-1]) return (f) def decrypt(c,d): c=hex(int(c)) print(c) c=c[2:].split("a") z=d g = list(d) d = 0 for i in g: d *= ord(i) d = round(abs(d) ** 0.5) f="" for i in c: e = chr(int(i)-d) d = round(300 * (d ** 0.5)) f = f + e if f[-len(z):]==z: f=f[:-len(z)] return (f) else: c="bbc"+12 def mynew(aaa=1): global top, filename, textPad top.title("无标题 - 加密文本编辑器") filename = None textPad.delete(1.0, END) textPad.insert(1.0, "") def myopen(aaa=1): global filename,kkk filename = askopenfilename() if filename == "": filename = None else: top.title(os.path.basename(filename)+" - 加密文本编辑器") format=os.path.basename(filename)[os.path.basename(filename).find(".")+1:] textPad.delete(1.0, END) try: f = open(filename, 'rb') c = f.read() ccc = str(c)[2:-1] ccc.replace("\\","") textPad.insert(1.0,decrypt(ccc,format)) kkk=1 except: f.close() try: f = open(filename, 'r', encoding="gbk") try: c = f.read() except: f.close() f = open(filename, 'r', encoding="utf-8") c = f.read() textPad.insert(1.0, c) kkk = 0 except: f.close() f = open(filename, 'rb') c = f.read() textPad.insert(1.0, c) kkk=0 f.close() def mysave(aaa=1): global filename,kkk try: msg = textPad.get(1.0, 'end')[0:-1] if kkk==0: f = open(filename, 'w', encoding="utf-8") f.write(msg) else: f = open(filename, 'wb') fffff = str(encryption(msg, format)) fffff = eval("b'{}'".format(fffff)) print(fffff) f.write(fffff) f.close() except: mysaveas() def mysaveas(aaa=1): global filename f = asksaveasfilename(initialfile="无标题.txt") filename = f format = os.path.basename(filename)[os.path.basename(filename).find(".") + 1:] msg = textPad.get(1.0, 'end')[0:-1] if not format == "py" and not format=="bat" and not format == "pyw"and not format == "cmd": fh = open(filename, 'wb') fffff = str(encryption(msg, format)) fffff = eval("b'{}'".format(fffff)) print(fffff) fh.write(fffff) else: fh = open(filename, 'w', encoding="utf-8") fh.write(msg) fh.close() top.title(os.path.basename(f)+" - 加密文本编辑器") def opened(files): global filename,kkk ff = '\n'.join((item.decode('gbk') for item in files)) filename = ff if filename == "": filename = None else: top.title(os.path.basename(filename) + " - 加密文本编辑器") format = os.path.basename(filename)[os.path.basename(filename).find(".") + 1:] textPad.delete(1.0, END) try: f = open(filename, 'rb') c = f.read() ccc = str(c)[2:-1] ccc.replace("\\", "") textPad.insert(1.0, decrypt(ccc, format)) kkk = 1 except: f.close() try: f = open(filename, 'r', encoding="gbk") try: c = f.read() except: f.close() f = open(filename, 'r', encoding="utf-8") c = f.read() textPad.insert(1.0, c) kkk = 0 except: f.close() f = open(filename, 'rb') c = f.read() textPad.insert(1.0, c) kkk = 0 f.close() top = Tk() top.title("无标题 - 加密文本编辑器") top.geometry("1000x600+100+50") menubar = Menu(top) menubar.add_command(label="新建", command=mynew) menubar.add_command(label="打开", command=myopen) menubar.add_command(label="保存", command=mysave) menubar.add_command(label="另存为", command=mysaveas) menu = Menu(top, tearoff=False) menu.add_command(label="新建", accelerator="Ctrl+N", command=mynew) menu.add_command(label="打开", accelerator="Ctrl+O", command=myopen) menu.add_command(label="保存", accelerator="Ctrl+S", command=mysave) menu.add_command(label="另存为", accelerator="Ctrl+Shift+S", command=mysaveas) def command(event): menu.post(event.x_root, event.y_root) top['menu'] = menubar all=Frame(top) all.pack(expand=YES, fill=BOTH) textPad = Text(all,font=('宋体', 14), undo=True) if filename == "": filename = None textPad.insert(1.0, "") else: top.title(os.path.basename(filename) + " - 加密文本编辑器") format = os.path.basename(filename)[os.path.basename(filename).find(".") + 1:] textPad.delete(1.0, END) try: f = open(filename, 'rb') c = f.read() ccc = str(c)[2:-1] ccc.replace("\\", "") textPad.insert(1.0, decrypt(ccc, format)) kkk = 1 except: f.close() try: f = open(filename, 'r', encoding="gbk") try: c = f.read() except: f.close() f = open(filename, 'r', encoding="utf-8") c = f.read() textPad.insert(1.0, c) kkk = 0 except: f.close() f = open(filename, 'rb') c = f.read() textPad.insert(1.0, c) kkk = 0 f.close() scroll = Scrollbar(all) textPad.config(yscrollcommand=scroll.set) scroll.config(command=textPad.yview) scroll.pack(side=RIGHT, fill=Y) textPad.pack(expand=YES,side=RIGHT,fill=BOTH) top.bind("<Control-N>", mynew) top.bind("<Control-n>", mynew) top.bind("<Control-O>", myopen) top.bind("<Control-o>", myopen) top.bind("<Control-S>", mysave) top.bind("<Control-s>", mysave) top.bind("<Control-Shift-S>", mysaveas) top.bind("<Control-Shift-s>", mysaveas) top.bind("<Button-3>", command) windnd.hook_dropfiles(top,func=opened) top.mainloop()
总结
到此这篇关于用python实现一个加密文字处理器的文章就介绍到这了,更多相关python加密文字处理器内容请搜索我们以前的文章或继续浏览下面的相关文章希望大家以后多多支持我们!