目录


一、前言


现在,互联网上的安全问题越来越严重。我们的文件虽然有杀毒软件保护,但光使用杀毒软件也不是完全安全的。于是,有了很多人选择了文件加解密工具,保护自己的文件。这样黑客即使拿到了文件,也无法破解掉文件的内容。


要想保护文件,使用公用的加密算法是不太安全的,所以我们要想更好地保护文件,需要制作一个加密算法。

完整代码(混淆版):

from tkinter import Tk #line:2
from tkinter .filedialog import askopenfilename #line:3
from easygui import buttonbox ,msgbox ,passwordbox #line:4
from tkinter .messagebox import showinfo ,showwarning ,showerror #line:5
from os import name ,environ #line:6
from hashlib import md5 #line:7
from random import shuffle #line:8
from base64 import (a85encode ,a85decode ,b64encode ,b64decode ,b32encode ,b32decode ,b16encode ,b16decode ,)#line:18
def shuffle_str (O0OOO0OO0OOOO0OO0 ):#line:21
OOOOO0OO0OOOO0OOO =list (O0OOO0OO0OOOO0OO0 )#line:22
shuffle (OOOOO0OO0OOOO0OOO )#line:23
return "".join (OOOOO0OO0OOOO0OOO )#line:24
def create_md5 (OO00O000000O0OO0O ):#line:27
OOOO000O0OOO00O0O =md5 (OO00O000000O0OO0O .encode ()).hexdigest ()#line:28
return OOOO000O0OOO00O0O #line:29
dx1marks ={}#line:32
dx1dict ="0123456789AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz`~@#$%^&*()_+{}|\/{}[]<>?:;\"'=+-.,! "#line:33
title ="文件通文件加解密工具"#line:36
ec ="UTF-8"#line:37
button ="确定"#line:38
userprofile =environ ["Userprofile"].replace ("\\","/")#line:39
states ='RUNNING'#line:40
hide =Tk ()#line:43
hide .withdraw ()#line:44
for i in range (0 ,len (dx1dict )):#line:47
if i <=9 :#line:48
dx1marks ["0"+str (i )]=dx1dict [i ]#line:49
else :#line:50
dx1marks [str (i )]=dx1dict [i ]#line:51
class Dx1 :#line:54
def __init__ (O0OOOO000OO00000O ):#line:56
O0OOOO000OO00000O .content ="DaoXi 1.0/2.0 Encrypt Decrypt Algorithm Library"#line:57
O0OOOO000OO00000O .interval_mark ="-"#line:58
O0OOOO000OO00000O .ec ="UTF-8"#line:59
O0OOOO000OO00000O .dx1dx1marks =dx1marks #line:60
def encrypt (O00O0000OO0O0O0OO ):#line:62
O0000000000OOO000 =a85encode (O00O0000OO0O0O0OO .content .encode (O00O0000OO0O0O0OO .ec )).decode (O00O0000OO0O0O0OO .ec )#line:63
O000O0OO0OO0O00O0 =[]#line:64
for OO00O00000OOOOO0O in O0000000000OOO000 :#line:65
O000O0OO0OO0O00O0 .append (list (dx1marks .keys ())[list (dx1marks .values ()).index (OO00O00000OOOOO0O )])#line:66
return ["OKAY",O00O0000OO0O0O0OO .interval_mark .join (O000O0OO0OO0O00O0 )]#line:67
def decrypt (OOO0O0O0OO0OO0OO0 ):#line:69
try :#line:70
OO00OOOO000O0O0O0 =OOO0O0O0OO0OO0OO0 .content .split (OOO0O0O0OO0OO0OO0 .interval_mark )#line:71
O00000OOOO000000O =[]#line:72
for O0OO0O0O00O00OOO0 in OO00OOOO000O0O0O0 :#line:73
O00000OOOO000000O .append (dx1marks [O0OO0O0O00O00OOO0 ])#line:74
OOO00OOO0O0OOOOOO ="".join (O00000OOOO000000O )#line:75
return ["OKAY",a85decode (OOO00OOO0O0OOOOOO .encode (OOO0O0O0OO0OO0OO0 .ec )).decode (OOO0O0O0OO0OO0OO0 .ec )]#line:76
except Exception as OO0O0OOOO00O0OOO0 :#line:77
return ["ERROR",OO0O0OOOO00O0OOO0 ]#line:78
def dx1encrypt (O00O000OOO000OO0O ,interval ="-"):#line:81
O0O0000OOOOO000OO =Dx1 ()#line:82
O0O0000OOOOO000OO .content =O00O000OOO000OO0O #line:83
O0O0000OOOOO000OO .interval_mark =interval #line:84
O000OOOO0OOOO0O0O =O0O0000OOOOO000OO .encrypt ()#line:85
if O000OOOO0OOOO0O0O [0 ]=="OKAY":#line:86
return O000OOOO0OOOO0O0O [1 ]#line:87
else :#line:88
return "Error! "+O000OOOO0OOOO0O0O [1 ]#line:89
def dx1decrypt (OOO0000OOOO000O0O ):#line:92
O000O0OOO0O00O0O0 =Dx1 ()#line:93
O000O0OOO0O00O0O0 .content =OOO0000OOOO000O0O #line:94
O0OO0000OO0O00OO0 =O000O0OOO0O00O0O0 .decrypt ()#line:95
if O0OO0000OO0O00OO0 [0 ]=="OKAY":#line:96
return O0OO0000OO0O00OO0 [1 ]#line:97
else :#line:98
return "Error! "+str (O0OO0000OO0O00OO0 [1 ])#line:99
class Dx2 :#line:102
def __init__ (O000O0O0000OO000O ):#line:104
O000O0O0000OO000O .content ="DaoXi 1.0/2.0 Encrypt Decrypt Algorithm Library"#line:105
O000O0O0000OO000O .interval_mark ="-"#line:106
O000O0O0000OO000O .password ="default"#line:107
O000O0O0000OO000O .__OO00OOO00O00OO000 ={}#line:108
O000O0O0000OO000O .__O00OOO00OO000000O ={}#line:109
O000O0O0000OO000O .__O00O00OOO000000OO =shuffle_str (dx1dict )#line:110
for O00OO00OOO0000O0O in range (0 ,len (O000O0O0000OO000O .__O00O00OOO000000OO )):#line:111
if O00OO00OOO0000O0O <=9 :#line:112
O000O0O0000OO000O .__OO00OOO00O00OO000 ["0"+str (O00OO00OOO0000O0O )]=O000O0O0000OO000O .__O00O00OOO000000OO [O00OO00OOO0000O0O ]#line:113
else :#line:114
O000O0O0000OO000O .__OO00OOO00O00OO000 [str (O00OO00OOO0000O0O )]=O000O0O0000OO000O .__O00O00OOO000000OO [O00OO00OOO0000O0O ]#line:115
def encrypt (O0OO00OOOO0000O0O ):#line:117
try :#line:118
O0OOOOO000O00000O =O0OO00OOOO0000O0O .content #line:119
OOOO000O0OOO00OO0 =b32encode (create_md5 (O0OO00OOOO0000O0O .password ).encode ()).decode ()#line:120
OOO0OOOO0O0OOO00O =b32encode (b64encode (a85encode (str (O0OO00OOOO0000O0O .__OO00OOO00O00OO000 ).encode ()))).decode ()#line:121
OOO00O0OOO00O0O00 =[]#line:123
for O00OO0O0OO00O0000 in O0OOOOO000O00000O :#line:124
OOO00O0OOO00O0O00 .append (list (O0OO00OOOO0000O0O .__OO00OOO00O00OO000 .keys ())[list (O0OO00OOOO0000O0O .__OO00OOO00O00OO000 .values ()).index (O00OO0O0OO00O0000 )])#line:127
OOOO0OO000O000OO0 =O0OO00OOOO0000O0O .interval_mark .join (OOO00O0OOO00O0O00 )#line:128
O0O00O0000OOOO0OO ="%s(dx2)%s(dx2)%s(dx2)"%(OOOO0OO000O000OO0 ,OOOO000O0OOO00OO0 ,OOO0OOOO0O0OOO00O )#line:130
OO00O00O0000OOOO0 =dx1encrypt (O0O00O0000OOOO0OO )#line:131
return ["OKAY",OO00O00O0000OOOO0 ]#line:132
except Exception as OO0O00O00O0OOO000 :#line:133
return ["ERROR",OO0O00O00O0OOO000 ]#line:134
def decrypt (O00O0O0O0O00O00O0 ):#line:136
try :#line:137
OOO0OO0OO00OO00OO =dx1decrypt (O00O0O0O0O00O00O0 .content ).split ("(dx2)")#line:138
OOO0OO0OO00OO00OO .pop (3 )#line:139
O00OOO000OO0000OO =b32decode (OOO0OO0OO00OO00OO [1 ]).decode ()#line:141
O00OOOO0O0000O00O =create_md5 (O00O0O0O0O00O00O0 .password )#line:142
if O00OOO000OO0000OO ==O00OOOO0O0000O00O :#line:144
O0OO0O000OO0O0OOO =a85decode (b64decode (b32decode (OOO0OO0OO00OO00OO [2 ]))).decode ()#line:145
OO0000O00OOOOOO0O =eval(O0OO0O000OO0O0OOO )#line:146
O00O0O0O0O00O00O0 .__O00OOO00OO000000O =OO0000O00OOOOOO0O #line:147
O00O000O0OO0O0OO0 =[]#line:149
OO000OO00OOOOO0O0 =OOO0OO0OO00OO00OO [0 ].split (O00O0O0O0O00O00O0 .interval_mark )#line:150
for O0OOOOOO0O00OO00O in OO000OO00OOOOO0O0 :#line:151
if OO0000O00OOOOOO0O [O0OOOOOO0O00OO00O ]:#line:152
O00O000O0OO0O0OO0 .append (OO0000O00OOOOOO0O [O0OOOOOO0O00OO00O ])#line:153
O0OO0OOO00O0O00O0 ="".join (O00O000O0OO0O0OO0 )#line:154
return ["OKAY",O0OO0OOO00O0O00O0 ]#line:155
else :#line:156
return ["ERROR","The password is incorrect"]#line:157
except Exception as O00O00O00OOO0O000 :#line:158
return ["ERROR",O00O00O00OOO0O000 ]#line:159
def dx2encrypt (OO00OOOOO0OOO00O0 ,O00O00O0O00O0O0O0 ):#line:162
O00O000O00OO00OO0 =Dx2 ()#line:163
O00O000O00OO00OO0 .content =OO00OOOOO0OOO00O0 #line:164
O00O000O00OO00OO0 .password =O00O00O0O00O0O0O0 #line:165
OO000O0O00OO0OOO0 =O00O000O00OO00OO0 .encrypt ()#line:166
if OO000O0O00OO0OOO0 [0 ]=="OKAY":#line:167
return OO000O0O00OO0OOO0 [1 ]#line:168
else :#line:169
return OO000O0O00OO0OOO0 [0 ]#line:170
def dx2decrypt (OO000O0OOO0OO000O ,OOO00OO00O0O0000O ):#line:173
OOOO00000O0OOO0O0 =Dx2 ()#line:174
OOOO00000O0OOO0O0 .content =OO000O0OOO0OO000O #line:175
OOOO00000O0OOO0O0 .password =OOO00OO00O0O0000O #line:176
OOOO0O0O0O0O00OOO =OOOO00000O0OOO0O0 .decrypt ()#line:177
if OOOO0O0O0O0O00OOO [0 ]=="OKAY":#line:178
return OOOO0O0O0O0O00OOO #line:179
else :#line:180
return OOOO0O0O0O0O00OOO #line:181
def encodef ():#line:184
global states #line:186
O0O000OOO0O0000OO =askopenfilename (title =title ,initialdir =userprofile ,filetypes =[("All Files","*.*")])#line:190
with open (O0O000OOO0O0000OO ,"rb")as O0OO0O000000OOO0O :#line:192
O0OOO0O000O000000 =O0OO0O000000OOO0O .read ()#line:193
OO000OO0OO0OO00O0 =passwordbox ('请输入加密密码(请妥善保存):',title )#line:195
OO0OO0OOOO0000O0O =dx2encrypt (b64encode (O0OOO0O000O000000 ).decode (),OO000OO0OO0OO00O0 ).encode ()#line:197
O0000O0OOO0OOO0OO =O0O000OOO0O0000OO .split ("/")#line:199
OO000OO0OO0OOOOO0 =O0000O0OOO0OOO0OO [len (O0000O0OOO0OOO0OO )-1 ]#line:200
O0O00OO0OO0000OOO ="/".join (O0000O0OOO0OOO0OO ).replace (OO000OO0OO0OOOOO0 ,"(encode)"+OO000OO0OO0OOOOO0 )#line:201
with open (O0O00OO0OO0000OOO ,"wb")as O0OOOOOOO0OOO0O00 :#line:203
O0OOOOOOO0OOO0O00 .write (OO0OO0OOOO0000O0O )#line:204
msgbox ("加密成功!",title ,button )#line:206
states ='OVER'#line:207
def decodef ():#line:210
global states #line:212
O000O0OOO0OO0OO0O =askopenfilename (title =title ,initialdir =userprofile ,filetypes =[("All Files","*.*")])#line:216
OO0OOOOO0O0OO0OOO =passwordbox ('请输入加密密码以解密文件:',title )#line:218
OOOOOO0OOOO0OOOO0 =False #line:220
with open (O000O0OOO0OO0OO0O ,"r",encoding =ec )as O0OOOOO0O0O0O000O :#line:221
O000OO0O0O0OO0OOO =dx2decrypt (O0OOOOO0O0O0O000O .read (),OO0OOOOO0O0OO0OOO )#line:222
if not O000OO0O0O0OO0OOO [1 ]=='The password is incorrect':#line:223
OO0OO0OO00O00OO00 =O000OO0O0O0OO0OOO [1 ].encode ()#line:224
OO0OO0OO00O00OO00 =b64decode (OO0OO0OO00O00OO00 )#line:225
OOOOOO0OOOO0OOOO0 =True #line:226
else :#line:227
showerror (title ,'密码有误!')#line:228
OO0OO0OO00O00OO00 ='The password is incorrect'#line:229
OOOO0OOOOO00OO0O0 =OO0OO0OO00O00OO00 #line:231
OO0O00OO000OO000O =O000O0OOO0OO0OO0O .split ("/")#line:233
O00OO00O00OO0000O =OO0O00OO000OO000O [len (OO0O00OO000OO000O )-1 ]#line:234
O0OO0OOOO000000O0 ="/".join (OO0O00OO000OO000O ).replace (O00OO00O00OO0000O ,"(decode)"+O00OO00O00OO0000O )#line:235
with open ("%s"%(O0OO0OOOO000000O0 ),"wb")as OOOO00OOO00O0OOO0 :#line:238
OOOO00OOO00O0OOO0 .write (OOOO0OOOOO00OO0O0 )#line:239
if OOOOOO0OOOO0OOOO0 ==True :#line:241
msgbox ("解密成功!",title ,button )#line:242
else :#line:243
exit ()#line:244
states ='OVER'#line:245
def help ():#line:248
O00OO000O0000OOOO ="""欢迎使用此软件,此软件可以加密或解密各种文件,以免被盗窃。
开发者:潘道熹
开发时间:2020年1月
联系邮箱:qingfengstudio@yeah.net
客服QQ:3377063617
代码语言:Python 3.6

如果您在使用中出现了问题,请联系客服解决。
"""#line:257
msgbox (O00OO000O0000OOOO ,title ,button )#line:258
try :#line:260
while states =='RUNNING':#line:261
if __name__ =="__main__"and name =="nt":#line:262
choose =buttonbox ("欢迎使用%s!\n请选择您需要的服务:"%title ,title ,("加密文件","解密文件","关于我们"))#line:263
if choose =="关于我们":#line:264
help ()#line:265
elif choose =="加密文件":#line:266
encodef ()#line:267
elif choose =="解密文件":#line:268
decodef ()#line:269
else :#line:270
exit ()#line:271
else :#line:272
showerror (title ,"请使用Windows系统运行此程序!\n请自主运行此程序,不能使用模块进行导入。")#line:273
exit ()#line:274
except Exception as e :#line:275
showwarning (title ,'错误:%s'%e )#line:276
hide .mainloop ()

