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