[MTC3]Cracking SHA1-Hashed Passwords

题目地址:https://www.mysterytwisterc3.org/en/challenges/level-ii/cracking-sha1-hashed-passwords

解题关键:根据键盘上的按键分布,可以看出右边的数字键只有2486,很有可能是当做上下左右的功能,然后对剩余的按照每个按键出现1次进行暴力,发现能得出解。(刚开始学python,dfs都不知道怎么return,噗)

复杂度:$O({2^n}n!)$

由于需要在10s之内求解,而python遍历全部密钥空间需要15s,而根据遍历的姿势,我们只要选出使最外层的循环正确的字母,就可以缩短一半的时间,因此若求解时间不符合要求,将最外层的求解顺序变换一下即可。

 1 #coding:utf-8
 2 import re
 3 from Crypto.Hash import SHA
 4 import hashlib
 5 import itertools
 6 import datetime
 7 starttime = datetime.datetime.now()
 8 hash1="67ae1a64661ac8b4494666f58c4822408dd0a3e4"
 9 str1="QqWw%58(=0Ii*+nN"
10 str2=[['Q', 'q'],[ 'W', 'w'],[ '%', '5'], ['8', '('],[ '=', '0'], ['I', 'i'], ['*', '+'], ['n', 'N']]
11 def sha_encrypt(str):
12     sha = hashlib.sha1(str)
13     encrypts = sha.hexdigest()
14     return encrypts
15 st3="0"*8
16 str4=""
17 str3=list(st3)
18 for a in range(0,2):
19     str3[0]=str2[0][a]
20     for b in range(0,2):
21         str3[1]=str2[1][b]
22         for c in range(0,2):
23             str3[2]=str2[2][c]
24             for d in range(0,2):
25                 str3[3] = str2[3][d]
26                 for e in range(0,2):
27                     str3[4] = str2[4][e]
28                     for f in range(0,2):
29                         str3[5] = str2[5][f]
30                         for g in range(0,2):
31                             str3[6] = str2[6][g]
32                             for h in range(0,2):
33                                 str3[7] = str2[7][h]
34                                 newS="".join(str3)
35                                 for i in itertools.permutations(newS, 8):
36                                     str4 = sha_encrypt("".join(i))
37                                     if str4==hash1:
38                                         print "".join(i)
39                                         endtime = datetime.datetime.now()
40                                         print (endtime - starttime).seconds
41                                         exit(0)

 

posted @ 2017-10-15 04:30  Elpsywk  阅读(791)  评论(0编辑  收藏  举报