ZIP压缩包加密和解密(利用Python破解ZIP或RAR文件密码)

  • ZIP压缩包加密和解密(利用Python破解ZIP或RAR文件密码)已关闭评论
  • 207 次浏览
  • A+
所属分类:随笔创作

背景介绍:

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

Python 代码实现如下:

importzipfileimportrarfileimporttimedefextractFiles(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 加密文件forlineinpasswdfiles.readlines():
passwd= line.strip("n")passwd=linetry:调用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两种格式结尾的压缩文件的解密,其主要思路是循环使用密码本中的每一行密码尝试进行压缩文件的解密操作,最后记录整个解密过程共花费的时长,以秒为单位。

夜行书生