0

I am doing PSET2 "Hacker Edition". I did used dictonary and It worked fine for cracking some passwords.I want to generate all the possible characters in incrementing order but I am unable to think the right algorithm. My code is:

char word[9]; 
word[8] = '\n';

for(int a =64; a < 123; a++)
{
    word[0] = a;
    for(int b =64; b < 123; b++)
        {
                word[1] = b; 
        for(int c =64; c < 123; c++)
            {
                    word[2] = c;
            for(int d =64; d < 123; d++)
                {
                        word[3] = d;
                for(int e =64; e < 123; e++)
                    {
                            word[4] = e;
                    for(int f =64; f < 123; f++)
                        {
                                word[5] = f;
                        for(int g =64; g < 123; g++)
                            {
                                    word[6] = g;
                            for(int h =64; h < 123; h++)
                                {
                                        word[7] = h;
                                        //do something with the word.
                                }

                            }

                        }

                    }

                }

            }

        }
}

The problem is my code only works for fixed sized password(as many as the no of loops) in this case 8. Also it seems that I am repeating same code many times thus making the code ugly and dirty. Please help me, maybe with some pseudocode.

2
  • I think this may come handy: perlmonks.org/?node_id=113490 Commented Jan 11, 2015 at 1:09
  • I came up with the same cascaded for-loop structure to generate all possible words. However, upon looking at your code, it seems that some characters are excluded. Did you do this on purpose? What if my password was "12345678" or even "Packers4"? There are other ascii characters that are acceptable to use as well.
    – user6766
    Commented Mar 30, 2015 at 1:44

2 Answers 2

2

The first optimization I'd choose is the use of breaks: this way you wouldn't have to go through all the loops before exiting with success.

A second hint that help circumvent the fixed size of the password, is the use of a nested loop:

for (length=0 to max_length_size)
    for(int a =64; a < 123; a++)
        // Perform your action
3
  • Thanks cs50s online hero. It would be great if you tell me what algorithm did you used for the same.I am still confused for how to get it done.
    – Milan
    Commented Oct 13, 2014 at 15:16
  • To be honest, I read the Pset but didn't try to solve it, because I wanteed to finish the course before.<br> The algorithm is a brute force one: you crete all the possible combinations, crypt each one using a grain of "salt" and compare them to your original hash: if the 2 hashes coincide, you found the password :-)
    – Cygni_61
    Commented Nov 1, 2014 at 10:27
  • I already did it in a simple and efficient way. anyway thanks.
    – Milan
    Commented Nov 1, 2014 at 14:23
0

Two recommendations:

1) Read about the usage DES of on the crypth library:

http://www.gnu.org/software/libc/manual/html_node/crypt.html http://www.perlmonks.org/?node_id=113490

2) Consider the principle used cypher strings on Caesar or Vigenère exercises.

Happy hacking!

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .