import random,zipfile,time,sys,threading#定义一个类,可以随机生成一个范围内的密码,并可以迭代class Dictor(): pSet = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789' #pSet = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789~!@#$%^&*()_-+=/*<>:;\'"[]{}|' def __init__(self,minlen,maxlen): if maxlen>minlen: self.__maxlen = maxlen self.__minlen = minlen else: self.__maxlen = minlen self.__minlen = maxlen def __iter__(self): return self #生成一个某个长度范围内的密码 def __next__(self): ret = '' for i in range(0,random.randrange(self.__minlen,self.__maxlen+1)): ret += random.choice(Dictor.pSet) return ret#定义成功标志位success = False#定义解压函数def extract_file(file, pwd): global success zfile = zipfile.ZipFile(file) try: zfile.extractall(path=r'C:\Users\Damon\Desktop',pwd=pwd.encode('utf-8')) success = True print('当前文件的密码为%s' %pwd) sys.exit() except Exception as e: pass #print(e)#多线程破解密码def main(): time1 = time.time() index = 1 for pwd in Dictor(2,2): if not success: t = threading.Thread(target=extract_file,args=(r'C:\Users\Damon\Desktop\1.zip',pwd)) t.start() t.join() print('第%d个线程' %index) index += 1 else: time2 = time.time() print('多线程本次破解的密码的时间花费为%s' %str(time2-time1)) breakif __name__ == '__main__': main()