0

My crack code works for the rofl code but not for all I tried two other codes and I don't know why was it not able to crack thos codes even when I increased my range of characters for search

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

int main(int argc, string argv[])
{
char i,j,k,l;
if(argc==2)
{

    char a[5]="\0";
    for(i=48;i<=122;i++)
    {
        a[0]=i;

        if(strcmp(crypt(a,"50"),argv[1])==0)
        {
         printf("%s\n", a);
         break;

        }
        else{
                for(j=48;j<=122;j++)
                {
                    a[1]=j;

                    if(strcmp(crypt(a,"50"), argv[1])==0)
                    {
                        printf("%s\n", a);
                        break;

                    }
                    else
                    {
                        for(k=48;k<=122;k++)
                        {
                            a[2]=k;
                            if(strcmp(crypt(a,"50"), argv[1])==0)
                            {
                                printf("%s\n", a);
                                break;

                            }
                            else
                            {
                                for(l=48;l<=122;l++)
                                {
                                    a[3]=l;


                                    if(strcmp(crypt(a,"50"), argv[1])==0)
                                    {
                                        printf("%s\n", a);
                                         break;

                                    }
                                }
                            }
                        }
                    }
                }
            }
        }


    }


}

Please help me fast....

1 Answer 1

0

You don't appear to be handling passwords that are fewer than 4 chars.

edited to add example

You have 4 nested loops. Each one sets the value of the char but later chars in the string are not reset, so when you move back up to an earlier loop, the later chars are still in the string.

For simplicity, in this example, let's say you were only looping over A to C and your passwords could only be 3 letters long.

Loop0 from 'A' to 'C'
  set a[0] & strcmp
  Loop1 from 'A' to 'C' 
    set a[1] & strcmp
    Loop2 from 'A' to 'C'
      set a[2] & strcmp

Now let's run this, looking for a result of "BA" as the password

L0:  a[0] is 'A' so a is "A"  // doesn't match  enter loop 1
L1:  a[1] is 'A' so a is "AA" // doesn't match  enter loop 2
L2:  a[2] is 'A' so a is "AAA" // doesn't match
L2:  a[2] is 'B' so a is "AAB" // doesn't match
L2:  a[2] is 'C' so a is "AAC" // doesn't match
L2: end  
L1:  a[1] is 'B' so a is "ABC" // doesn't match  enter loop 2
L2:  a[2] is 'A' so a is "ABA" // doesn't match
L2:  a[2] is 'B' so a is "ABB" // doesn't match
L2:  a[2] is 'C' so a is "ABC" // doesn't match
L2: end  
L1:  a[1] is 'C' so a is "ACC" // doesn't match  enter loop 2
L2:  a[2] is 'A' so a is "ACA" // doesn't match 
L2:  a[2] is 'B' so a is "ACB" // doesn't match
L2:  a[2] is 'C' so a is "ACC" // doesn't match  
L2: end
L1: end
L0:  a[0] is 'B' so a is "BCC" // doesn't match enter loop 1
L1:  a[1] is 'A' so a is "BAC" // doesn't match enter loop 2
L2:  a[2] is 'A' so a is "BAA" // doesn't match
L2:  a[2] is 'B' so a is "BAB" // doesn't match
L2:  a[2] is 'C' so a is "BAC" // doesn't match 
L2: end
L1:  a[1] is 'B' so a is "BBC" // doesn't match enter loop 2
L2:  a[2] is 'A' so a is "BBA" // doesn't match

etc etc etc.

Can you see that you will never get to "BA" because a[2] is never reset to NULL?

Every time you enter one of those loops, you need to set the next char to NULL.

end of edit

Also, no need to include all those extra chars. Passwords will only include chars A-Z or a-z.

2
  • I get that the passwords contain only uppercase and lowercase characters but I am also handling passwords that are less than 4 characters as I checked for that in the most outer loop.... Can you please explain how did you know that the passwords less than 4 characters aren't being generated ? Commented Dec 21, 2017 at 6:46
  • see my edited answer
    – curiouskiwi
    Commented Dec 21, 2017 at 20:18

You must log in to answer this question.

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