二、思路

(1)加密算法的实现

这个加密算法我已经实现出来了,名字叫做​​Dx2​​​,在其之前有​​Dx1​​​,但是并不安全,​​Dx1​​设计思路如下:

①先生成一个固定的包含所有​​ASCII​​字符的字符串,后将其生成为映射字典。

②读取文件内容,使用公用的加密算法(我使用了​​ascii85​​)加密,然后根据映射字典生成映射数字的序列,实现加密。一旦黑客得到映射字典,那就可以破解文件内容了。

③创建一个新文件,将加密内容写入。

旧一代的加密算法如下(使用的是模块形式):

from base64 import a85encode,a85decode

marks = {}
string = '0123456789AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz`~@#$%^&*()_+{}|\/{}[]<>?:;"\'=+-.,!'

for i in range(0,len(string)):
if i <= 9:
marks['0' + str(i)] = string[i]
else:
marks[str(i)] = string[i]

class __Daoxi():
def __init__(self):
self.content = 'Daoxi Encode'
self.interval_mark = '-'
self.ec = 'UTF-8'
self.marks = marks
def encode(self):
try:
text = a85encode(self.content.encode(self.ec)).decode(self.ec)
letters = []
for i in text:
letters.append(list(marks.keys())[list(marks.values()).index(i)])
return ['OKAY',self.interval_mark.join(letters)]
except Exception as e:
return ['ERROR',e]
def decode(self):
try:
text = self.content.split(self.interval_mark)
letters = []

