背景介绍:在工作中我们可能会遇到一些加密过的压缩文件,如果需要获取压缩文件的内容,必须先解密,然而仅凭手动去尝试肯定是不大可能的,那么我们借助 Python 来实现会极大的增加破解成功的可能性,今天小编就来聊一聊关于python3中文本加密解密?接下来我们就一起去研究一下吧!

python3中文本加密解密(之如何暴力破解加密文件)

python3中文本加密解密

背景介绍:

在工作中我们可能会遇到一些加密过的压缩文件,如果需要获取压缩文件的内容,必须先解密,然而仅凭手动去尝试肯定是不大可能的,那么我们借助 Python 来实现会极大的增加破解成功的可能性。

Python 代码实现如下:

import zipfile import rarfile import time def extractFiles(filename): #记录开始时间 start_time = time.time() #判断压缩包类型并读取压缩包 if(filename.endswith(".zip")): fp=zipfile.ZipFile(filename,'r') if(filename.endswith(".rar")): fp=rarfile.RarFile(filename,'r') #读取密码本文件 try: passwdfiles=open("passwd.txt") except: print("the file not found") #遍历密码本中的每一行密码去匹配 zip 加密文件 for line in passwdfiles.readlines(): passwd= line.strip("\n") # passwd=line try: #调用extractall来依次尝试密码 fp.extractall(path='.',pwd=passwd.encode("utf-8")) print("the %s of passwd is right"%passwd) except: print("the %s of passwd is wrong"%passwd) # 记录结束时间 end_time = time.time() print('破解压缩包花了%s秒' % (end_time - start_time)) if __name__ == '__main__': extractFiles("test.zip")

小结:

上述示例代码适合以.zip和.rar两种格式结尾的压缩文件的解密,其主要思路是循环使用密码本中的每一行密码尝试进行压缩文件的解密操作,最后记录整个解密过程共花费的时长,以秒为单位。

,