0

I thought this code would iterate over all possible combos from aaaa-ZZZZ; however, it fails to do so. I would just like some insight into why my program may not be functioning properly. Thanks in advance.

#define salt         "50"
#define _XOPEN_SOURCE
#include <unistd.h>
#include <cs50.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

int main(int argc, string argv[])
{
// alert if 1 or 3 arg given
if (argc != 2)
{
    printf("Usage: ./crack k\n");
    return 1;
}

// store hashed pass
string hashpass = argv[1];
// create alphabet
char alphabet[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
// create pass
char pass[5] = {0};

//iterate to create guess passes
for (int i = 0; i < 52; i++) {
    pass[0] = alphabet[i];
    for (int j = 0; j < 52; j++) {
        pass[1] = alphabet[j];
        for (int k = 0; k < 52; k++) {
            pass[2] = alphabet[k];
            if (hashpass == crypt( pass, salt)) {
            printf("%s\n", pass);
            return 0;
            }
            for (int l = 0; l < 52; l++) {
                pass[3] = alphabet[l];
                if (hashpass == crypt( pass, salt)) {
                    printf("%s\n", pass);
                    return 0;
                }
            }
        }
    }        
}
printf("%s\n", pass);
printf("Failed :(\n");
return 1;

}

1

It doesn't work: string1 == string2. Even if they are equal you won't get "true". You should compare them char by char. Happily, somebody has already coded special function available in string.h: strcmp(string1, string2) which returns 0 if they are equal.

Other issues:

  1. You should check not only 3 or 4 char passwords, but 1 and 2 as well.
  2. I'm puzzled about 4 min running time. I even copy paste your code and run it. Well it takes some time as there are a lot of operations. But it is 10 sec, might be 20. But not 4 min.
6
  • First off thanks so much for the help. Secondly, I made the fixes that you highlighted and now the runtime is under 20 seconds, but the code fails to find the password. I used strcmp and it still won't function properly... – Drusk Jul 9 '17 at 21:35
  • Hmm, it should I believe. Let me see your updated code, your input and output. – obesman Jul 10 '17 at 4:04
  • for (int i = 0; i < 52; i++) { pass[0] = alphabet[i]; if (strcmp(hashpass,crypt(pass, salt)) == 0) { printf("%s\n", pass); return 0; } for (int j = 0; j < 52; j++) { pass[1] = alphabet[j]; if (strcmp(hashpass,crypt(pass, salt)) == 0) { printf("%s\n", pass); return 0; } This is repeated 2 times – Drusk Jul 10 '17 at 14:01
  • Input is ./crack 50.jPgLzVirkc. Output is nothing. Just a new prompt such as ~/workspace/pset2/ $ – Drusk Jul 10 '17 at 14:04
  • Your code seems should work for 4 char pass. Try 50fkUxYHbnXGw. Shorter checks don't work because after first iteration till inner loop you have pass[] already filled with "aaaa". so you can not check "b" for example, only "baaa". You need either to create four arrays of 1,2,3,4 elements or to "clear" back to 0 each element after you pass corresponding loop. – obesman Jul 10 '17 at 16:00

You must log in to answer this question.

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