for i in text:
letters.append(marks[i])
result = ''.join(letters)
return ['OKAY',a85decode(result.encode(self.ec)).decode(self.ec)]
except Exception as e:
return ['ERROR',e]

daoxi = __Daoxi()

新一代解决了这个问题,实现了“一次一密”。包含内容:


dx1(文件内容)……+md5(密码)+dx1(str(映射字典))


综合版代码如下(混淆):

from hashlib import md5 #line:2
from random import shuffle #line:3
from base64 import (a85encode ,a85decode ,b64encode ,b64decode ,b32encode ,b32decode ,b16encode ,b16decode ,)#line:13
def shuffle_str (O0OO00O00OOOOOOO0 ):#line:16
OOOOOO00O00O000O0 =list (O0OO00O00OOOOOOO0 )#line:17
shuffle (OOOOOO00O00O000O0 )#line:18
return "".join (OOOOOO00O00O000O0 )#line:19
def create_md5 (OOOOOOOOOO0OOOOO0 ):#line:22
O0OOO0OO0000OOO0O =md5 (OOOOOOOOOO0OOOOO0 .encode ()).hexdigest ()#line:23
return O0OOO0OO0000OOO0O #line:24
dx1marks ={}#line:28
dx1dict ="0123456789AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz`~@#$%^&*()_+{}|\/{}[]<>?:;\"'=+-.,!"#line:29
for i in range (0 ,len (dx1dict )):#line:31
if i <=9 :#line:32
dx1marks ["0"+str (i )]=dx1dict [i ]#line:33
else :#line:34
dx1marks [str (i )]=dx1dict [i ]#line:35
class Dx1 :#line:38
def __init__ (OO0000O0000O0O0OO ):#line:39
OO0000O0000O0O0OO .content ="DaoXi 1.0/2.0 Encrypt Decrypt Algorithm Library"#line:40
OO0000O0000O0O0OO .interval_mark ="-"#line:41
OO0000O0000O0O0OO .ec ="UTF-8"#line:42
OO0000O0000O0O0OO .dx1dx1marks =dx1marks #line:43
def encrypt (OO00O0000O000000O ):#line:45
O0OO00OO0000O0OO0 =a85encode (OO00O0000O000000O .content .encode (OO00O0000O000000O .ec )).decode (OO00O0000O000000O .ec )#line:46
O00OOOOOO0OOOOOOO =[]#line:47
for O0OOOOOO00O0OO000 in O0OO00OO0000O0OO0 :#line:48
O00OOOOOO0OOOOOOO .append (list (dx1marks .keys ())[list (dx1marks .values ()).index (O0OOOOOO00O0OO000 )])#line:49
return ["OKAY",OO00O0000O000000O .interval_mark .join (O00OOOOOO0OOOOOOO )]#line:50
def decrypt (OO0O000O0O0O0O00O ):#line:52
try :#line:53
O00OOOO00OO0000OO =OO0O000O0O0O0O00O .content .split (OO0O000O0O0O0O00O .interval_mark )#line:54
O000OO0O0000000OO =[]#line:55
for O000O0OOO00OOO000 in O00OOOO00OO0000OO :#line:57
O000OO0O0000000OO .append (dx1marks [O000O0OOO00OOO000 ])#line:58
OO00000OOO00OO0O0 ="".join (O000OO0O0000000OO )#line:59
return ["OKAY",a85decode (OO00000OOO00OO0O0 .encode (OO0O000O0O0O0O00O .ec )).decode (OO0O000O0O0O0O00O .ec )]#line:60
except Exception as O0O0000O000000OO0 :#line:61
return ["ERROR",O0O0000O000000OO0 ]#line:62
def dx1encrypt (O0O00O0O00O000O00 ,interval ="-"):#line:65
O00OOO0O0OOO000OO =Dx1 ()#line:66
O00OOO0O0OOO000OO .content =O0O00O0O00O000O00 #line:67
O00OOO0O0OOO000OO .interval_mark =interval #line:68
O00OO0OO000O0OOOO =O00OOO0O0OOO000OO .encrypt ()#line:69
if O00OO0OO000O0OOOO [0 ]=="OKAY":#line:70
return O00OO0OO000O0OOOO [1 ]#line:71
else :#line:72
return "Error! "+O00OO0OO000O0OOOO [1 ]#line:73
def dx1decrypt (OO0000OO0O00O0O00 ):#line:76
O0O00OOOOO00OOOO0 =Dx1 ()#line:77
O0O00OOOOO00OOOO0 .content =OO0000OO0O00O0O00 #line:78
O00OO000O00O0OOOO =O0O00OOOOO00OOOO0 .decrypt ()#line:79
if O00OO000O00O0OOOO [0 ]=="OKAY":#line:80
return O00OO000O00O0OOOO [1 ]#line:81
else :#line:82
return "Error! "+str (O00OO000O00O0OOOO [1 ])#line:83
class Dx2 :#line:87
def __init__ (O00O0O000O0OOO0O0 ):#line:88
O00O0O000O0OOO0O0 .content ="DaoXi 1.0/2.0 Encrypt Decrypt Algorithm Library"#line:89
O00O0O000O0OOO0O0 .interval_mark ="-"#line:90
O00O0O000O0OOO0O0 .password ="default"#line:91
O00O0O000O0OOO0O0 .__O0OOOO0OO00O0OO0O ={}#line:92
O00O0O000O0OOO0O0 .__O000OO0OO00OOOO00 ={}#line:93
O00O0O000O0OOO0O0 .__OO0O0O0O0OOO00O00 =shuffle_str (dx1dict )#line:94
for OO00OO000000O00OO in range (0 ,len (O00O0O000O0OOO0O0 .__OO0O0O0O0OOO00O00 )):#line:96
if OO00OO000000O00OO <=9 :#line:97
O00O0O000O0OOO0O0 .__O0OOOO0OO00O0OO0O ["0"+str (OO00OO000000O00OO )]=O00O0O000O0OOO0O0 .__OO0O0O0O0OOO00O00 [OO00OO000000O00OO ]#line:98
else :#line:99
O00O0O000O0OOO0O0 .__O0OOOO0OO00O0OO0O [str (OO00OO000000O00OO )]=O00O0O000O0OOO0O0 .__OO0O0O0O0OOO00O00 [OO00OO000000O00OO ]#line:100
def encrypt (OOOO000OOOOO000O0 ):#line:102
try :#line:103
OO0OOOOOO00O000O0 =OOOO000OOOOO000O0 .content #line:104
O0OO0O0000O0OO000 =b32encode (create_md5 (OOOO000OOOOO000O0 .password ).encode ()).decode ()#line:105
O00OOOOOO000O0OOO =b32encode (b64encode (a85encode (str (OOOO000OOOOO000O0 .__O0OOOO0OO00O0OO0O ).encode ()))).decode ()#line:106
O0OO0O0O0OO00000O =[]#line:108
for O0O00OO00OO0O000O in OO0OOOOOO00O000O0 :#line:109
O0OO0O0O0OO00000O .append (list (OOOO000OOOOO000O0 .__O0OOOO0OO00O0OO0O .keys ())[list (OOOO000OOOOO000O0 .__O0OOOO0OO00O0OO0O .values ()).index (O0O00OO00OO0O000O )])#line:112
O0000OOO0O0O000OO =OOOO000OOOOO000O0 .interval_mark .join (O0OO0O0O0OO00000O )#line:113
O0OO000O00OOO000O ="%s(dx2)%s(dx2)%s(dx2)"%(O0000OOO0O0O000OO ,O0OO0O0000O0OO000 ,O00OOOOOO000O0OOO )#line:115
O000O00OO00O0OOOO =dx1encrypt (O0OO000O00OOO000O )#line:116
return ["OKAY",O000O00OO00O0OOOO ]#line:117
except Exception as O0O00O00O000OO00O :#line:118
return ["ERROR",O0O00O00O000OO00O ]#line:119
def decrypt (O0OO0OO0OO0O00O00 ):#line:121
try :#line:122
OOOOO0OOO0OO00OOO =dx1decrypt (O0OO0OO0OO0O00O00 .content ).split ("(dx2)")#line:123
OOOOO0OOO0OO00OOO .pop (3 )#line:124
O0O00OOOOOOOO000O =b32decode (OOOOO0OOO0OO00OOO [1 ]).decode ()#line:126
OO0O0000O00O0OO0O =create_md5 (O0OO0OO0OO0O00O00 .password )#line:127
if O0O00OOOOOOOO000O ==OO0O0000O00O0OO0O :#line:129
O0OO000OOO00OO00O =a85decode (b64decode (b32decode (OOOOO0OOO0OO00OOO [2 ]))).decode ()#line:130
OO0000O0OO00OOOO0 =eval(O0OO000OOO00OO00O )#line:131
O0OO0OO0OO0O00O00 .__O000OO0OO00OOOO00 =OO0000O0OO00OOOO0 #line:132
O00O000OO00O0O0OO =[]#line:134
OO00OO00OOO00O00O =OOOOO0OOO0OO00OOO [0 ].split (O0OO0OO0OO0O00O00 .interval_mark )#line:135
for OO0O0OO0OO0000OOO in OO00OO00OOO00O00O :#line:136
if OO0000O0OO00OOOO0 [OO0O0OO0OO0000OOO ]:#line:137
O00O000OO00O0O0OO .append (OO0000O0OO00OOOO0 [OO0O0OO0OO0000OOO ])#line:138
O0O000OO0O0O0OOOO ="".join (O00O000OO00O0O0OO )#line:139
return ["OKAY",O0O000OO0O0O0OOOO ]#line:140
else :#line:141
return ["ERROR","The password is incorrect"]#line:142
except Exception as OOOO0OOOOOO0000OO :#line:143
return ["ERROR",OOOO0OOOOOO0000OO ]#line:144
def dx2encrypt (O00OOOO00000OO00O ,O0OO0O00OO00OOO00 ):#line:147
OO0OOOO00OO00OO00 =Dx2 ()#line:148
OO0OOOO00OO00OO00 .content =O00OOOO00000OO00O #line:149
OO0OOOO00OO00OO00 .password =O0OO0O00OO00OOO00 #line:150
O0OO0OO00O00O00OO =OO0OOOO00OO00OO00 .encrypt ()#line:151
if O0OO0OO00O00O00OO [0 ]=="OKAY":#line:152
return O0OO0OO00O00O00OO #line:153
else :#line:154
return O0OO0OO00O00O00OO #line:155
def dx2decrypt (O000O0O0OOOOOOOOO ,OO0OOOO00O0000O00 ):#line:158
O0000O0OO0000OO0O =Dx2 ()#line:159
O0000O0OO0000OO0O .content =O000O0O0OOOOOOOOO #line:160
O0000O0OO0000OO0O .password =OO0OOOO00O0000O00 #line:161
O0OO000O00O0O000O =O0000O0OO0000OO0O .decrypt ()#line:162
if O0OO000O00O0O000O [0 ]=="OKAY":#line:163
return O0OO000O00O0O000O #line:164
else :#line:165
return O0OO000O00O0O000O #line:166

