0

I managed to get the program to output some of the passwords but not all. For some of the hashes I use as input, the program executes seemingly forever. (haven't let it go on longer than 4 mins)

Any pointers would be much appreciated.

#define _XOPEN_SOURCE
#include <unistd.h>
#include <cs50.h>
#include <string.h>
#include <stdio.h>

char* crypt(const char *key, const char *salt);
char* alphabet = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
char word[6];
char* hashed_word = NULL;

int main(int argc, char* argv[])
{
    //check password is valid
    if(argc != 2)
    {
        if(argc == 1)
        {
            printf("Error: please provide password for decyphering\n");
            return 1;
        }

        else
        {
            printf("Error: more than two arguments have been provided\n");
            return 2;
        }
    }

    //extract salt from argv[1]
    char salt[3] = {argv[1][0], argv[1][1], '\0'};



    ////////////////////////////////////////////////////////

    for(int x = 0; x < strlen(alphabet); x++)
    {
        word[0] = alphabet[x];
        //printf("%s\n", word);
        hashed_word = crypt(word, salt);

        if(strcmp(hashed_word, argv[1]) == 0)
        {
            goto PRINT;
        }
    }

    for(int x = 0; x < strlen(alphabet); x++)
    {
        word[0] = alphabet[x];
        //printf("%s\n", word);
        hashed_word = crypt(word, salt);

        if(strcmp(hashed_word, argv[1]) == 0)
        {
            goto PRINT;
        }

        for(int y = 0; y < strlen(alphabet); y++)
        {
            word[1] = alphabet[y];
            //printf("%s\n", word);
            hashed_word = crypt(word, salt);

            if(strcmp(hashed_word, argv[1]) == 0)
            {
                goto PRINT;
            }
        }
    }

    for(int x = 0; x < strlen(alphabet); x++)
    {
        word[0] = alphabet[x];
        //printf("%s\n", word);
        hashed_word = crypt(word, salt);

        if(strcmp(hashed_word, argv[1]) == 0)
        {
            goto PRINT;
        }

        for(int y = 0; y < strlen(alphabet); y++)
        {
            word[1] = alphabet[y];
            //printf("%s\n", word);
            hashed_word = crypt(word, salt);

            if(strcmp(hashed_word, argv[1]) == 0)
            {
                goto PRINT;
            }


            for(int z = 0; z < strlen(alphabet); z++)
            {
                word[2] = alphabet[z];
                //printf("%s\n", word);
                hashed_word = crypt(word, salt);

                if(strcmp(hashed_word, argv[1]) == 0)
                {
                    goto PRINT;
                }
            }
        }
    }

    for(int x = 0; x < strlen(alphabet); x++)
    {
        word[0] = alphabet[x];
        //printf("%s\n", word);
        hashed_word = crypt(word, salt);

        if(strcmp(hashed_word, argv[1]) == 0)
        {
            goto PRINT;
        }

        for(int y = 0; y < strlen(alphabet); y++)
        {
            word[1] = alphabet[y];
            //printf("%s\n", word);
            hashed_word = crypt(word, salt);

            if(strcmp(hashed_word, argv[1]) == 0)
            {
                goto PRINT;
            }


            for(int z = 0; z < strlen(alphabet); z++)
            {
                word[2] = alphabet[z];
                //printf("%s\n", word);
                hashed_word = crypt(word, salt);

                if(strcmp(hashed_word, argv[1]) == 0)
                {
                    goto PRINT;
                }

                for(int f = 0; f < strlen(alphabet); f++)
                {
                    word[3] = alphabet[f];
                    //printf("%s\n", word);
                    hashed_word = crypt(word, salt);

                    if(strcmp(hashed_word, argv[1]) == 0)
                    {
                        goto PRINT;
                    }
                }
            }
        }
    }

    for(int x = 0; x < strlen(alphabet); x++)
    {
        word[0] = alphabet[x];
        //printf("%s\n", word);
        hashed_word = crypt(word, salt);

        if(strcmp(hashed_word, argv[1]) == 0)
        {
            goto PRINT;
        }

        for(int y = 0; y < strlen(alphabet); y++)
        {
            word[1] = alphabet[y];
            //printf("%s\n", word);
            hashed_word = crypt(word, salt);

            if(strcmp(hashed_word, argv[1]) == 0)
            {
                goto PRINT;
            }


            for(int z = 0; z < strlen(alphabet); z++)
            {
                word[2] = alphabet[z];
                //printf("%s\n", word);
                hashed_word = crypt(word, salt);

                if(strcmp(hashed_word, argv[1]) == 0)
                {
                    goto PRINT;
                }

                for(int f = 0; f < strlen(alphabet); f++)
                {
                    word[3] = alphabet[f];
                    //printf("%s\n", word);
                    hashed_word = crypt(word, salt);

                    if(strcmp(hashed_word, argv[1]) == 0)
                    {
                        goto PRINT;
                    }

                    for(int h = 0; h < strlen(alphabet); h++)
                    {
                        word[4] = alphabet[h];
                        //printf("%s\n", word);
                        hashed_word = crypt(word, salt);

                        if(strcmp(hashed_word, argv[1]) == 0)
                        {
                            goto PRINT;
                        }
                    }
                }
            }
        }
    }

PRINT: printf("%s\n", word);
return 0;
}
0

The operative phrase is "seemingly forever". This is a brute force program. It will run quickly for short passwords and take a long time for others. If you want to observe performance, try putting a counter and some printf statements at strategic points in the program to observe what's running. (Increment the counter with each printf statement and print the current counter value out.) You could also try running it overnight and see if it finishes.

When password length exceeds about 4 in this code, it will take substantially longer to execute.

You could also try looking at the code to see if there's any way to make it run more efficiently. Since shorter passwords are successfully cracked, I'm inclined to think it is working, just very slowly, but there's no guarantee.

If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)

You must log in to answer this question.

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