0

I want to find an alphabetical string that is anywhere between 1-4 characters long.

I start off by iterating through the list of 52 letters:

letters = string.ascii_letters

I then need to iterate through the same list for the next 3 characters of the string until I find the string I am looking for.

If each _ represents the list of 52 letters, I need to basically do this, while checking for a match at each iteration:

_
_ _
_ _ _
_ _ _ _

How would I best structure a series of loops to do this?


If the premise of the question seems confusing, this is for a problem set on brute force cracking. I have simply extracted the part of the question that I am struggling with.


Edit: This is where I've got to so far.

#we know the salt is the 2-digit '50'
#we know the key is limited to 4 alphabetical letters
#cycle through all possibilities of the key till we match the hash

letters = string.ascii_letters
lcounter = 0
i = 0
j = 0
k = 0
l = 0
tryhash = "a"
word = [letters[i]]

while(tryhash != hash):
    for c in letters:
        word = [letters[i]]  #this does not work as the additional letters need to be appended to word after the first lcounter loop
        tryword = ''.join(word)
        tryhash = crypt.crypt(tryword, "50")

        if (tryhash == hash):
            print(word)
            break

        i += 1

        if (lcounter > 0) and (i == 52):
            i = 0
            if (lcounter == 1) and (j == 0):
                word.insert(lcounter, letters[j])
            j += 1

            if (lcounter > 1) and (k == 52):
                j = 0
                if (lcounter == 2) and (k == 0):
                    word.insert(lcounter, letters[k])
                k += 1

                if (lcounter > 2) and (k == 52):
                    k = 0
                    if (lcounter == 3) and (l == 0):
                        word.insert(lcounter, letters[l])
                    l += 1

    lcounter += 1
Kyap
  • 91
  • 1
  • 9
  • Welcome to SO Kyap! While we're more than happy to help, you have to show us what you've done first. SO isn't (usually) a code writing service. – AlG Mar 08 '17 at 14:14
  • Could you include some example data or pseudocode? I'm having trouble reading your question – Patrick Haugh Mar 08 '17 at 14:15
  • See http://stackoverflow.com/questions/7074051/what-is-the-best-way-to-generate-all-possible-three-letter-strings. – kennytm Mar 08 '17 at 14:16
  • Possible duplicate of [What is the best way to generate all possible three letter strings?](http://stackoverflow.com/questions/7074051/what-is-the-best-way-to-generate-all-possible-three-letter-strings) – MB-F Mar 08 '17 at 14:18
  • Thanks for the welcome. I've added my code. As to the possible duplicate, it is very similar however the answer to that question (using the itertools.permutation) does not work very well as it has to consider 1,2 and 3 letter characters, not just permutations of 4 letters. – Kyap Mar 09 '17 at 13:21

2 Answers2

2

You can do something like this:


    import string
    import itertools

    data = string.ascii_lowecase

    for i in itertools.permutations(data, 4):

        if i == 'your_string':
            #do something
        else:
            pass

Pankhuri Agarwal
  • 764
  • 3
  • 23
  • Using the itertools.permutation does not work very well as I have to consider 1,2 and 3 letter characters, not just permutations of 4 letters. – Kyap Mar 09 '17 at 13:21
  • you can use a outerloop to maintain the string length and use itertools.permutation then. – Pankhuri Agarwal Mar 10 '17 at 10:28
0

something like this perhaps:

my_string = "some"
for letter1 in string.ascii_letters:
    if letter1 == my_string:
        print("success")
    for letter2 in string.ascii_letters:
        if letter1 + letter2 == my_string:
            print("success")
        for letter3 in string.ascii_letters:
            if letter1 + letter2 + letter3 == my_string:
                print("success")
            for letter4 in string.ascii_letters:
                if letter1 + letter2 + letter3 + letter4 == my_string
                    print("success")
The4thIceman
  • 3,709
  • 2
  • 29
  • 36
  • Perfect! So simple as well. I wish I has been able to come up with the logic for this. – Kyap Mar 09 '17 at 13:22