|
楼主 |
发表于 2020-3-13 18:22:01
|
显示全部楼层
优化了一下~
- import os
- import easygui as g
- import random as r
- import pickle as p
- import sys
- #定义一个文件类
- class File:
- dict = {}
- def __init__(self,path):
- self.dir = os.path.dirname(path)
- self.name = os.path.splitext(os.path.basename(path))[0]
- self.ext = os.path.splitext(os.path.basename(path))[1]
- self.newname = ''.join(r.sample('qwertyuiopasdfghjklzxcvbnm&^$@#%1234567890',8))#改变文件名,障眼法
- def getDict(self):
- return self.dict
- def setDict(self,ext):#将相关信息存入字典便于索引
- self.dict = {'dir' : self.dir,'name' : self.name,'ext' : self.ext,'newname' : self.newname,'newext' : ext}
- def delDict(self):
- del self.dict
- x = property(getDict,setDict,delDict)
- def en_decryption(path):#加解密函数
- with open(path,'rb+') as f:
- old = f.read()
- new = old[::-1]
- f.seek(0)
- f.write(new)#反转文件使其无法被识别,同样操作即可解密
- try:
- with open('table.pkl','rb') as s:
- filelist = list(p.load(s))
- except EOFError:
- filelist = []
- if g.passwordbox('请输入密码','文件加密解密') == '12345nlx':
- while True:
- if g.boolbox('选择要进行的操作','文件加密',choices = ('加密','解密')):
- try:
- path = g.fileopenbox('请选择文件','文件加密')
- f = File(path)
- os.chdir(f.dir)
- except TypeError:
- break
- f.x = g.choicebox('请选择加密文件的拓展名','文件加密',choices = ('.txt','.mp4','.DLLs','.Doc','.mp3'))
- if f.x == None:#选择拓展名伪装
- break
- else:
- c = '判断'
- for each in filelist:#判断文件是否已加密
- if f.x['name'] == each['newname']:
- c = g.choicebox('文件已加密,是否重新选择文件?','文件加密',choices = ('是','否'))
- break
- if c == '是':
- continue
- elif c == '否':
- break
- elif c == '判断':
- g.msgbox('文件加密中......','文件加密')
- en_decryption(path)#加密
- os.rename(os.path.basename(path),f.x['newname'] + f.x['newext'])
- os.chdir(sys.path[0])
- filelist.append(f.dict)#将加密文件存入已加密列表
- with open('table.pkl','wb') as s:
- p.dump(filelist,s)#存入pickle
- if g.boolbox('加密完成,是否退出程序','文件加密'):
- break
- else :
- if filelist == []:
- g.textbox('提示','文件解密',text = '未找到加密文件')
- if g.boolbox('是否退出程序','文件解密'):
- break
- else:
- continue
- else:
- try:
- for each in g.multchoicebox('搜索到已加密文件如下,请选择要解密的文件','文件解密',choices = filelist):
- for file in filelist:
- if each == str(file):
- g.textbox('文件解密','文件解密',text = '文件%s解密中,请不要退出程序......'%(file['name'] + file['ext']))
- os.chdir(file['dir'])
- try:
- en_decryption(file['newname'] + file['newext'])#解密
- os.rename(file['newname'] + file['newext'],file['name'] + file['ext'])#重命名
-
- filelist.remove(file)#解密后将文件从已加密列表中删除
- except FileNotFoundError:
- g.msgbox('检测到文件路径已改变,请自行选择加密文件','文件解密')#路径检测
- try:
- path = g.fileopenbox('请选择文件','文件解密')
- f = File(path)
- for file in filelist:
- if f.name == file['newname']:
- os.chdir(f.dir)
- g.textbox('文件解密','文件解密',text = '文件%s解密中,请不要退出程序......'%(file['name'] + file['ext']))
- en_decryption(file['newname'] + file['newext'])#解密
- os.rename(file['newname'] + file['newext'],file['name'] + file['ext'])#重命名
- filelist.remove(file)
- except TypeError:
- break
- break
- except TypeError:
- break
- os.chdir(sys.path[0])
- with open('table.pkl','wb') as s:
- p.dump(filelist,s)#存入pickle
- if g.boolbox('解密完成,是否退出程序','文件解密'):
- break
- else:
- g.msgbox('密码错误!程序退出','文件加密解密')
- g.msgbox('谢谢使用','文件加密解密')
-
-
-
复制代码 |
|