之前用zipfile的时候,看到一篇帖子,使用python破解压缩zip文件的密码,虽然试了下不能用,但是有感而发,试着写了几个生成密码文件的函数,主要使用到itertools
# 生成密码数据字典并测试
import itertools
import sys
import random
# 生成6位的数字的密码字典
def generate_password_txt(filename):
f = open(filename,'w')
for id in range(1000000):
password = str(id).zfill(6)+'\n'
f.write(password)
f.close()
# 生成指定字符串以及长度的密码字典,不包含重复
def generate_password_txt2(args,pass_lenth,filename):
#args = 'abcdefg1234567'
datas = iter(args)
ppassword = itertools.permutations(datas, pass_lenth)
print(next(ppassword))
f = open(filename, 'w')
while True:
try:
password = ''.join(next(ppassword)) + '\n'
f.write(password)
except StopIteration:
sys.exit()
# 生成包含重复字母的,如aaaa,aab这样
def generate_password_txt3(args,pass_lenth,filename):
datas = iter(args)
ppassword = itertools.product(datas, repeat=pass_lenth)
f = open(filename, 'w')
while True:
try:
password = ''.join(next(ppassword)) + '\n'
f.write(password)
except StopIteration:
sys.exit()
def test(password):
# 随便指定一个密码用来测试
if password=='abcd24':
return password
def main(filename):
passFile = open(filename)
for line in passFile.readlines():
password = line.strip('\n')
print(password)
guess = test(password)
if guess:
print("=========密码是:"+password+"\n")
break
if __name__ == '__main__':
filename='password4.txt'
# _str=r'abcde12345@'
# pass_len=6
# generate_password_txt2(_str,pass_len,filename)
main(filename)
网友评论