(二)GUI等的实现

其实很简单,该怎么做就怎么做,请看第三部分代码实现。

三、代码实现

导入需要用到的模块:

# 导入需要用到的模块
from tkinter import Tk
from tkinter.filedialog import askopenfilename
from easygui import buttonbox, msgbox,passwordbox
from tkinter.messagebox import showinfo, showwarning, showerror
from os import name, environ
from hashlib import md5
from random import shuffle
from base64 import (
a85encode,
a85decode,
b64encode,
b64decode,
b32encode,
b32decode,
b16encode,
b16decode,
)

先创建两个功能函数:

#生成随机字符串集合
def shuffle_str(s):
str_list = list(s)
shuffle(str_list)
return "".join(str_list)

#生成md5值储存密码
def create_md5(s):
enc = md5(s.encode()).hexdigest()
return enc

声明变量及隐藏​​tkinter​​窗口:

#设置加密字典和映射字典
dx1marks = {}
dx1dict = "0123456789AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz`~@#$%^&*()_+{}|\/{}[]<>?:;\"'=+-.,! "

# 声明变量
title = "文件通文件加解密工具"
ec = "UTF-8"
button = "确定"
userprofile = environ["Userprofile"].replace("\\", "/")
states = 'RUNNING'

# 隐藏窗口
hide = Tk()
hide.withdraw()

