0

On Pset2, I feel like I am so close to finally finishing this project up. I am working on the crack one and no matter what password I input, the same password, ZZZZ returns. Where could I possibly be going wrong? I want to get it to a point where it works and then I will go and try to clean up my code. Thanks and have a great day!

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

int main(int argc, string argv[])
{
// 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 (crypted == password) {
        goto PRINT;
    }
}

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


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

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




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

as a bulk error, I will tell you that the following statement is incorrect if you want to compare two strings

if (crypted == password)

with this you are really comparing two pointers(memory addresses), if you want to really compare two strings you should use some function like strcmp ().

You must log in to answer this question.

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