鱼C论坛

 找回密码
 立即注册
查看: 1504|回复: 1

[原创] 交作业了,简单的文件加密解密脚本

[复制链接]
发表于 2020-3-12 19:36:13 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
简单的文件加密解密,鱼油们多多指点!

文件加密解密.zip

2.02 KB, 下载次数: 10

文件加密解密

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2020-3-13 18:22:01 | 显示全部楼层
优化了一下~

  1. import os
  2. import easygui as g
  3. import random as r
  4. import pickle as p
  5. import sys

  6. #定义一个文件类
  7. class File:
  8.     dict = {}
  9.     def __init__(self,path):
  10.         self.dir = os.path.dirname(path)
  11.         self.name = os.path.splitext(os.path.basename(path))[0]
  12.         self.ext = os.path.splitext(os.path.basename(path))[1]
  13.         self.newname = ''.join(r.sample('qwertyuiopasdfghjklzxcvbnm&^$@#%1234567890',8))#改变文件名,障眼法
  14.     def getDict(self):
  15.         return self.dict     
  16.     def setDict(self,ext):#将相关信息存入字典便于索引
  17.         self.dict = {'dir' : self.dir,'name' : self.name,'ext' : self.ext,'newname' : self.newname,'newext' : ext}
  18.     def delDict(self):
  19.         del self.dict
  20.     x = property(getDict,setDict,delDict)
  21. def en_decryption(path):#加解密函数
  22.     with open(path,'rb+') as f:
  23.         old = f.read()
  24.         new = old[::-1]      
  25.         f.seek(0)
  26.         f.write(new)#反转文件使其无法被识别,同样操作即可解密
  27. try:
  28.     with open('table.pkl','rb') as s:
  29.         filelist = list(p.load(s))
  30. except EOFError:
  31.     filelist = []
  32. if g.passwordbox('请输入密码','文件加密解密') == '12345nlx':
  33.     while True:
  34.         if g.boolbox('选择要进行的操作','文件加密',choices = ('加密','解密')):
  35.             try:
  36.                 path = g.fileopenbox('请选择文件','文件加密')
  37.                 f = File(path)
  38.                 os.chdir(f.dir)
  39.             except TypeError:
  40.                 break
  41.             f.x = g.choicebox('请选择加密文件的拓展名','文件加密',choices = ('.txt','.mp4','.DLLs','.Doc','.mp3'))
  42.             if f.x == None:#选择拓展名伪装
  43.                 break
  44.             else:
  45.                 c = '判断'
  46.                 for each in filelist:#判断文件是否已加密
  47.                     if f.x['name'] == each['newname']:
  48.                         c = g.choicebox('文件已加密,是否重新选择文件?','文件加密',choices = ('是','否'))
  49.                         break
  50.                 if c == '是':
  51.                     continue
  52.                 elif c == '否':
  53.                     break           
  54.                 elif c == '判断':
  55.                     g.msgbox('文件加密中......','文件加密')
  56.                     en_decryption(path)#加密
  57.                     os.rename(os.path.basename(path),f.x['newname'] + f.x['newext'])
  58.                     os.chdir(sys.path[0])
  59.                     filelist.append(f.dict)#将加密文件存入已加密列表
  60.                 with open('table.pkl','wb')  as s:
  61.                     p.dump(filelist,s)#存入pickle
  62.                 if g.boolbox('加密完成,是否退出程序','文件加密'):
  63.                     break
  64.         else :
  65.             if filelist == []:
  66.                 g.textbox('提示','文件解密',text = '未找到加密文件')
  67.                 if  g.boolbox('是否退出程序','文件解密'):
  68.                     break
  69.                 else:
  70.                     continue
  71.             else:
  72.                 try:
  73.                     for each in g.multchoicebox('搜索到已加密文件如下,请选择要解密的文件','文件解密',choices = filelist):
  74.                         for file in filelist:
  75.                             if each == str(file):
  76.                                 g.textbox('文件解密','文件解密',text = '文件%s解密中,请不要退出程序......'%(file['name'] + file['ext']))
  77.                                 os.chdir(file['dir'])
  78.                                 try:
  79.                                     en_decryption(file['newname'] + file['newext'])#解密
  80.                                     os.rename(file['newname'] + file['newext'],file['name'] + file['ext'])#重命名
  81.                                     
  82.                                     filelist.remove(file)#解密后将文件从已加密列表中删除
  83.                                 except FileNotFoundError:
  84.                                     g.msgbox('检测到文件路径已改变,请自行选择加密文件','文件解密')#路径检测
  85.                                     try:
  86.                                         path = g.fileopenbox('请选择文件','文件解密')
  87.                                         f = File(path)
  88.                                         for file in filelist:
  89.                                             if f.name == file['newname']:
  90.                                                 os.chdir(f.dir)
  91.                                                 g.textbox('文件解密','文件解密',text = '文件%s解密中,请不要退出程序......'%(file['name'] + file['ext']))
  92.                                                 en_decryption(file['newname'] + file['newext'])#解密
  93.                                                 os.rename(file['newname'] + file['newext'],file['name'] + file['ext'])#重命名
  94.                                                 filelist.remove(file)
  95.                                     except TypeError:
  96.                                         break
  97.                                 break
  98.                 except TypeError:
  99.                     break
  100.                 os.chdir(sys.path[0])
  101.                 with open('table.pkl','wb')  as s:
  102.                     p.dump(filelist,s)#存入pickle
  103.                 if g.boolbox('解密完成,是否退出程序','文件解密'):
  104.                     break
  105. else:
  106.     g.msgbox('密码错误!程序退出','文件加密解密')
  107. g.msgbox('谢谢使用','文件加密解密')
  108.    

  109.    
  110.    

复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-6-1 18:03

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表