Python实验-字典攻击!你的密码分分钟被搞定!


  1. # 字典攻击

  2. import itertools # 迭代器

  3. import datetime

  4. import hashlib

  5. import time

  6. def generatelibary(library, length=8):

  7. libararys = itertools.product(library,repeat=length)

  8. dic = open("paswordlirbarys.txt","w",encoding='utf-8')#写模式打开文件

  9. for i in libararys:

  10. dic.writelines(i)

  11. dic.writelines("\n")

  12. dic.close()

  13. #x = hashlib.md5("123".encode(encoding="utf-8")).hexdigest()#hash算法存储密码

  14. #202cb962ac59075b964b07152d234b70

  15. #print(x)

  16. def dict_attack(path,password):

  17. file = open(path)

  18. for passwords in file:

  19. #print(passwords)

  20. passwords = passwords.split("\n")[0]

  21. if password == hashlib.md5(passwords.encode(encoding="utf-8")).hexdigest():

  22. print("你的密码是:{}".format(passwords))

  23. if __name__ == "__main__":

  24. #lowercase = 'abcdefghijklmnopqrstuvwxyz'#字符组合

  25. #uppercase = 'ABCDEFGHIJKLMNOPQRS'

  26. #digits = '0123456789'

  27. word = "xiaowng2067"

  28. #special = """!"#$%&'( )*+,-./:;<=>?@[]^_`{|}~"""

  29. #word = lowercase + uppercase + digits + special

  30. starttime = datetime.datetime.now() # 获取当前时间

  31. print(time.strftime("%Y%m%d%H%M%S", time.localtime(time.time())))

  32. generatelibary(word,length=6) #生成8位数字字典

  33. #dict_attack("paswordlirbarys.txt","05213bc82bacf7312806baf095038402")

  34. endtime = datetime.datetime.now()

  35. print(time.strftime("%Y%m%d%H%M%S", time.localtime(time.time())))

  36. print('The time cost: ')

  37. print(endtime - starttime)#时间

 

六位密码测试效果最好,八位太长,需要时间过多

后记

近期有很多朋友通过私信咨询有关Python学习问题。为便于交流,点击蓝色自己加入讨论解答资源基地

 

你可能感兴趣的:(Python,信息安全,os,python,数据挖掘,putty)