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 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.

Edited 2 Years Ago by DavidB

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 password[6];
   char broken[]="";
   printf("Password to break: ");
   scanf("%s",password);



   for (int i=0;i<strlen(password);i++)
    broken[i] = password + 'a';

   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;

                        if(broken == password)
                        {
                            printf("Password is %s",broken);
                            return 0;
                        }
                    }
                }
            }
        }
    }
}

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

Edited 2 Years Ago by cgeier: comment

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


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


    //passwords that are 2 characters
   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.

Edited 2 Years Ago by cgeier: code change

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.

Edited 2 Years Ago by cgeier: comment

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

This article has been dead for over six months. Start a new discussion instead.