分配到​​Dx1​​字典:

#分配到字典
for i in range(0, len(dx1dict)):
if i <= 9:
dx1marks["0" + str(i)] = dx1dict[i]
else:
dx1marks[str(i)] = dx1dict[i]

如下为​​Dx1​​算法的实现:

#dx1低级加密算法
class Dx1:
#设定属性
def __init__(self):
self.content = "DaoXi 1.0/2.0 Encrypt Decrypt Algorithm Library"
self.interval_mark = "-"
self.ec = "UTF-8"
self.dx1dx1marks = dx1marks
#加密函数
def encrypt(self):
text = a85encode(self.content.encode(self.ec)).decode(self.ec)
letters = []
for i in text:
letters.append(list(dx1marks.keys())[list(dx1marks.values()).index(i)])
return ["OKAY", self.interval_mark.join(letters)]
#解密函数
def decrypt(self):
try:
text = self.content.split(self.interval_mark)
letters = []
for i in text:
letters.append(dx1marks[i])
result = "".join(letters)
return ["OKAY", a85decode(result.encode(self.ec)).decode(self.ec)]
except Exception as e:
return ["ERROR", e]

#dx1算法加密函数
def dx1encrypt(content, interval="-"):
dx1 = Dx1()
dx1.content = content
dx1.interval_mark = interval
text = dx1.encrypt()
if text[0] == "OKAY":
return text[1]
else:
return "Error! " + text[1]

