1

this version of crack works for lol, and prints the following: new hash = 50i2t3sOSAZtktr [what is the 'tr' at the end???] lol

for other passwords, the program never returns a decoded pw, just exits.

#include <cs50.h>
#include <stdio.h>
#include <string.h>
#include <crypt.h>
#define _XOPEN_Source
#include <unistd.h>
char get_hash[14];  //this is an array with 14 locations, static allocation, last location is \0; it is a string and it is pointer is get_hash
//crack a password that is up to 5 characters; using the salt, cycle through all the options from 1-5 chars

int main(int argc, char* argv[])

{
//printf ("argc1 = %d\n", argc);
    if (argc != 2)     
    {
     printf ("usage: ./crack hash\n"); 
     return (1);
    }

    if (argc == 2)     
    {  
        strcpy(get_hash, argv[1]);
        printf("hash being input is = %s\n", get_hash);       
   // {
    //  for (int i=0, n=strlen (argv[1]); i<n; i++)
     // {
          //get_hash[i]=*(argv[1][i];)

     //// }
      //printf ("get_hash=%str", get_hash))
    }
               else
               {
               printf ("usage: ./crack hash\n"); 
               return (1);
               }
    string testit="NOPE";  
    string alloptions = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";  //everything to be compared
    char collect[6];   //string of 6 characters
    char salt[3]; salt[0] = get_hash[0]; salt[1] = get_hash[1]; salt[2] = '\0';  get_hash[13]= '\0';//get the salt from the hash that is input; string
//first try all the single letter options
for (int i=0; i<26+26; i++)
{
    collect[0] = alloptions[i];
    collect[1] = '\0';
    //printf("collect[0]= %char=, %d= \n", collect[0], collect[0]);
    //printf("collect[1]= %char\n", collect[1]);
    string new_hash= crypt(collect, salt);
    //printf("new_hash = %str\n ", new_hash);
    //printf("get_hash = %str\n", get_hash);
    if (strcmp(new_hash, get_hash)==0)
    {
        printf("%s\n", collect);
        printf("get through 1 \n");
        return 0;
    }

}
    printf("get through 1 \n");
  //////////////////////////////////////////////////////////////////////////////////  
 //now try 2 letter passwords
  for (int i=0; i<26+26; i++)
{
    collect[0] = alloptions[i];
      for (int j=0; j<26+26; j++)
      {
          collect[1]=alloptions[j];
          collect[2] = '\0';
          char* new_hash= crypt(collect, salt);
          if (strcmp(new_hash, get_hash) == 0)
              {
                  printf("%s\n", collect);
                   return 0;
              }
       }
 }

printf("get through 2 \n");        
////////////////////////////////////////////////////////////////
 //now try 3 letter passwords
 for (int i=0; i<26+26; i++) 
   {           
    collect[0] = alloptions[i];           
   for (int j=0; j<26+26; j++)   
      {
       collect[1] = alloptions[j]; 
            for (int k=0; k<26+26;k++)
            {
                collect[2] = alloptions[k];
                collect[3] = '\0';
    //printf("collect[2]= %char=, %d= \n", collect[2], collect[2]);
    //printf("collect[3]= %char\n", collect[3]);
                char* new_hash = crypt(collect, salt);
                //printf("new_hash = %str\n ", new_hash);
                printf("new_hash = %str\n", new_hash);
                if (strcmp(new_hash, get_hash)==0)
                {
                  printf("%s\n", collect);
                 return 0;   
            }      
         }

       } 


    }
printf("get through 3 \n");
//try 4 letters
 for (int i=0; i<26+26; i++) 
{           
     collect[0] = alloptions[i];           
   for (int j=0; j<26+26; j++)   
      {
        collect[1] = alloptions[j];  
            for (int k=0; k<26+26;k++)
            {
              collect[2] = alloptions[k];
             for (int l=0; l<26+26;l++)   
             {
               collect[3]= alloptions[l];
               collect[4]= '\0';
               char* new_hash = crypt(collect, salt);

                if  (strcmp(new_hash, get_hash)==0 || strcmp(collect, testit)==0)
                {
                  printf("%s\n", collect);
                  printf("%string\n", new_hash);
                  printf("%string\n", get_hash);

                 return 0;      
                }

             }   
            }       
           }        
        } 

    printf("get through 4 \n");
 //////////////////////////////////////////////////////////////////////////////               
//try five characters                

for (int i=0; i<26+26; i++) 
   {           
    collect[0] = alloptions[i];           
   for (int j=0; j<26+26; j++)   
      {
        collect[1] = alloptions[j];  
            for (int k=0; k<26+26;k++)
            {
                collect[2] = alloptions[k];
             for (int l=0; l<26+26;l++)   
             {
               collect[3]= alloptions[l];

             for (int m=0; l<26+26;l++)
             {   collect[4]=alloptions[m];
                 collect[5]= '\0';
                 char* new_hash = crypt(collect, salt); 
                 if (strcmp(new_hash, get_hash)==0)
                {
                  printf("%s\n", collect);
                 return 0;      
                }
             }  
           }       
        }        
    }              
  }   
 printf("no luck\n");  
 printf("get through 5\n");
 return 1;             


   }  
0

1 Answer 1

0

I want to preface this by saying I am fairly new to programming so not entirely sure. First, I think you need to declare an actual variable for get_hash. I don't think you are declaring it right. I would just say string get_hash = argv[1]; Next, for your collect variable, you are getting garbage values. Think, if you are declaring a variable of 6 characters, so long as none of them are defined, random values are going to be put there. I would change that if I were you. Once you figure those out and see whether or not it helped you, then I can try assisting you again. Have a great night!

3
  • 1
    1. I had made a mistake in the indices. I declared string get_hash;then added this: get_hash = argv[1]; I can now get Malan's pw (NOPE) from the hash malan: 50QvlJWn2qJGE (NOPE). However, when I submit the program, the only remark I see is : :) crack submitted (in green) no other remarks appear?
    – lMH
    Commented Mar 8, 2019 at 4:44
  • That means you did everything right! Commented Mar 8, 2019 at 13:17
  • 1
    oh boy...i kept trying to find errors, since I thought the check was just stuck!!!
    – lMH
    Commented Mar 8, 2019 at 14:09

You must log in to answer this question.

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