0

I am trying to figure out how to solve the crack problem from pset2, everything works fine with compiling but when I try to crack the hash from the example

50cI2vYkF0YU2 which is Zamayla's password (LOL), I still dont get it as an output! I added the printf("%s\n,key); so that I make sure (LOL) is there and I can find it screenshot from the output of my program

so what am I doing wrong?

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


bool crack(string given_hash);


int main(int argc, string argv[])
{
if (argc != 2)
{
    printf("Usage ./crack hash\n");
    return 1;
}

if (!crack(argv[1]))
    return 1;
}

bool crack(string given_hash)

{    
string new_hash;
string Alphabet = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
char key[6];
char salt[3];
salt[0] = given_hash[0], salt[1] = given_hash[1], salt[2] = '\0';


// single-letter keys.
for (int i = 0; i < 52; i++)
{
    key[0] = Alphabet[i], key[1] = '\0';
     new_hash = crypt(key,salt);
    if (strcmp(new_hash, given_hash) == 0)
    {
        printf("given hash: %s\n",given_hash);
        printf("new hash: %s\n",new_hash);
        printf("you got the key: %s\n",key);
        return 0;
    }
}

// fpr 2-letter keys.
for (int i = 0; i < 52; i++)
{
    key[0] = Alphabet[i], key[2] = '\0';
    for (int j = 0; j < 52; j++)
    {
        key[1] = Alphabet[j]; 
    }
     new_hash = crypt(key,salt);
    if (strcmp(new_hash, given_hash) == 0)
    {
        printf("given hash: %s\n",given_hash);
        printf("new hash: %s\n",new_hash);
        printf("you got the key: %s\n",key);
        return 0;
    }
}

// for 3-letter keys.
for (int i = 0; i < 52; i++)
{
    key[0] = Alphabet[i], key[3] = '\0';
    for (int j = 0; j < 52; j++)
    {
        key[1] = Alphabet[j];
        for (int k = 0; k < 52; k++)
        {
            key[2] = Alphabet[k];
            printf("%s\n",key);
        }
    }
     new_hash = crypt(key,salt);
    if (strcmp(new_hash, given_hash) == 0)
    {
        printf("given hash: %s\n",given_hash);
        printf("new hash: %s\n",new_hash);
        printf("you got the key: %s\n",key);
        return 0;
    }
}

// for 4-letter keys.
for (int i = 0; i < 52; i++)
{
    key[0] = Alphabet[i], key[4] = '\0';
    for (int j = 0; j < 52; j++)
    {
        key[1] = Alphabet[j];
        for (int k = 0; k < 52; k++)
        {
            key[2] = Alphabet[k];
            for( int l = 0; l < 52; l++)
            {
                key[3] = Alphabet[l];
            }
        }
    }
     new_hash = crypt(key,salt);
    if (strcmp(new_hash, given_hash) == 0)
    {
        printf("given hash: %s\n",given_hash);
        printf("new hash: %s\n",new_hash);
        printf("you got the key: %s\n",key);
        return 0;
    }
}

// for 5-letter keys.
for (int i = 0; i < 52; i++)
{
    key[0] = Alphabet[i], key[5] = '\0';
    for (int j = 0; j < 52; j++)
    {
        key[1] = Alphabet[j];
        for (int k = 0; k < 52; k++)
        {
            key[2] = Alphabet[k];
            for(int l = 0; l < 52; l++)
            {
                key[3] = Alphabet[l];
                for(int m = 0; m < 52; m++)
                {
                    key[4] = Alphabet[m];
                }
            }
        }
    }
     new_hash = crypt(key,salt);
    if (strcmp(new_hash, given_hash) == 0)
    {
        printf("given hash: %s\n",given_hash);
        printf("new hash: %s\n",new_hash);
        printf("you got the key: %s\n",key);
        return 0;
    }
}

printf("you have done something wrong!\n");
return 1;

}

well there's definitely something wrong with the crypt function how do I get same hashes for so many different keys ?

screenshot from the new hashes my code generates

0
1

What you're really trying to say is that it compiles, but it doesn't work. There's a major problem with the logic. I won't spoil it for you, but I'll point you at it.

Add a print statement to print out all of the single char keys and another to print out all of the two char keys inside their respective loops.

I would also add that the code above doesn't seem to produce output that matches the output posted. Are you sure that you posted the correct code, or that you've recompiled your edited code?

Also, add a return 0; statement between the two char and 3 char loops to stop the program so you can see what's happening.

Once you do this, you'll understand exactly what's wrong. Hopefully, you'll also know how to fix it. If not, leave a comment!.

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

5
  • But I already did that, I printed the new hashes I generate wit 'crypt' and it seems that the new hashes are generated every now and then not for every new key which is weird. – Diaa Alaa Mar 16 '19 at 8:31
  • No I haven't updated the code, but when I get home I will for sure. – Diaa Alaa Mar 16 '19 at 8:34
  • no, I didn't say to print the hashes, I said to print the 1 and 2 letter keys used to generate the hashes. If you did, you'd see that the two letter hashes all end with 'z' (at least from the code above.) That should be a really big clue. Also, remember that each time you change the code, it needs to be recompiled. It's not an automatic process. – Cliff B Mar 16 '19 at 8:38
  • I didnt get the "all end with z" keys because I put the printed key where I should have put the crypt statement ( inside the last letter loop). – Diaa Alaa Mar 18 '19 at 2:36
  • thank you for your hint, I knew I had the problem in my crypt function because I kept getting the same hashes though I had different keys because it actually bounced back to the first key and crypted it, after I rearranged the crypt statement inside the last letter loop, I got new hashes with every new key and voala, it worked like magic. Thank you for not spoiling it sir, it made me reread the whole code over and over until I figured it out myself, sorry for the late response I had tons of work to do and I couldnt get back to my computer. – Diaa Alaa Mar 18 '19 at 2:38

You must log in to answer this question.

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