# algorithm for brute force pset2 hacker edition

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;
word = '\n';

for(int a =64; a < 123; a++)
{
word = a;
for(int b =64; b < 123; b++)
{
word = b;
for(int c =64; c < 123; c++)
{
word = c;
for(int d =64; d < 123; d++)
{
word = d;
for(int e =64; e < 123; e++)
{
word = e;
for(int f =64; f < 123; f++)
{
word = f;
for(int g =64; g < 123; g++)
{
word = g;
for(int h =64; h < 123; h++)
{
word = 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.

• I think this may come handy: perlmonks.org/?node_id=113490 Jan 11 '15 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
Mar 30 '15 at 1:44

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++)