danyfebrero

(CS50) Crack

Sep 24th, 2019
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.71 KB | None | 0 0
  1. #include <cs50.h>
  2. #include <stdio.h>
  3. #include <crypt.h>
  4. #include <string.h>
  5.  
  6. /*
  7. brian:51.xJagtPnb6s
  8. bjbrown:50GApilQSG3E2
  9. emc:502sDZxA/ybHs
  10. greg:50C6B0oz0HWzo
  11. jana:50WUNAFdX/yjA
  12. lloyd:50n0AAUD.pL8g
  13. malan:50CcfIk1QrPr6
  14. natmelo:50JIIyhDORqMU
  15. rob:51v3Nh6ZWGHOQ
  16. veronica:61v1CDwwP95bY
  17. walker:508ny6Rw0aRio
  18. zamyla:50cI2vYkF0YU2
  19. */
  20.  
  21. bool checkpass(string inputhashes, string generatedpass, string isalt);
  22.  
  23. int main(int argc, string argv[])
  24. {
  25.     if (argc != 2)                            //checking if the amount of arguments is correct
  26.     {
  27.         printf("Usage: ./crack hash\n");
  28.         return 1;
  29.     }
  30.    
  31.     string hashedpass = argv[1];              //getting the hashed pass from the argument
  32.     int saltdigits = 2;                       //
  33.     char salt[saltdigits + 1];                //adding 1 space for the end of the array
  34.     for (int i = 0; i < saltdigits; i++)      //taking the salt from the hashedpass
  35.     {
  36.         salt[i] = hashedpass[i];
  37.     }
  38.  
  39.     string alphabeth = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
  40.     int alphalen = strlen(alphabeth);
  41.     //printf("%s\n", crypt("g","20"));
  42.  
  43.     //checking pass of 1 letter
  44.     for (int i = 0; i < alphalen; i++)
  45.     {
  46.         char key[2];
  47.         key[0] = alphabeth[i];
  48.         if (checkpass(hashedpass, key, salt) == true)
  49.         {
  50.             printf("The password is: %s\n", key);
  51.             return 0;
  52.         }
  53.     }
  54.    
  55.     //checking pass of 2 letters
  56.     for (int i = 0; i < alphalen; i++)            // first letter
  57.     {
  58.         for (int i1 = 0; i1 < alphalen; i1++)     // second letter
  59.         {
  60.             char key[3];
  61.             key[0] = alphabeth[i];
  62.             key[1] = alphabeth[i1];    // both letters together
  63.             if (checkpass(hashedpass, key, salt) == true)
  64.             {
  65.                 printf("The password is: %s\n", key);
  66.                 return 0;
  67.             }            
  68.         }
  69.     }    
  70.    
  71.     //checking pass of 3 letters
  72.     for (int i = 0; i < alphalen; i++)
  73.     {
  74.         for (int i1 = 0; i1 < alphalen; i1++)
  75.         {
  76.             for (int i2 = 0; i2 < alphalen; i2++)
  77.             {
  78.                 char key[4];
  79.                 key[0] = alphabeth[i];
  80.                 key[1] = alphabeth[i1];
  81.                 key[2] = alphabeth[i2];  
  82.                 if (checkpass(hashedpass, key, salt) == true)
  83.                 {
  84.                     printf("The password is: %s\n", key);
  85.                     return 0;
  86.                 }                
  87.             }
  88.         }
  89.     }
  90.    
  91.     //checking pass of 4 letters
  92.     for (int i = 0; i < alphalen; i++)
  93.     {
  94.         for (int i1 = 0; i1 < alphalen; i1++)
  95.         {
  96.             for (int i2 = 0; i2 < alphalen; i2++)
  97.             {
  98.                 for (int i3 = 0; i3 < alphalen; i3++)
  99.                 {
  100.                     char key[5];
  101.                     key[0] = alphabeth[i];
  102.                     key[1] = alphabeth[i1];
  103.                     key[2] = alphabeth[i2];
  104.                     key[3] = alphabeth[i3];
  105.                     if (checkpass(hashedpass, key, salt) == true)
  106.                     {
  107.                         printf("The password is: %s\n", key);
  108.                         return 0;                    
  109.                     }
  110.                 }                
  111.             }
  112.         }
  113.     }
  114.    
  115.     //checking pass of 5 letters    
  116.     for (int i = 0; i < alphalen; i++)
  117.     {
  118.         for (int i1 = 0; i1 < alphalen; i1++)
  119.         {
  120.             for (int i2 = 0; i2 < alphalen; i2++)
  121.             {
  122.                 for (int i3 = 0; i3 < alphalen; i3++)
  123.                 {
  124.                     for (int i4 = 0; i4 < alphalen; i4++)
  125.                     {
  126.                         char key[6];
  127.                         key[0] = alphabeth[i];
  128.                         key[1] = alphabeth[i1];
  129.                         key[2] = alphabeth[i2];
  130.                         key[3] = alphabeth[i3];
  131.                         key[4] = alphabeth[i4];
  132.                        
  133.                         if (checkpass(hashedpass, key, salt) == true)
  134.                         {
  135.                             printf("%s\n", key);
  136.                             return 0;                    
  137.                         }                      
  138.                     }
  139.                 }                
  140.             }
  141.         }
  142.     }
  143.    
  144.     printf("The password wasn't found.\n");
  145.     return 1;
  146. }
  147.  
  148. bool checkpass(string inputhashes, string generatedpass, string isalt)  //check if the hashes are equal
  149. {
  150.     bool result = false;
  151.     string join = crypt(generatedpass, isalt);
  152.     if (strcmp(join, inputhashes) == 0)
  153.     {
  154.         result = true;
  155.     }
  156.     return result;
  157. }
  158. // I need to find a way of not repeat all loops
Add Comment
Please, Sign In to add comment