Advertisement
Guest User

Untitled

a guest
Sep 13th, 2018
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.92 KB | None | 0 0
  1. #define _XOPEN_SOURCE
  2. #define DOUBLE_ALPHA 52
  3. #include <cs50.h>
  4. #include <stdio.h>
  5. #include <string.h>
  6. #include <unistd.h>
  7.  
  8. int main(int argc, string argv[])
  9. {
  10.     // prompt user for hash password from the cmd line, limited to 1 argument only
  11.     if (argc != 2)
  12.     {
  13.         printf("Usage: ./crack hash\n");
  14.         return 1;
  15.     }
  16.  
  17.     // get "salt" for crypt() by retrieving the first two characters of argv[1]
  18.     char salt[3];
  19.     salt[0] = argv[1][0];
  20.     salt[1] = argv[1][1];
  21.  
  22.     //  password may have up to four characters in length
  23.     char password[5];
  24.     string alphabets = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
  25.  
  26.     // testing for password with one character length
  27.     for (int i = 0; i < DOUBLE_ALPHA; i++)
  28.     {
  29.         password[1] = '\0';
  30.         password[0] = alphabets[i];
  31.         string hash2 = crypt(password, salt);
  32.         if (strcmp(argv[1], hash2))
  33.         {
  34.             printf("Password: %s\n", password);
  35.             return 0;
  36.         }
  37.     }
  38.  
  39.     // testing for password with two characters length
  40.     for (int i = 0; i < DOUBLE_ALPHA; i++)
  41.     {
  42.         password[2] = '\0';
  43.         password[0] = alphabets[i];
  44.         for (int j = 0; j < DOUBLE_ALPHA; j++)
  45.         {
  46.             password[1] = alphabets[j];
  47.             string hash2 = crypt(password, salt);
  48.             if (strcmp(argv[1], hash2))
  49.             {
  50.                 printf("Password: %s\n", password);
  51.                 return 0;
  52.             }
  53.         }
  54.     }
  55.  
  56.     // testing for password with three characters length
  57.     for (int i = 0; i < DOUBLE_ALPHA; i++)
  58.     {
  59.         password[3] = '\0';
  60.         password[0] = alphabets[i];
  61.         for (int j = 0; j < DOUBLE_ALPHA; j++)
  62.         {
  63.             password[1] = alphabets[j];
  64.             for (int k = 0; k < DOUBLE_ALPHA; k++)
  65.             {
  66.                 password[2] = alphabets[k];
  67.                 string hash2 = crypt(password, salt);
  68.                 if (strcmp(argv[1], hash2))
  69.                 {
  70.                     printf("Password: %s\n", password);
  71.                     return 0;
  72.                 }
  73.             }
  74.         }
  75.     }
  76.  
  77.     // testing for password with four characters length
  78.     for (int i = 0; i < DOUBLE_ALPHA; i++)
  79.     {
  80.         password[4] = '\0';
  81.         password[0] = alphabets[i];
  82.         for (int j = 0; j < DOUBLE_ALPHA; j++)
  83.         {
  84.             password[1] = alphabets[j];
  85.             for (int k = 0; k < DOUBLE_ALPHA; k++)
  86.             {
  87.                 password[2] = alphabets[k];
  88.                 for (int l = 0; l < DOUBLE_ALPHA; l++)
  89.                 {
  90.                     password[3] = alphabets[l];
  91.                     string hash2 = crypt(password, salt);
  92.                     if (strcmp(argv[1], hash2))
  93.                     {
  94.                         printf("Password: %s\n", password);
  95.                         return 0;
  96.                     }
  97.                 }
  98.             }
  99.         }
  100.     }
  101. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement