I am on the crack challenge in PSet 2. When I am reassigning the variables to check the crypt, I keep getting a segmentation error, so it's not changing. I understand my code is messy, but here it is. If you could help me out, that would mean a lot. Have a great day!
#define _XOPEN_SOURCE
#include <cs50.h>
#include <stdio.h>
#include <string.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* pass1;
char* pass2;
char* pass3;
char* pass4;
char* pass5;
// Decrypted
string decrypted;
// Main function
for (int i = 0; i < 52; i++)
{
// One character password
pass1 = &alpha[i];
string crypted = crypt(pass1, salt);
if (crypted == password) {
decrypted = pass1;
}
}
for (int j = 0; j < 52; j++)
{
// Two character password
pass2 = pass1;
strcat(pass2, &alpha[j]);
if (crypt(pass2, salt) == password)
{
decrypted = pass2;
}
}
for (int k = 0; k < 52; k++)
{
pass3 = pass2;
strcat(pass3, &alpha[k]);
if (crypt(pass3, salt) == password)
{
decrypted = pass3;
}
}
for (int l = 0; l < 52; l++)
{
// Four character password
pass4 = pass3;
strcat(pass4, &alpha[l]);
if(crypt(pass4, salt) == password)
{
decrypted = pass4;
}
}
for (int m = 0; m < 52; m++)
{
// Five character password
pass5 = pass4;
strcat(pass5, &alpha[m]);
if(crypt(pass5, salt) == password)
{
decrypted = pass5;
}
}
printf("password: %s\n", decrypted);
return 0;
}