So I've looked up a few posts with similar issues and taking the advice of commenters I've put printf after every For loop to see where the program gets stuck.
It was initially was printing one letter, then two letters, three letters and is now onto printing four letters. For example the last two words printed were 'MVwV, MVwU'. In my mind this is how I was hoping the program would work.
When I take the printf statements out though, the cursor just sits there and never prints the solution printf statement "Original password:....". Can anyone give me a hint as to why this might be?
#define _XOPEN_SOURCE
#include <crypt.h>
#include <cs50.h>
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
int main(int argc, string argv[])
{
int character;
int secondletter;
int thirdletter;
int fourthletter;
int fifthletter;
int letter;
char testpassword[6];
char salt[3];
if (argc!= 2)
{
printf("Error: please enter valid hashed password\n");
return 1;
}
else
{
string hashedpassword = argv[1];
salt[0] = hashedpassword[0];
salt[1] = hashedpassword[1];
salt[2] = '\0';
for (letter = 65; letter < 123; letter++)
{
if (letter == 91)
{
letter = 97;
}
testpassword[0] = letter;
testpassword[1] = '\0';
printf("%s\n", testpassword);
if (strcmp(hashedpassword, crypt(testpassword, salt)) == 0)
{
printf("Original password: %s \n", testpassword);
return 0;
}
}
for (letter = 65; letter < 123; letter++)
{
if (letter == 91)
{
letter = 97;
}
for (secondletter = 65; secondletter < 123; secondletter++)
{
if (secondletter == 91)
{
secondletter = 97;
}
testpassword[0] = letter;
testpassword[1] = secondletter;
testpassword[2] = '\0';
printf("%s\n", testpassword);
if (strcmp(hashedpassword, crypt(testpassword, salt)) == 0)
{
printf("Original password: %s \n", testpassword);
return 0;
}
}
}
for (letter = 65; letter < 123; letter++)
{
if (letter == 91)
{
letter = 97;
}
for (secondletter = 65; secondletter < 123; secondletter++)
{
if (secondletter == 91)
{
secondletter = 97;
}
for (thirdletter = 65; thirdletter < 123; thirdletter++)
{
if (thirdletter == 91)
(thirdletter = 97);
testpassword[0] = letter;
testpassword[1] = secondletter;
testpassword[2] = thirdletter;
testpassword[3] = '\0';
printf("%s\n", testpassword);
if (strcmp(hashedpassword, crypt(testpassword, salt)) == 0)
{
printf("Original password: %s \n", testpassword);
return 0;
}
}
}
}
for (letter = 65; letter < 123; letter++)
{
if (letter == 91)
{
letter = 97;
}
for (secondletter = 65; secondletter < 123; secondletter++)
{
if (secondletter == 91)
{
secondletter = 97;
}
for (thirdletter = 65; thirdletter < 123; thirdletter++)
{
if (thirdletter == 91)
(thirdletter = 97);
for (fourthletter = 65; fourthletter < 123; fourthletter++)
{
if(fourthletter == 91)
(fourthletter = 97);
testpassword[0] = letter;
testpassword[1] = secondletter;
testpassword[2] = thirdletter;
testpassword[3] = fourthletter;
testpassword[4] = '\0';
printf("%s\n", testpassword);
if (strcmp(hashedpassword, crypt(testpassword, salt)) == 0)
{
printf("Original password: %s \n", testpassword);
return 0;
}
}
}
}
}
for (letter = 65; letter < 123; letter++)
{
if (letter == 91)
{
letter = 97;
}
for (secondletter = 65; secondletter < 123; secondletter++)
{
if (secondletter == 91)
{
secondletter = 97;
}
for (thirdletter = 65; thirdletter < 123; thirdletter++)
{
if (thirdletter == 91)
(thirdletter = 97);
for (fourthletter = 65; fourthletter < 123; fourthletter++)
{
if(fourthletter == 91)
(fourthletter = 97);
for (fifthletter = 65; fifthletter < 123; fifthletter++)
{
if (fifthletter == 91)
fifthletter = 97;
testpassword[0] = letter;
testpassword[1] = secondletter;
testpassword[2] = thirdletter;
testpassword[3] = fourthletter;
testpassword[4] = fifthletter;
testpassword[5] = '\0';
printf("%s\n", testpassword);
if (strcmp(hashedpassword, crypt(testpassword, salt)) == 0)
{
printf("Original password: %s \n", testpassword);
return 0;
}
}
}
}
}
}
}
}