根本原理正在于Python规范库zipfile以及扩大库unrar提供的解紧缩办法extractall()能够指定明码,这样的话起首(手动或用顺序)天生一个字典,而后顺次测验考试此中的明码,假如可以失常解紧缩则示意明码正确。
import os import sys #zipfile是Python规范库 import zipfile #测验考试导入扩大库unrar,假如不就暂时装置 try: from unrar import rarfile except: path = '"'+os.path.dirname(sys.executable)+'\\scripts\\pip" install --upgrade pip' os.system(path) path = '"'+os.path.dirname(sys.executable)+'\\scripts\\pip" install unrar' os.system(path) from unrar import rarfile def decryptRarZipFile(filename): #依据文件扩大名,应用没有同的库 if filename.endswith('.zip'): fp = zipfile.ZipFile(filename) elif filename.endswith('.rar'): fp = rarfile.RarFile(filename) #解紧缩的指标文件夹 desPath = filename[:-4] if not os.path.exists(desPath): os.mkdir(desPath) #先测验考试不必明码解紧缩,假如胜利则示意紧缩文件不明码 try: fp.extractall(desPath) fp.close() print('No password') return #应用明码字典进行暴力破解 except: try: fpPwd = open('pwddict.txt') except: print('No dict file pwddict.txt in current directory.') return for pwd in fpPwd: pwd = pwd.rstrip() try: if filename.endswith('.zip'): for file in fp.namelist(): #对zip文件需求从新编码再解码,防止中文乱码 fp.extract(file, path=desPath, pwd=pwd.encode()) os.rename(desPath+'\\'+file, desPath+'\\'+file.encode('cp437').decode('gbk')) print('Success! ====>'+pwd) fp.close() break elif filename.endswith('.rar'): fp.extractall(path=desPath, pwd=pwd) print('Success! ====>'+pwd) fp.close() break except: pass fpPwd.close() if __name__ == '__main__': filename = sys.argv[1] if os.path.isfile(filename) and filename.endswith(('.zip', '.rar')): decryptRarZipFile(filename) else: print('Must be Rar or Zip file')
更多Python相干技巧文章,请拜访Python教程栏目进行学习!
以上就是python怎样破解紧缩包明码的具体内容,更多请存眷资源魔其它相干文章!
标签: python教程 python编程 python使用问题 python破解压缩包密码
版权声明:除非特别标注,否则均为本站原创文章,转载时请以链接形式注明文章出处。
抱歉,评论功能暂时关闭!