字典攻击

1.首先我们这里要用到的是迭代器,itertools

import  itertools 
  1. 分析:这里将会提到哈希值,用字典写一个新的模式文件,确定密码,生成字典与哈希密码,然后遍历字典(就类似与整个字典循环一遍),生成字典里每一个值得哈希,再判断哈希密码与字典哈希的值是否相同,若相同则输出字典值。最终破解哈希值,知道密码。
import  itertools  
import  datetime
import  hashlib
import  time
def generatelibary(library,length=8):
    librarys = itertools.product(library, repeat=length)
    dic = open("paswordlirbarys.txt", "w", encoding='utf-8')
    for i in librarys:
        dic.writelines(i)
        dic.writelines("\n")
    dic.close()
#x = hashlib.md5("shin6x".encode(encoding="utf-8")).hexdigest()
#print(x)
def dict_attack(path,password):
    file = open(path)
    for passwords in file:
        passwords = passwords.split("\n")[0]
        if password == hashlib.md5(passwords.encode(encoding="utf-8")).hexdigest():
            print("你的密码是:{}".format(passwords))
if __name__ == "__main__":
    word = "shuaixinyu615"
    starttime = datetime.datetime.now()
    print(time.strftime("%Y%m%d%H%M%S", time.localtime(time.time())))
    generatelibary(word, length=6)
    dict_attack("paswordlirbarys.txt", "9c767d479da733c4d9e3ee4753f88995")
    endtime = datetime.datetime.now()
    print(time.strftime("%Y%m%d%H%M%S", time.localtime(time.time())))
    print('The time cost:')
    print(endtime-starttime)

3.总结:这里我们是并不知道密码的,所以这串代码的作用就是暴力破解,注意:密码越长,花费的时间就越长。
4.实验结果:

20210103220755
你的密码是:shin6x
你的密码是:shin6x
20210103220806
The time cost:
0:00:10.902610

Process finished with exit code 0

你可能感兴趣的:(python学习中的理解,python)