0

I have been working on crack lately, and I have been running into a lot of problems. Sorry, but lately I have been needing to be on here a lot, probably asking stupid questions. I want to thank you all for all the help you guys have given me so far. Here is my problem: My code works for everything except for passwords that are five characters. Where could I be going wrong? Here is my code. Thank you all so much and have a wonderful day.

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

int main(int argc, string argv[])
{
clock_t tic = clock();
// Gets password and returns errors
if (argc <= 1)
{
    printf("Error: Enter a password when running the program\n");
    return 1;
} else if (argc >= 3)
{
    printf("Error: Please only input one value");
    return 2;
}
// Gets the salt and password
char salt[3] = {argv[1][0], argv[1][1]};
string password = argv[1];
// Alphabet
char* alpha = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";

// Passwords
char word[5];
char* crypted;

// Main function
for (int i = 0; i < 52; i++)
{
    // One character password
    word[0] = alpha[i];
    crypted = crypt(word, salt);
    if (strcmp(crypted, password) == 0) {
        goto PRINT;
    }
}

// Two characters
for (int i = 0; i < 52; i++)
{
    // One character password
    word[0] = alpha[i];
    crypted = crypt(word, salt);
    if (strcmp(crypted, password) == 0) {
        goto PRINT;
    }
    for (int j = 0; j < 52; j++)
    {
        // Two character password
        word[1] = alpha[j];
        crypted = crypt(word, salt);
        if (strcmp(crypted, password) == 0)
        {
            goto PRINT;
        }
    }
}


// Three Characters
for (int i = 0; i < 52; i++)
{
    // One character password
    word[0] = alpha[i];
    crypted = crypt(word, salt);
    if (strcmp(crypted, password) == 0)
    {
        goto PRINT;
    }
    for (int j = 0; j < 52; j++)
    {
        // Two character password
        word[1] = alpha[j];
        crypted = crypt(word, salt);
        if (strcmp(crypted, password) == 0)
        {
            goto PRINT;
        }
        for (int k = 0; k < 52; k++)
        {
            word[2] = alpha[k];
            crypted = crypt(word, salt);
            if (strcmp(crypted, password) == 0)
            {
                goto PRINT;
            }
        }
    }
}

for (int i = 0; i < 52; i++)
{
    // One character password
    word[0] = alpha[i];
    crypted = crypt(word, salt);
    if (strcmp(crypted, password) == 0)
    {
        goto PRINT;
    }
    for (int j = 0; j < 52; j++)
    {
        // Two character password
        word[1] = alpha[j];
        crypted = crypt(word, salt);
        if (strcmp(crypted, password) == 0)
        {
            goto PRINT;
        }
        for (int k = 0; k < 52; k++)
        {
            word[2] = alpha[k];
            crypted = crypt(word, salt);
            if (strcmp(crypted, password) == 0)
            {
                goto PRINT;
            }
            for (int l = 0; l < 52; l++)
            {
                // Four character password
                word[3] = alpha[l];
                crypted = crypt(word, salt);
                if(strcmp(crypted, password) == 0)
                {
                    goto PRINT;
                }
            }
        }
    }
}

for (int i = 0; i < 52; i++)
{
    // One character password
    word[0] = alpha[i];
    crypted = crypt(word, salt);
    if (strcmp(crypted, password) == 0)
    {
        goto PRINT;
    }
    for (int j = 0; j < 52; j++)
    {
        // Two character password
        word[1] = alpha[j];
        crypted = crypt(word, salt);
        if (strcmp(crypted, password) == 0)
        {
            goto PRINT;
        }
        for (int k = 0; k < 52; k++)
        {
            word[2] = alpha[k];
            crypted = crypt(word, salt);
            if (strcmp(crypted, password) == 0)
            {
                goto PRINT;
            }
            for (int l = 0; l < 52; l++)
            {
                // Four character password
                word[3] = alpha[l];
                crypted = crypt(word, salt);
                if(strcmp(crypted, password) == 0)
                {
                    goto PRINT;
                }
                for (int m = 0; m < 52; m++)
                {
                    // Five character password
                    word[4] = alpha[m];
                    crypted = crypt(word, salt);
                    if (strcmp(crypted, password) == 0)
                    {
                        goto PRINT;
                    }
                }
            }
        }
    }
}

PRINT: printf("password: %s\n", word);
clock_t toc = clock();
printf("Elapsed: %f seconds\n", (double)(toc - tic) / CLOCKS_PER_SEC);
return 0;
}
2

It doesn't work for 5 characters because you haven't allocated enough space for a 5-char string with char word[5]; Remember the null char!

As a consequence, when you set word = {'a', 'a', 'a', 'a', 'a'} and then use that with crypt, crypt will be expecting a string so it may not be "aaaa" but "aaaaaxyz" where x,y,z happen to be the next bytes of memory until you hit a 0.

In a nutshull, always be sure your char arrays that you want to treat as "strings" are null-terminated.

4
  • 1
    Very nice catch. I completely missed that.
    – Cliff B
    Aug 7 '18 at 0:48
  • Sorry, but what does that last sentence mean? All the passwords seem to work right now except for 50CcfIk1QrPr6, which returned ZZZZZ. I changed the word[5] to word[6]. Is there anything else I need to do? Thanks! Aug 7 '18 at 3:54
  • 1
    The last char in each string must be '\0' It's only luck that the others worked. When you set the salt, you need to specifically set salt[2]='\0' and when you declare word[6] you should initialize it to all '\0'
    – curiouskiwi
    Aug 7 '18 at 3:57
  • Perfect! That worked. Thanks, man! Aug 7 '18 at 12:14

You must log in to answer this question.

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