python暴力破解zip文件演示原理附生成7位数的所有排列组合

"""暴力破解zip文件演示原理,请预先准备扩展名为zip的文件,不要用rar或7z文件。"""

import zipfile  
import time 
  

filename = "test.zip"                       # 待破解密码的zip文件

if filename.endswith('.zip'):               # 如果是zip文件,则加载密码字典

  fp = zipfile.ZipFile(filename)

  "加载密码字典"
  pass_list = []
  f = open("dict.txt")                      # 打开密码字典
  for line in f:
    if line.strip()!="":
      pass_list.append(line.strip())
  f.close()

  "遍历密码字典"
  for password in pass_list:
    try:
        password = str(password)
        fp.extractall(path='.', pwd=password.encode('utf-8'))  # 尝试用password解压,失败则下一个密码
        print("成功破解,它的密码是{}".format(password))
        break        
    except:
        pass 

 

下面是生成dict.txt密码字典

"""生成7位数的所有排列组合

"""

import itertools as its
words = "1234567890"              # 字符表

all_comb = []

for n in range(7):
    r = its.product(words,repeat=n)
    for i in r:
        all_comb.append("".join(i))


f = open("dict.txt",'w')         # 写入文件
for s in all_comb:
    f.write(s + "\n")
f.close()