python 试图从有限的集合中找到解压缩unzip and unrar的密码
花了很大的力气下了些资料,结果发现解压缩rar文件竟然要密码。不知道密码,找啊找,密码没找到,找到了试图猜密码的代码,copy下来试一试,发觉大海捞针试图猜密码肯本不可能,放弃了。
1.Install rarfile and unrar
在commandline中,运行:pip install selenium
在PyCharm中,点击 File -> Settings -> Project -> Python Interpreter -> + -> 输入selenium,点击install
2.有个问题,虽然安装了unrar,但是并不能解压,原因是没有unrar.exe。
3.下载winrar https://guanjia.qq.com/ab/winrar/index.html
4.copy unrar.exe到PycharmProjects\pythonProject\venv\Scripts
一:解压缩zip文件
import zipfile def extractFile(zipFile, password): ###提取文件的类 try: zipFile.extractall(pwd=bytes(password, "utf8")) ###打开压缩文件,提供密码... print("This file\'s password is " + password) ###破解到密码 return True except: pass ###假如失败,就跳过继续 def mainStep(): zipFile = zipfile.ZipFile('D:\\BaiduNetdiskDownload\\testforzip.zip') # 这里的第二个参数用r表示是读取zip文件,w是创建一个zip文件,默认是r for i in range(120, 130): # 挨个挨个的写入密码 if extractFile(zipFile, str(i)): break if __name__ == '__main__': mainStep()
二:解压缩rar文件
import zipfile, rarfile, os, shutil def extractFile(rf, password, folder_abs): ###提取文件的类 try: rf.extractall(pwd=password, path=folder_abs) ###打开压缩文件,提供密码... print("This file\'s password is " + password) ###破解到密码 return True except: pass ###假如失败,就跳过继续 def mainStep(): _rarfile = "D:\\BaiduNetdiskDownload\\abcd.rar" folder_abs = "D:\\abc" rf = rarfile.RarFile(_rarfile, mode='r') # mode的值只能为'r' with open("data\\pwd_rar.txt", 'r') as fp_pwd: for line in fp_pwd.readlines(): # 挨个挨个的写入密码 try: shutil.rmtree(folder_abs, True) except: pass if extractFile(rf, line.rstrip(os.linesep), folder_abs): break shutil.rmtree(folder_abs, True) if __name__ == '__main__': mainStep()