0

This is day 2 of being stuck on the problem, with over 12 hours spent on this problem. I can't seem to find the issue with my code (except that it has a runtime complexity of O(n^6) ). I know this is really bad design but I want to do this problem first somehow and then think about improving my code for better run time and I know this is possibly the worst run time one can achieve for this problem.

So, I can't seem to figure out why does my program seem to be stuck in an infinite loop! I tried crypt() on 'a' using the following code:

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

char salt[]="";
char plain[5];
plain[0]='a';
string hash = argv[1];
strncat(salt,hash,2);
string c =  crypt(plain,salt);   
printf ("%s\n",c); //print the hash    
return 0;
}

and then went on to "crack" the password with the hash generated by crypt() for 'a' i.e 50OqznXGVcOJU . But, my crack program still seems to be running forever, not able to crack 'a'. I tried running the debugger and it seems to be working like I expected, going through all the password of length 1 and then increment the length of password. And even the hash generated by the first program matches the hash generated in the "crack" program when I simply print it without comparing the hash. I'll post the code. It'd be helpful if somebody can take the time to look through it.

string ABC = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
int alphalen = strlen(ABC);
int len = 1;
char plain[6];
char salt[3];
strncat(salt,argv[1],2);
while(len <= 5)
{
    for (int i=0;i<alphalen;i++)
    {
        plain[0] = ABC[i];
        //check hash for passwords of length 1
        if (len ==1)
        {
            plain[1]='\0';
            string c0 = crypt(plain,salt);
            if (strcmp(c0,argv[1]) == 0)
            {
                printf ("%s\n",plain); //print the password
                return 0;
            }
        }
        //after trying all passwords of length 1
        if(len>1) //for passwords of length > 1
        {
            for (int j=0;j<alphalen;j++)
            {
                plain[1] = ABC[j];
                //try passwords of length 2
                if (len == 2)
                {
                    plain[2]='\0';
                    string c1 = crypt(plain,salt);
                    if (strcmp(c1,argv[1])==0)
                    {
                        printf ("%s \n",plain); //print the password
                        return 0;;
                    }
                }
                if(len>2) //for passwords of length > 2
                {
                    for (int k=0;k<alphalen;k++)
                    {
                        plain[2] = ABC[k];
                        //try passwords of length 3
                        if (len == 3)
                        {
                            plain[3]='\0';
                            string c2 = crypt(plain,salt);
                            if (strcmp(c2,argv[1])==0)
                            {
                                printf ("%s\n",plain); //print the password
                                return 0;;
                            }
                        }
                        if(len>3) //for passwords of length > 3
                        {
                            for (int l=0;l<alphalen;l++)
                            {
                                plain[3] = ABC[l];
                                //try passwords of length 4
                                if(len == 4)
                                {
                                    plain[4]='\0';
                                    string c3 = crypt(plain,salt);
                                    if (strcmp(c3,argv[1])==0)
                                    {
                                        printf ("%s\n",plain); //print the password
                                        return 0;;
                                    }
                                }
                                if(len>4) //for passwords of length > 4
                                {
                                    for (int m=0;m<alphalen;m++)
                                    {
                                        plain[4] = ABC[m];
                                        //try passwords of length 5
                                        if (len == 5)
                                        {
                                            plain[5]='\0';
                                            string c4 = crypt(plain,salt);
                                            if (strcmp(c4,argv[1])==0)
                                            {
                                            printf ("%s\n",plain); //print the password
                                            return 0;;
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
    len++; // increment after passwords of lenght = len not found
}
return 0;
}
7
  • char salt[] = ""; looks ugly, result is a char[1], you probably meant char salt[3];. You also might have meant char plain[6] = {};. Don't forget the null terminators! Oh, and <alphalen, not <=alphalen. Does it work if you temporarily do like while (len < 3), just to reduce maximum runtime? – Blauelf Mar 13 '18 at 16:48
  • salt[] =""; doesn't cause any trouble since I'm using strncat to copy the first 2 chars of hash to salt and strncat thus make a new string of length 2 itself. Also, yes I was wondering the same thing about plain[6], so do I deliberately make plain[6] = '\0' beforehand? or C will do it automatically. When I do "printf("%s",plain) " it prints properly string of any length upto 5 ,even though I did not terminate with \0. Strlen(ABC) gave me 51, that's why I used <= . I guess it's counting from 0? I don't think I missed any alphabet. – Vinit Kumar Singh Mar 13 '18 at 17:24
  • How should strncat allocate memory? You provide space for one character and write three. And it probably somewhat works by accident, as after starting the programme, most memory is zero, and variables often are placed at addresses divisible by 8, leaving some bytes unused. It's 51 since you are missing upper-case N. – Blauelf Mar 13 '18 at 17:32
  • Gee! Thanks . That's embarrassing. I've updated the code now. Take a look if possible . – Vinit Kumar Singh Mar 13 '18 at 17:59
  • Hey, my program works fine now! I've no idea how changing those things took me out of infinite loop. If there was some issue with string the program should have been ending without printing out anything , but it was just not ending. Thanks a lot man! I guess i really have to brush up some minute basics :) – Vinit Kumar Singh Mar 13 '18 at 18:05

You must log in to answer this question.

Browse other questions tagged .