#dx1算法解密函数
def dx1decrypt(content):
dx1 = Dx1()
dx1.content = content
text = dx1.decrypt()
if text[0] == "OKAY":
return text[1]
else:
return "Error! " + str(text[1])

由于​​Dx2​​算法源代码不便公布,发布混淆版:

class Dx2 :#line:2
def __init__ (O00O000000O000O0O ):#line:4
O00O000000O000O0O .content ="DaoXi 1.0/2.0 Encrypt Decrypt Algorithm Library"#line:5
O00O000000O000O0O .interval_mark ="-"#line:6
O00O000000O000O0O .password ="default"#line:7
O00O000000O000O0O .__O00O0OOO00O00000O ={}#line:8
O00O000000O000O0O .__OO0OO0OOOO00OO0O0 ={}#line:9
O00O000000O000O0O .__O0O0O0OO00OOOO0OO =shuffle_str (dx1dict )#line:10
for OOO00O000OOO000OO in range (0 ,len (O00O000000O000O0O .__O0O0O0OO00OOOO0OO )):#line:11
if OOO00O000OOO000OO <=9 :#line:12
O00O000000O000O0O .__O00O0OOO00O00000O ["0"+str (OOO00O000OOO000OO )]=O00O000000O000O0O .__O0O0O0OO00OOOO0OO [OOO00O000OOO000OO ]#line:13
else :#line:14
O00O000000O000O0O .__O00O0OOO00O00000O [str (OOO00O000OOO000OO )]=O00O000000O000O0O .__O0O0O0OO00OOOO0OO [OOO00O000OOO000OO ]#line:15
def encrypt (O00OO0O000OOOO0O0 ):#line:17
try :#line:18
OO00O00O0O0OOO0O0 =O00OO0O000OOOO0O0 .content #line:19
O000O0O0O00O00OO0 =b32encode (create_md5 (O00OO0O000OOOO0O0 .password ).encode ()).decode ()#line:20
OO00O0OO0000O00O0 =b32encode (b64encode (a85encode (str (O00OO0O000OOOO0O0 .__O00O0OOO00O00000O ).encode ()))).decode ()#line:21
OO0OO0O0OOO000OOO =[]#line:23
for O0O0OOOO00O000000 in OO00O00O0O0OOO0O0 :#line:24
OO0OO0O0OOO000OOO .append (list (O00OO0O000OOOO0O0 .__O00O0OOO00O00000O .keys ())[list (O00OO0O000OOOO0O0 .__O00O0OOO00O00000O .values ()).index (O0O0OOOO00O000000 )])#line:27
OO0OOO0O0000O0O00 =O00OO0O000OOOO0O0 .interval_mark .join (OO0OO0O0OOO000OOO )#line:28
O00O00OO0OO0O0OOO ="%s(dx2)%s(dx2)%s(dx2)"%(OO0OOO0O0000O0O00 ,O000O0O0O00O00OO0 ,OO00O0OO0000O00O0 )#line:30
OO000O000O0O00000 =dx1encrypt (O00O00OO0OO0O0OOO )#line:31
return ["OKAY",OO000O000O0O00000 ]#line:32
except Exception as OO0OO000OOOO0O000 :#line:33
return ["ERROR",OO0OO000OOOO0O000 ]#line:34
def decrypt (OOO0O0OO0OOOO0000 ):#line:36
try :#line:37
OOO0OO00O0000O0OO =dx1decrypt (OOO0O0OO0OOOO0000 .content ).split ("(dx2)")#line:38
OOO0OO00O0000O0OO .pop (3 )#line:39
O0000O00OOO0O0O0O =b32decode (OOO0OO00O0000O0OO [1 ]).decode ()#line:41
OOO0OOOO0O00000OO =create_md5 (OOO0O0OO0OOOO0000 .password )#line:42
if O0000O00OOO0O0O0O ==OOO0OOOO0O00000OO :#line:44
O0O0O0O00O00O0O00 =a85decode (b64decode (b32decode (OOO0OO00O0000O0OO [2 ]))).decode ()#line:45
OO00OOOO000O00000 =eval(O0O0O0O00O00O0O00 )#line:46
OOO0O0OO0OOOO0000 .__OO0OO0OOOO00OO0O0 =OO00OOOO000O00000 #line:47
O0OOOO00OO000O00O =[]#line:49
O0000O0000000OOO0 =OOO0OO00O0000O0OO [0 ].split (OOO0O0OO0OOOO0000 .interval_mark )#line:50
for OOOOO0OO0OOOO0O00 in O0000O0000000OOO0 :#line:51
if OO00OOOO000O00000 [OOOOO0OO0OOOO0O00 ]:#line:52
O0OOOO00OO000O00O .append (OO00OOOO000O00000 [OOOOO0OO0OOOO0O00 ])#line:53
OO0OO00OOOOOO0000 ="".join (O0OOOO00OO000O00O )#line:54
return ["OKAY",OO0OO00OOOOOO0000 ]#line:55
else :#line:56
return ["ERROR","The password is incorrect"]#line:57
except Exception as O0OOOO0O000O00OOO :#line:58
return ["ERROR",O0OOOO0O000O00OOO ]#line:59
def dx2encrypt (O000OO000O000OO0O ,OO0O00O0OO0OOOO0O ):#line:62
O0OOOO0OO0000OOO0 =Dx2 ()#line:63
O0OOOO0OO0000OOO0 .content =O000OO000O000OO0O #line:64
O0OOOO0OO0000OOO0 .password =OO0O00O0OO0OOOO0O #line:65
OOOO0OO0O000O00OO =O0OOOO0OO0000OOO0 .encrypt ()#line:66
if OOOO0OO0O000O00OO [0 ]=="OKAY":#line:67
return OOOO0OO0O000O00OO [1 ]#line:68
else :#line:69
return OOOO0OO0O000O00OO [0 ]#line:70
def dx2decrypt (OO00O0OOO0O00OOO0 ,OOOO0000O0000O0O0 ):#line:73
OOO00OO000OOO0OOO =Dx2 ()#line:74
OOO00OO000OOO0OOO .content =OO00O0OOO0O00OOO0 #line:75
OOO00OO000OOO0OOO .password =OOOO0000O0000O0O0 #line:76
OO0OOOO00O00OOO0O =OOO00OO000OOO0OOO .decrypt ()#line:77
if OO0OOOO00O00OOO0O [0 ]=="OKAY":#line:78
return OO0OOOO00O00OOO0O #line:79
else :#line:80
return OO0OOOO00O00OOO0O

