0

I'm stuck on pset2's Hacker Edition crack.c. I made this code after exploring various ideas for hours and it really makes sense to me but apparently the dumb computer can't really understand what I'm trying to say (sarcasm).

Can anyone elaborate what I'm doing wrong? The code compiles perfectly, but when I execute the file it like processes for a minute and so and then just returns me to my workspace like nothing happened. It does print out the newline tho.

Also I am aware of the fact that with this code I am only covering the all 4 letter combinations (meaning that I do not cover combinations with 1, 2 or 3 letters). However, this hash (50fkUxYHbnXGw) is 'rofl' encrypted and it still doesn't get printed out. Thanks.

#define _XOPEN_SOURCE
#include <unistd.h>
#include <stdio.h>
#include <cs50.h>
#include <string.h>
#include <stdlib.h>

int main (int argc, string argv [])
{
const char *salt = "50";
if (argc != 2 ){
    printf ("Bad job mate\n");
    return 1;
    }

char alphabet [] = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u',
                    'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',
                    'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'};
char result [4];
int i, j, k, l;
for (i = 0; i < 52; i++){
    for (j = 0; j < 52; j++){
        for (k = 0; k < 52; k++){
            for (l = 0; l < 52; l++){
                result [0] = alphabet [i];
                result [1] = alphabet [j];
                result [2] = alphabet [k];
                result [3] = alphabet [l];
                if (crypt (result, salt) == argv [1]){
                    printf ("%c%c%c%c", result [0], result [1], result [2], result [3]);
                    break;
                }

            }
        }
    }
}
printf ("\n");
return 0;
}
0

just in case im not to late the differences between mine and yours is my char result[4] was char result[5] for the 4 digit chars and the last char was a \0. i just checked my solution without the 5th nul char at the end and it failed to work. just so you know though the 4 digit test only will find 4 of the 10 solutions

0

Very late reply to your question, hope it's still helpful.

I believe I had a similar issue to you when using the boolean statement crypt (result, salt) == argv [1]

The way it was explained to me was that argv[v] is a pointer, and char * is the result of the crypt function, so you can't compare pointers to a char array using the == operator. Instead, strcmp (String compare) works better as follows: strcmp(string1, string2) Which will return:

  • <0: string1 < string2
  • .>0: string1 > string2
  • =0: string1 = string2

You must log in to answer this question.

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