For pset2 crack, I have managed to crack 10 of the 12 passwords given, but, the other 2 passwords are not getting cracked. Any help would be appreciated.
Code:
//Cracks password encrypted using crypt function using brute force
#define _XOPEN_SOURCE
#include <cs50.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
bool decrypt_1(string hash, string salt);
bool decrypt_2(string hash, string salt);
bool decrypt_3(string hash, string salt);
bool decrypt_4(string hash, string salt);
bool decrypt_5(string hash, string salt);
int main(int argc, string argv[])
{
//Ensures proper usage
if (argc != 2)
{
printf("Usage: ./crack hash(hash made by crypt function)\n");
return 1;
}
//Gets hash
string hash = argv[1];
//Extracts salt from hash
char salt[3];
salt[0] = hash[0];
salt[1] = hash[1];
salt[2] = '\0';
//Tries to crack password
bool decrypted_1 = decrypt_1(hash, salt);
bool decrypted_2 = decrypt_2(salt, salt);
bool decrypted_3 = decrypt_3(salt, salt);
bool decrypted_4 = decrypt_4(salt, salt);
bool decrypted_5 = decrypt_5(salt, salt);
//If password can't be cracked, prints error and exits
if (!decrypted_1 && !decrypted_2 && !decrypted_3 && !decrypted_4 && !decrypted_5)
{
printf("Usage: ./crack hash(hash made by crypt function)\n");
return 2;
}
}
//Cracks single-letter password
bool decrypt_1(string hash, string salt)
{
//Declares all possibilities to iterate through
string possibilities = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
char tries[6];
int num = strlen(possibilities);
for (int i = 0; i < num; i++)
{
//Sets first letter to i'th possibility
tries[0] = possibilities[i];
tries[1] = '\0';
tries[2] = '\0';
tries[3] = '\0';
tries[4] = '\0';
tries[5] = '\0';
string s = crypt(tries, salt);
eprintf("%s\n", tries);
//Checks if hashes match
if (strcmp(s, hash) == 0)
{
printf("%s\n", tries);
return true;
}
}
return false;
}
//Cracks 2-letter passwords
bool decrypt_2(string hash, string salt)
{
//Declares all possibilities to iterate through
string possibilities = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
char tries[6];
int num = strlen(possibilities);
//Cracks 2-letter passwords
for (int i = 0; i < num; i++)
{
//Sets first letter to i'th possibility
tries[0] = possibilities[i];
for (int j = 0; j < num; j++)
{
//Sets second letter to j'th possibility
tries[1] = possibilities[j];
tries[2] = '\0';
tries[3] = '\0';
tries[4] = '\0';
tries[5] = '\0';
string s = crypt(tries, salt);
eprintf("%s\n", tries);
//Checks if hashes match
if (strcmp(s, hash) == 0)
{
printf("%s\n", tries);
return true;
}
}
}
return false;
}
//Cracks 3-letter passwords
bool decrypt_3(string hash, string salt)
{
//Declares all possibilities to iterate through
string possibilities = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
char tries[6];
int num = strlen(possibilities);
//Cracks 3-letter passwords
for (int i = 0; i < num; i++)
{
//Sets first letter to i'th possibility
tries[0] = possibilities[i];
for (int j = 0; i < num; i++)
{
//Sets second letter to j'th possibility
tries[1] = possibilities[j];
for (int k = 0; k < num; k++)
{
//Sets third letter to k'th possibility
tries[2] = possibilities[k];
tries[3] = '\0';
tries[4] = '\0';
tries[5] = '\0';
string s = crypt(tries, salt);
eprintf("%s\n", tries);
//Checks if hashes match
if (strcmp(s, hash) == 0)
{
printf("%s\n", tries);
return true;
}
}
}
}
return false;
}
//Cracks 4-letter passwords
bool decrypt_4(string hash, string salt)
{
//Declares all possibilities to iterate through
string possibilities = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
char tries[6];
int num = strlen(possibilities);
//Cracks 4-letter passwords
for (int i = 0; i < num; i++)
{
//Sets first letter to i'th possibility
tries[0] = possibilities[i];
for (int j = 0; j < num; j++)
{
//Sets second letter to j'th possibility
tries[1] = possibilities[j];
for (int k = 0; k < num; k++)
{
//Sets third letter to k'th possibility
tries[2] = possibilities[k];
for (int l = 0; l < num; l++)
{
//Sets fourth to l'th possibility
tries[3] = possibilities[l];
tries[4] = '\0';
tries[5] = '\0';
string s = crypt(tries, salt);
eprintf("%s\n", tries);
//Checks if hashes match
if (strcmp(s, hash) == 0)
{
printf("%s\n", tries);
return true;
}
}
}
}
}
return false;
}
//Cracks 5-letter passwords
bool decrypt_5(string hash, string salt)
{
//Declares all possibilities to iterate through
string possibilities = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
char tries[6];
int num = strlen(possibilities);
//Cracks 5-letter password
for (int i = 0; i < num; i++)
{
//Sets first letter to i'th possibility
tries[0] = possibilities[i];
for (int j = 0; j < num; j++)
{
//Sets second letter to j'th possibility
tries[1] = possibilities[j];
for (int k = 0; k < num; k++)
{
//Sets third letter to k'th possibility
tries[2] = possibilities[k];
for (int l = 0; l < num; l++)
{
//Sets fourth letter to l'th possibility
tries[3] = possibilities[l];
for (int m = 0; m < num; m++)
{
//Sets fifth letter to m'th possibility
tries[4] = possibilities[m];
tries[5] = '\0';
string s = crypt(tries, salt);
eprintf("%s\n", tries);
//checks if hashes match
if (strcmp(s, hash) == 0)
{
printf("%s\n", tries);
return true;
}
}
}
}
}
}
return false;
}
Failing hashes:
50C6B0oz0HWzo
&
50cI2vYkF0YU2
These passwords are for greg and zamyla in cs50x 2019