0

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;

}

1 Answer 1

1
  1. For comparing strings, use not == (which compares strings by memory location, not content), but strcmp, a function returning 0 for equal strings.

  2. Similarly, decrypted = pass1; will copy the memory location, not the characters. If you change those later, they will be changed for both variables. strcpy and its siblings will copy characters, but you have to provide the space. If you avoid changing the content after finding a match, you don't even have to copy the match to another variable.

  3. I totally not get your pass1 to pass5. I don't think there's a need for &alpha[i] here, more likely you should have something like a char[6] where you write your password candidate to, and which you pass to crypt.

You must log in to answer this question.

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