加密函数、解密函数和帮助,以及最后的GUI:

# 加密函数
def encodef():
#设置状态
global states
# 加载一个选择文件的窗口
get = askopenfilename(
title=title, initialdir=userprofile, filetypes=[("All Files", "*.*")]
)
# 读取文件内容
with open(get, "rb") as f:
temp = f.read()
#请求用户输入密码
p = passwordbox('请输入加密密码(请妥善保存):',title)
# 加密文件内容
encode_temp = dx2encrypt(b64encode(temp).decode(),p).encode()
# 获取文件名称
file_name_list = get.split("/")
file_name = file_name_list[len(file_name_list) - 1]
file_path = "/".join(file_name_list).replace(file_name, "(encode)" + file_name)
# 加密文件
with open(file_path, "wb") as a:
a.write(encode_temp)
# 提示用户加密成功
msgbox("加密成功!", title, button)
states = 'OVER'

# 解密函数
def decodef():
#设置状态
global states
# 加载一个选择文件的窗口
get = askopenfilename(
title=title, initialdir=userprofile, filetypes=[("All Files", "*.*")]
)
#请求用户输入加密密码
p = passwordbox('请输入加密密码以解密文件:',title)
# 读取加密文件
passwordIsRight = False
with open(get, "r", encoding=ec) as f:
check = dx2decrypt(f.read(),p)
if not check[1] == 'The password is incorrect':
temp = check[1].encode()
temp = b64decode(temp)
passwordIsRight = True
else:
showerror(title,'密码有误!')
temp = 'The password is incorrect'
# 获取文件解密内容
decode_temp = temp
# 获取文件名称
file_name_list = get.split("/")
file_name = file_name_list[len(file_name_list) - 1]
file_path = "/".join(file_name_list).replace(file_name, "(decode)" + file_name)

# 在解密目录下添加文件
with open("%s" % (file_path), "wb") as a:
a.write(decode_temp)
# 提示用户加密成功
if passwordIsRight == True:
msgbox("解密成功!", title, button)
else:
exit()
states = 'OVER'

# 帮助函数
def help():
text = """欢迎使用此软件,此软件可以加密或解密各种文件,以免被盗窃。
开发者:潘道熹
开发时间:2020年1月
联系邮箱:qingfengstudio@yeah.net
客服QQ:3377063617
代码语言:Python 3.6

如果您在使用中出现了问题,请联系客服解决。
"""
msgbox(text, title, button)

try:
while states == 'RUNNING':
if __name__ == "__main__" and name == "nt":
choose = buttonbox("欢迎使用%s!\n请选择您需要的服务:" % title, title, ("加密文件", "解密文件", "关于我们"))
if choose == "关于我们":
help()
elif choose == "加密文件":
encodef()
elif choose == "解密文件":
decodef()
else:
exit()
else:
showerror(title, "请使用Windows系统运行此程序!\n请自主运行此程序,不能使用模块进行导入。")
exit()
except Exception as e:
showwarning(title,'错误:%s' % e)
hide.mainloop()