注意:密码没有加上符号。密码位数只到8位。 有更多想法的大佬可以把完善后的代码贴在评论下共享。 
 1 import time
 2 from typing import List
 3 from tqdm import tqdm
 4 from itertools import chain
 5 from zipfile import ZipFile
 6 
 7 start = time.time()
 8 
 9 # chr(97) -> 'a' 这个变量保存了密码包含的字符集
10 '''
11 dictionaries = [chr(i) for i in
12                 chain(range(97, 123),    # a - z
13                       range(65, 91),    # A - Z
14                       range(48, 58))]    # 0 - 9
15 '''
16 
17 dictionaries = [chr(i) for i in
18 
19                 chain(
20                     range(97, 123),    # a - z
21                     range(48, 58))]    # 0 - 9
22 #dictionaries.extend(['.com', 'www.'])    # 添加自定义的字符集
23 
24 file_name = 'D:\qwe.zip'
25 
26 def all_passwd(dictionaries: List[str], maxlen: int):
27     # 返回由 dictionaries 中字符组成的所有长度为 maxlen 的字符串
28 
29     def helper(temp: list, start: int, n: int):
30         # 辅助函数,是个生成器
31         if start == n:    # 达到递归出口
32             yield ''.join(temp)
33             return
34         for t in dictionaries:
35             temp[start] = t    # 在每个位置
36             yield from helper(temp, start + 1, n)
37 
38     yield from helper([0] * maxlen, 0, maxlen)
39 
40 zfile = ZipFile(file_name, 'r')    # 很像open
41 
42 def extract(zfile: ZipFile, pwd: str) -> bool:
43     # zfile: 一个ZipFile类, pwd: 密码
44     try:
45         zfile.extractall(path='.', pwd=pwd.encode('utf-8'))    # 密码输入错误的时候会报错
46         now = time.time()                                      # 故使用 try - except 语句
47         print(f"Password is: {pwd}")                           # 将正确的密码输出到控制台
48         return True
49     except:
50         return False    
51 # 用 bool 类型的返回值告诉主程序是否破解成功 (意思就是返回 True 了以后就停止)
52 
53 lengths = [2, 3, 4 ,5 ,6 ,7 ,8 ]    # 密码长度
54 total = sum(len(dictionaries) ** k for k in lengths)    # 密码总数
55 
56 
57 for pwd in tqdm(chain.from_iterable(all_passwd(dictionaries, maxlen) for maxlen in lengths), total=total):
58     if extract(zfile, pwd):    # 记得extract函数返回的是bool类型的哦
59         break

 


安装库方法:cmd->  pip install tqdm