Hi guys , I have to do simple brute force program for my homework and I dont know what to do. It should work when I put there some password(max. 4 letters for examle: azxd ) and the program should start to trying every possible combination one at time like a, b, ...z, then aa, ab, ac, ... zz, until it finds the right one. There should be best infinite loop but i dont know how to write it in C. Can someone help me please?

If the password is maximum four letters long, you won't need an infinite loop. I think a combination of four variables can be arranged, at most, 4! times: `4! = 4*3*2*1 = 24`. (It has been several years since my last stats class, so I could be wrong. But there …

Updated code:
added `int permutationCount = 1;`, `permutationCount += 1;`, and `printf ("%d", permutationCount);`

``````    #define AsciiArraySize 52

int i,j = 0;
int permutationCount = 1; //keeps track of # of permutations
char asciiArray[AsciiArraySize];

int arrCount = 0;

// insert upper case letters
// …``````

## All 8 Replies

If the password is maximum four letters long, you won't need an infinite loop. I think a combination of four variables can be arranged, at most, 4! times: `4! = 4*3*2*1 = 24`. (It has been several years since my last stats class, so I could be wrong. But there is a formula for this variation.)

In any case, have you at least started writing some code? Please post what you have done so far, so members can offer guidance and suggestions.

Yes I started but it doesnt work yet and I don't know why.

``````#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main()
{
int i;
char broken[]="";

for(int j = 65; j < 122; ++j)
{
broken[0] = j;
for(int k = 65; k < 122; ++k)
{
broken[1] = k;
for(int l = 65; l < 122; ++l)
{
broken[2] = l;
for(int m = 65; m < 122; ++m)
{
broken[3] = m;
for(int n=65; n < 122; ++n)
{
broken[4] = n;

{
return 0;
}
}
}
}
}
}
}
``````

I believe the number of permutations are as follows (for English alphabet):

``````4-letter permutations:
26*26*26*26

3-letter permutations:
26*26*26

2-letter permutations:
26*26

1-letter permutations:
26
``````

Here is an article that explains it:
http://www.mathsisfun.com/combinatorics/combinations-permutations.html

Please look at the ascii table again. You will see the following:

``````A-Z is 65-90
a-z is 97-122

and 91-96 are other characters.
``````

However, if your password is case-sensitive, then you have, in essence, 52 letters--not 26 (26 lower case letters + 26 upper case letters)

``````4-letter permutations:
52*52*52*52

3-letter permutations:
52*52*52

2-letter permutations:
52*52

1-letter permutations:
52
``````

I would use a character array to hold the valid characters.

``````    #define AsciiArraySize 52

int i,j = 0;
char asciiArray[AsciiArraySize];

int arrCount = 0;

// insert upper case letters
// i = ascii character code
for (i=65; i <= 90; i++)
{
asciiArray[arrCount] = i;
arrCount += 1; //increment counter
}//for

// insert lower case letters
// i = ascii character code
for (i=97; i <= 122; i++)
{
asciiArray[arrCount] = i;
arrCount += 1; //increment counter
}//for

for (i=0; i < AsciiArraySize; i++)
{
printf("%c \n", asciiArray[i]);
}//for

for (i=0; i < AsciiArraySize; i++)
{
//print contents of array
for (j=0; j < AsciiArraySize; j++)
{
printf("%c%c \n", asciiArray[i],asciiArray[j]);
permutationCount += 1;
}//for
}//for
``````

You were on the right track using nested for loops. So now add the code for passwords that are 3-characters long, and then passwords that are 4-characters long.

Updated code:
added `int permutationCount = 1;`, `permutationCount += 1;`, and `printf ("%d", permutationCount);`

``````    #define AsciiArraySize 52

int i,j = 0;
int permutationCount = 1; //keeps track of # of permutations
char asciiArray[AsciiArraySize];

int arrCount = 0;

// insert upper case letters
// i = ascii character code
for (i=65; i <= 90; i++)
{
asciiArray[arrCount] = i;
arrCount += 1; //increment counter
}//for

// insert lower case letters
// i = ascii character code
for (i=97; i <= 122; i++)
{
asciiArray[arrCount] = i;
arrCount += 1; //increment counter
}//for

//permutations of length 1
for (i=0; i < AsciiArraySize; i++)
{
printf("%c \n", asciiArray[i]);
permutationCount += 1;
}//for

//permutations of length 2
for (i=0; i < AsciiArraySize; i++)
{
//print contents of array
for (j=0; j < AsciiArraySize; j++)
{
printf("%c%c \n", asciiArray[i],asciiArray[j]);
permutationCount += 1;
}//for
}//for

printf ("%d", permutationCount);
``````

Now add the code for passwords that are 3-characters long, and then passwords that are 4-characters long.

Ok got it, thank you very much for your time and for help :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.