Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.7k views
in Technique[技术] by (71.8m points)

python 3.x - Pass cracker how not use four loops for four letters combination

I was practising for educational purpose with simply password cracker. I know that I could use itertool but in my case when I'm learning I would miss facing problems and learning on them and indeed I've met one which is not giving me a peace.

What I want to learn is if I need get for example four combinations, so how to get in a loop first letter 'a',then another step'a' and again 'a' and 'a', to have 'aaaa' later on'abaa' etc.

So I wrote that:

import string
passe = 'zulu'
mylist = []
#letters = string.ascii_lowercase
letters = ['a','b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'r', 's', 't', 'u', 'w', 'q', 'y', 'z']
mineset= set()
for a in letters:
    for b in letters:
        for c in letters:
            for d in letters:
                s = a + b + c + d
                mylist.append(s)
mineset=set(mylist)

k = sorted(mineset)
print(k)
for i in k:
    if i == passe:
        print('got it: ', i )
print(passe in k)

It works in someway but the problems are: I had to made a set from list because combinations were repeated. And finally, I was struggling with making it in without creating four loops for four letters, something like that:

To try to solve those I went with that approach:


letters = ['a','b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'r', 's', 't', 'u', 'w', 'q', 'y', 'z']
password = 'pass'
combin = ''
lista=[]

for x in range(1,5):
    for y in letters:
        combin +=y
        lista.append(combin)
    combin=''
mineset=set(lista)
print(mineset)
for i in lista:
    if i == password:
        print('pass:', i)

But the results are disspainting: {'abc', 'a', 'ab', 'abcd', 'abcde'}

I was seating on it for a long day but can't even closely achieve similar effect to 4 loops in previous code.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

While I don't recommend this approach as a general rule, for purposes of learning here is a way to achieve what you want:

def getCombos(lts):
    rslt = []
    for l1 in lts:
        for l2 in lts:
            for l3 in lts:
                for l4 in lts:
                    s = l1+l2+l3+l4
                    rslt.append(s)
    return rslt

letters = 'abcdefghijklmnopqrstuvwxyz'
getCombos(letters)

As is illustrated by a simple example, this code is of O(n^x) in complexity where n = number of characters and x = length of the letters. This approach, quickly becomes unwieldly as the following example illustrates:

getCombos('abc")

yields 81 entries including:

['aaaa',
 'aaab',
 'aaac',
 'aaba',
 'aabb',
 'aabc',
 'aaca',
 'aacb',
 'aacc',
 'abaa',
 'abab',
 'abac',
 'abba',
 'abbb',
 'abbc',
 'abca',
 'abcb',
 'abcc',
 'acaa',
 'acab',
 'acac',
 'acba',
 'acbb',
 'acbc',
 'acca',
 'accb',
 'accc',
 'baaa',
 'baab',
 'baac',
 'baba',
 'babb',
 'babc',
 'baca',
 'bacb',
 'bacc',
 'bbaa',
 'bbab',
 'bbac',
 'bbba',
 'bbbb',
 'bbbc',
 'bbca',
 'bbcb',
 'bbcc',
 'bcaa',
 'bcab',
 'bcac',
 'bcba',
 'bcbb',
 'bcbc',
 'bcca',
 'bccb',
 'bccc',
 'caaa',
 'caab',
 'caac',
 'caba',
 'cabb',
 'cabc',
 'caca',
 'cacb',
 'cacc',
 'cbaa',
 'cbab',
 'cbac',
 'cbba',
 'cbbb',
 'cbbc',
 'cbca',
 'cbcb',
 'cbcc',
 'ccaa',
 'ccab',
 'ccac',
 'ccba',
 'ccbb',
 'ccbc',
 'ccca',
 'cccb',
 'cccc']

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...