准备工作:
导入库:
import itertools as its
import os
from zipfile import ZipFile
passwd函数尝试给定的压缩包密码来解压:
path:文件路径
i:密码
def passwd(path,i):
type_ = os.path.splitext(path)[-1][1:]
if type_ == "zip":
with ZipFile(path,'r') as zip:
try:
zip.extractall('./create_data/文件',pwd=str(i).encode('utf-8'))
print(f"解压成功,密码是{i}")
print("解压后文件位置:./create_data/文件")
return 1
except Exception as e:
pass
创建密码:
def create_pwd(length):
# words = '1234567890qwertyuiopalskdjfhgzmxncbv' //根据words生成密码
words='1234567890'
for i in range(1,length+1): //生成密码长度为1~length
base = its.product(words,repeat=i) //根据words生成密码,密钥长度和可重复字符数目为i
for i in base: //把生成的密码发送出去
yield ''.join(i)
主程序:
if __name__ == '__main__':
path="./tp.zip"
for p in create_pwd(4):
print("输入密码为:",p)
flag = passwd(path,p)
if flag:
break