0

Crack compiles, but when I run it, nothing happens (I know the code isn't pretty, but I couldn't find the fault). It doesn't return anything, doesn't even stop. Thanks in advance! p.s. Sorry for the weird format, can't figure out how to change it

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

int main (int argc, string argv[])
{
    if (argc!=2)
    {
        printf("usage: ./crack hash");
        return 1;
    }

    string hash = argv[1];

    //gets input

    char salt[2];
    char key[4];
    key[0] = 65;
    salt[0] = hash[0];
    salt[1] = hash[1];
    while(key[0]<123)
    {
        if (key[0]>90)
        {key[0] = 97;}
        string temp = crypt(key, salt);
        if (temp == hash)
        {
            printf("%s\n", key);
            return 0;
            break;
        }
        key[0]++;

    }

    //checks 1 digit passwords

    key[0] = 65;
    key[1] = 65;
    while(true)
    {
        if (key[0]>90)
        {
            key[0]=97;
        }
        if (key[1]>90)
        {
            key[1]=97;
        }
        string temp = crypt(key, salt);
        if (temp == hash)
        {
            printf("%s\n", key);
            return 0;
            break;
        }
        key[1]++;
        if(key[1]>122)
        {
            key[0]++;
            key[1]= 65;
        }
        if (key[0]>122)
        {
            break;
        }
    }

    //checks 2 digit passwords

    key[0] = 65;
    key[1] = 65;
    key[2] = 65;
    while(true)
    {
        if (key[0]>90)
        {
            key[0]=97;
        }
        if (key[1]>90)
        {
            key[1]=97;
        }
        if (key[2]>90)
        {
            key[2]=97;
        }
        string temp = crypt(key, salt);
        if (temp == hash)
        {
            printf("%s\n", key);
            return 0;
            break;
        }
        key[2]++;
        if(key[2]>122)
        {
            key[1]++;
            key[2]= 65;
        }
        if (key[1]>122)
        {
            key[0]++;
            key[1]= 65;
        }
        if (key[0]>122)
        {
            break;
        }
    }

    //checks 3 digit passwords

    key[0] = 65;
    key[1] = 65;
    key[2] = 65;
    key[3] = 65;
    while(true)
    {
        if (key[0]>90)
        {
            key[0]=97;
        }
        if (key[1]>90)
        {
            key[1]=97;
        }
        if (key[2]>90)
        {
            key[2]=97;
        }
        if (key[3]>90)
        {
            key[3]=97;
        }
        string temp = crypt(key, salt);
        if (temp == hash)
        {
            printf("%s\n", key);
            return 0;
            break;
        }

        key[3]++;
        if(key[3]>122)
        {
            key[2]++;
            key[3]= 65;
        }

        if(key[2]>122)
        {
            key[1]++;
            key[2]= 65;
        }
        if (key[1]>122)
        {
            key[0]++;
            key[1]= 65;
        }
        if (key[0]>122)
        {
            printf("error\n");
            break;
        }
    }
    //checks 4 digit passwords
}

1 Answer 1

0

Your code will not get past the first while loop because the while condition never returns false.

key[0]++;

What you are saying here is to take the value of the first index of key and add 1. You are not actually changing the index of key.

You can use a variable to keep track of your index. For example

int i = 0;
key[i];
i++;

This will increment the index of key for each run of the while loop.

When you run your code and it doesn't exit, just sits there not doing anything, it is likely it is stuck in an infinite loop. A good way to figure out where you are going wrong is to print something after each loop and this should tell you which loop your code gets caught in.

EDIT: Your 'if' statement if causing an infinite loop. When you the if statement returns true and key[0] becomes 97, on the next iteration key[0] will be 98. This also meets the 'if' statement condition and so the loop will keep assigning the value of 97 meaning the while condition always returns true.

3
  • The thing is, key[0] value is used in the loop condition, so the value is what I want to increment each time, not the index. I've changed all the loops to work on the same condition instead of forever, but so far I'm getting the same thing...
    – Rainy
    Commented May 22, 2017 at 13:11
  • 1
    Oh okay, your 'if' statement if causing an infinite loop. When you the if statement returns true and key[0] becomes 97, on the next iteration key[0] will be 98. This also meets the 'if' statement condition and so the loop will keep assigning the value of 97 meaning the while condition always returns true. Commented May 25, 2017 at 15:30
  • @cr0nch, edit your answer to include what you said in your comment. Commented Jun 21, 2017 at 17:54

You must log in to answer this question.

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