bocajbee

Crack Final

Jan 16th, 2020
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.20 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <cs50.h>
  3. #include <string.h>
  4. #include <stdlib.h>
  5. #include <ctype.h>
  6. #include <crypt.h>
  7.  
  8. // easy passwords for testing:
  9. // aaaaa:50XcgR31jl/4M
  10. // from problem set docs:
  11. // Z = 50R.6FuTGui8U
  12. // https://cs50.stackexchange.com/questions/24856/pset2-crack-vs-pointers
  13. // anushree:50xcIMJ0y.RXo = YES
  14. // brian:50mjprEcqC/ts = CA
  15. // bjbrown:50GApilQSG3E2 = UPenn (takes a while)
  16. // lloyd:50n0AAUD.pL8g = lloyd (takes a while)
  17. // malan:50CcfIk1QrPr6 = maybe (takes a while)
  18. // maria:509nVI8B9VfuA = TF
  19. // natmelo:50JIIyhDORqMU = nope
  20. // rob:50JGnXUgaafgc = ROFL
  21. // stelios:51u8F0dkeDSbY = NO
  22. // zamyla:50cI2vYkF0YU2 = LOL
  23.  
  24. int main(int argc, char *argv[])  // declaring char *argv[] here is telling the function to expect a char type input & a its gonna be a pointer to an array of characters (remember how these work?)
  25. {
  26.     if (argc != 2)  // prevent the user from not entering the correct number of command line arguments
  27.     {
  28.         printf("Invalid Hash Needed!:\n ");
  29.         return 1;
  30.     }
  31.     char *user_input;      // declare a pointer (it's own variable in memory) called user_input
  32.     user_input = argv[1];  // pointing to the array of characters, that the user inputted in each index of memory from arg[1]
  33.  
  34.     char salt[3] = {user_input[0], user_input[1], '\0'};  // declare an array of size 3. To store each of the two characters of the salt. The user input at the [0]th index of user_input & [1]st index of user_input + the null character at the end
  35.     string alphabet = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";  // declare an array of ALL OF THE test letters in the alphabet (a string is also an array of chars remember)
  36.     char guess[5];  // declare an array of size FIVE that will store our guesses spat out (X to XXXX)
  37.  
  38.     for (int i = 0; i < strlen(alphabet); i++)  // check your onenote for how these forloops work. This loops through 1 char (a-Z)
  39.     {
  40.         guess[0] = alphabet[i];
  41.         guess[1] = '\0';
  42.  
  43.         if (strcmp(crypt(guess, salt), argv[1]) == 0)  // check to compare the plaintext guess + salt & the hash in argv[1] with the strcmp() function AFTER it's encrypted into a hash with the crypt() function
  44.         {
  45.             printf("Password: %s\n", guess);  // print the guess on the last iteration of the loop if the above condition is true
  46.             return 0; // program is kill if the loop if above was successful
  47.         }
  48.     }
  49.  
  50.     for (int i = 0; i < strlen(alphabet); i++)  // check onenote for how these nested forloops work. This loops through 2 chars (a-ZZ)
  51.     {
  52.         guess[0] = alphabet[i];
  53.         for (int j = 0; j < strlen(alphabet); j++)
  54.         {
  55.             guess[1] = alphabet[j];
  56.             guess[2] = '\0';
  57.  
  58.             if (strcmp(crypt(guess, salt), argv[1]) == 0)  // check to compare the plaintext guess + salt & the hash in argv[1] with the strcmp() function AFTER it's encrypted into a hash with the crypt() function
  59.             {
  60.                 printf("Password: %s\n", guess);  // print the guess on the last iteration of the loop if the above condition is true
  61.                 return 0; // program is kill if the loop if above was successful
  62.             }
  63.         }
  64.     }
  65.  
  66.     for (int i = 0; i < strlen(alphabet); i++)  // check onenote for how thes enested forloops work. This loops through 3 chars (a-ZZZ)
  67.     {
  68.         guess[0] = alphabet[i];
  69.         for (int j = 0; j < strlen(alphabet); j++)
  70.         {
  71.             guess[1] = alphabet[j];
  72.             for (int k = 0; k < strlen(alphabet); k++)
  73.             {
  74.                 guess[2] = alphabet[k];
  75.                 guess[3] = '\0';
  76.  
  77.                 if (strcmp(crypt(guess, salt), argv[1]) == 0)  // check to compare the plaintext guess + salt & the hash in argv[1] with the strcmp() function AFTER it's encrypted into a hash with the crypt() function
  78.                 {
  79.                     printf("Password: %s\n", guess);  // print the guess on the last iteration of the loop if the above condition is true
  80.                     return 0; // program is kill if the loop if above was successful
  81.                 }
  82.             }
  83.         }
  84.     }
  85.  
  86.     for (int i = 0; i < strlen(alphabet); i++)  // check onenote for how these nested forloops work. This loops through 4 chars (a-ZZZZ)
  87.     {
  88.         guess[0] = alphabet[i];
  89.         for (int j = 0; j < strlen(alphabet); j++)
  90.         {
  91.             guess[1] = alphabet[j];
  92.             for (int k = 0; k < strlen(alphabet); k++)
  93.             {
  94.                 guess[2] = alphabet[k];
  95.                 for (int l = 0; l < strlen(alphabet); l++)
  96.                 {
  97.                     guess[3] = alphabet[l];
  98.                     guess[4] = '\0';
  99.  
  100.                     if (strcmp(crypt(guess, salt), argv[1]) == 0)  // check to compare the plaintext guess + salt & the hash in argv[1] with the strcmp() function AFTER it's encrypted into a hash with the crypt() function
  101.                     {
  102.                         printf("Password: %s\n", guess);  // print the guess on the last iteration of the loop if the above condition is true
  103.                         return 0; // program is kill if the loop if above was successful
  104.                     }
  105.                 }
  106.             }
  107.         }
  108.     }
  109.     printf("\n");
  110. }
Add Comment
Please, Sign In to add comment