I've been trying to get my crack code working for days now. It took me a while to even understand the problem but I think I have somewhat of a handle on it now. My code compiles completely fine but when I run ./crack 50fkUxYHbnXGw absolutely nothing happens. I get no errors, no output but also my code doesn't even seem to exit. I am never returned to ~/workspace/pset2/crack/. I also can't get debug50 to work either so I'm at a bit of a loss! Any help would be much appreciated.

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

char *crypt(const char *key, const char *salt);

int main(int argc, string argv [])
    if (argc != 2)
        printf("Error in command line input.\n");
        return 1;

    char key[5];
    char salt[] = "50";
    char letters[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";

    while (strcmp(argv[1], crypt(key, salt)) !=0)
        for (int i = 0; i < 51; i++)
        {    key[0] = letters[i];
            for (int j = 0; j < 51; j++)
                key[1] = letters[j];
                for (int k = 0; k < 51; k++)
                    key[2] = letters[k];
                    for (int l = 0; l < 51; l++)
                        key[3] = letters[l];

    } printf("%s\n", key);

    return 0;

2 Answers 2


The problem is that you have essentially created an infinite loop.

Initially when your while code starts, your first for loop runs, then your second, then third and then fourth but they never check the while loop condition, so they keep on going on till the letters in key are "zzzz" and if the password you gave is not "zzzz" the while loop condition (which is checked when all for loops are completed) becomes false, so all the for loops execute again, condition is again checked with "zzzz", again failed and so on.

So you need to find a way to check the main condition after every cycle of every for loop.


I think the issue is that key is initialized by c to a random value (whatever is in the bits that are allocated to the variable, and because your set of possible characters does not include '\0', the program will always read these random characzters as part of the key. Try adding \0 to the character set. However maybe you will have to change the algorithm because you will have key like a\0BBc and I am not sure how it will be interpreted. In general, I suggest making the program print all the keys that it processes, to make sure that you are looping through them correctly. You can try with a smaller set of characters, say (a,b,c), and if you want to test also for the password, you can use the crypt function to calculate the hash of a simple test password, say baca, that you can feed as command argument. Once you are certain that all combinations are processed you can eliminate the printing part to speed up everything and expand to the full char set.

You must log in to answer this question.

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