编程语言
首页 > 编程语言> > python – Key Bruteforcing

python – Key Bruteforcing

作者:互联网

我需要找一个密码密钥.密钥长度为10个字符,密钥中字母的顺序为3,7,2,1,4,5,6,10,8,9.我也知道密钥的md5.

所以我写了

mystring = "abcdefghijklmnopqrstuvwxyz"
for letter3 in mystring:
    for letter7 in mystring[mystring.index(letter3):]:
        for letter2 in mystring[:mystring.index(letter7)]:
            for letter1 in mystring[:mystring.index(letter2)]:
                for letter4 in mystring[mystring.index(letter1):]:
                    for letter5 in mystring[mystring.index(letter4):]:
                        for letter6 in mystring[mystring.index(letter4):]:
                            for letter10 in mystring[mystring.index(leter6):]:  
                                for letter8 in mystring[:mystring.index(letter10)]:
                                    for letter9 in mystring[mystring.index(letter8):]:
                                        strr = "".join([letter1,letter2,letter3,letter4,letter5,letter6,letter7,letter8,letter9,letter10])
                                        if hashlib.md5(strr).hexdigest() == md5:
                                            print "key = " ,strr
                                            break

字符顺序:

钥匙:

|letter1|letter2|letter3|letter4|letter5|letter6|letter7|letter8|letter9|letter10|

密钥按字母顺序排序:

|letter3|letter7|letter2|letter1|letter4|letter5|letter6|letter10|letter8|letter9|

问题是时间(在外部循环中进行一次迭代需要大约6个小时).有什么建议来优化for循环的范围?

解决方法:

解决方案位于底部

你很近.你想要的是……

mystring = "abcdefghijklmnopqrstuvwxyz"
for letter1 in mystring:
    for letter2 in mystring[mystring.index(letter1):]:
        for letter3 in mystring[mystring.index(letter2):]:
            for letter4 in mystring[mystring.index(letter3):]:
                for letter5 in mystring[mystring.index(letter4):]:
                    for letter6 in mystring[mystring.index(letter5):]:
                        for letter7 in mystring[mystring.index(letter6):]:
                            for letter8 in mystring[mystring.index(leter7):]:  
                                for letter9 in mystring[mystring.index(letter8):]:
                                    for letter10 in mystring[mystring.index(letter9):]:
                                        strr = "".join([letter3,letter7,letter2,letter1,letter4,letter5,letter6,letter10,letter8,letter9])
                                        if hashlib.md5(strr).hexdigest() == md5:
                                            print "key = " ,strr
                                            break

该循环严重减少了迭代次数,因为您搜索的字符数量更少.每个循环搜索的字符少于其外循环.请注意,每个循环搜索方向相同,组织方式更加有条理.

但它也很慢,因为mystring.index()是一个缓慢的操作.我们可以通过不使用mystring.index()来加快速度…

mystring = "abcdefghijklmnopqrstuvwxyz"

for i1 in range(len(mystring)):
    for i2 in range(i1, len(mystring)):
        for i3 in range(i2, len(mystring)):
            for i4 in range(i3, len(mystring)):
                for i5 in range(i4, len(mystring)):
                    for i6 in range(i5, len(mystring)):
                        for i7 in range (i6, len(mystring)):
                            for i8 in range (i7, len(mystring)):
                                for i9 in range (i8, len(mystring)):
                                    for i10 in range (i9, len(mystring)):
                                        strr = "".join([mystring[i3], mystring[i7], mystring[i2], mystring[i1], mystring[i4], mystring[i5], mystring[i6], mystring[i10], mystring[i8], mystring[i9]])
                                        if hashlib.md5(strr).hexdigest() == md5:
                                            print "key = " ,strr
                                            break

这仍然是超级慢,只是因为它是大量的迭代.

这里的诀窍是itertools ……

mystring = "abcdefghijklmnopqrstuvwxyz"

import itertools

for L1, L2, L3, L4, L5, L6, L7, L8, L9, L10 in itertools.combinations_with_replacement(mystring, 10):
    strr = "".join([L3, L7, L2, L1, L4, L5, L6, L10, L8, L9])
    if hashlib.md5(strr).hexdigest() == 'a50e38475041f76219748ee22c4377d4':
        print ('key = {}'.format(strr))
        break

itertools.combinations_with_replacement()是如何处理这些嵌套循环的,其中A> = B> = C> =& ct在python中并且速度非常快.这是最快的解决方案.

标签:python,brute-force
来源: https://codeday.me/bug/20190609/1204816.html