2

I'm trying to make pset2 crack but having so much problems and my head is about to explode. Here is my code; my first error is on line 41. I never get to pass a value to the key for crypt. I'm very needed for help.

#include <stdio.h>
#include <cs50.h>
#include <ctype.h>
#include <string.h>
#include <crypt.h>
#include <unistd.h>

#define _XOPEN_SOURCE
char* crypt (const char *key, const char *salt);

int main (int argc, string argv[])
{
    // Check if only 2 arguments
    if (argc != 2)
    {
        printf("Insert only one hash\n");
        return 1;
    }

    // Creacion d variable para asignarle el valor del hash
    string hash = argv[1];
    char salt[] = {hash[0], hash[1], '\0'};

    printf ("Hash is: %s\n", hash);
    printf ("Salt is: %s\n", salt);

    // Creacion del loop para interactuar con el hash.
    // Creacion de array para almacenar los caracteres q se iran crakeando.
    // Creacion de variable para almacenar el valor q devuelva crypt function
    char alpha[] = {"abcdefghijklmnopqrstuvwxyz"};
    string cnum;

    for (int i = 0, n = strlen(hash); i < n; i ++)
    {
        for (int j = 0; j < 26; j++)
        {
            // Check si min o mayusc
            if (islower(hash[i]))
            {
                tolower(alpha[j]);
                cnum = crypt(alpha[j], salt);
                if (cnum == hash[i])
                {
                    break;
                }
            }
            else
            {
                toupper(alpha[j]);
                cnum = crypt(alpha[j], salt);
                if (cnum == hash[i])
                {
                    break;
                }
            }
        }
        printf ("%c", cnum);
    }

}

errors when compiling are:

crack.c:41:30: error: incompatible integer to pointer conversion passing 'char' to parameter of type 'const char *'; take the address with &
      [-Werror,-Wint-conversion]
                cnum = crypt(alpha[j], salt);
                             ^~~~~~~~
                             &
crack.c:9:26: note: passing argument to parameter 'key' here
char* crypt (const char *key, const char *salt);
                         ^
crack.c:42:26: error: comparison between pointer and integer ('string' (aka 'char *') and 'int') [-Werror]
                if (cnum == hash[i])
                    ~~~~ ^  ~~~~~~~
crack.c:50:30: error: incompatible integer to pointer conversion passing 'char' to parameter of type 'const char *'; take the address with &
      [-Werror,-Wint-conversion]
                cnum = crypt(alpha[j], salt);
                             ^~~~~~~~
                             &
crack.c:9:26: note: passing argument to parameter 'key' here
char* crypt (const char *key, const char *salt);
                         ^
crack.c:51:26: error: comparison between pointer and integer ('string' (aka 'char *') and 'int') [-Werror]
                if (cnum == hash[i])
                    ~~~~ ^  ~~~~~~~
crack.c:57:23: error: format specifies type 'int' but the argument has type 'string' (aka 'char *') [-Werror,-Wformat]
        printf ("%c", cnum);
                 ~~   ^~~~
                 %s
5 errors generated.
make: *** [crack] Error 1
1
  • I'm also having problems, except that the program is always printing Killed. Mar 26 '19 at 2:58
2

You seem to have some misunderstanding about the crypt function. crypt takes 2 arguments, a string (like "abcd") and a salt (like "50").

You appear to be trying to compare individual chars.

The idea here is that you are given a hash and you need to try all combinations of 1,2,3,4 & 5 letter long strings to try to find the password that matches the hash. So for example, you might test "a" then "b" then "c" ... then "aa" "ab" "ac" ... "ZZZZA" ... "ZZZZZ" until you find the match. You have to call crypt on every one of the possible combinations.

1
  • omg really misunderstod, lets see how it goes now. Thank you my friend you have been very helpful Feb 7 '18 at 6:15

You must log in to answer this question.

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