fork download
  1. #define _XOPEN_SOURCE
  2. //#include <cs50.h>
  3. #include <stdio.h>
  4. #include <string.h>
  5. #include <unistd.h>
  6. #include <crypt.h>
  7.  
  8. // brute force crack a DES encrypted password
  9. // algorithm will attempt combinations up to 4 chars long, using only alphabetic chars
  10.  
  11. // set highest int to convert to string
  12. const unsigned long long ALPHA_52_MAX = 7311616;
  13. // set characters to be used for cracking attempts
  14. char ALPHA_52[52] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
  15.  
  16. char itoa52(long long int_52_modulo);
  17. int Thingie(char* pdKey, unsigned int udKeySize, unsigned int * pudResultLen);
  18.  
  19. int main(int argc, char *argv[])
  20. {
  21. if (argc != 2)
  22. {
  23. // reject invalid parameters
  24. printf("Usage: ./crack hash\n");
  25. return 1;
  26. }
  27.  
  28. char salt[2] = {argv[1][0], argv[1][1]};
  29.  
  30. // iterate over all combinations of 4 alphabetic characters
  31. for (long long alpha_52_int = 0; alpha_52_int < ALPHA_52_MAX; alpha_52_int++)
  32. {
  33. char pdKey[4] = {0, 0, 0, 0};
  34. unsigned int udResultLen = 0;
  35.  
  36. if (0 == Thingie(
  37. pdKey,
  38. sizeof(pdKey) / sizeof(pdKey[0]),
  39. &udResultLen))
  40. {
  41. printf("error\n");
  42. return 1;
  43. }
  44.  
  45. if (!strcmp(crypt(pdKey, salt), argv[1]))
  46. {
  47. printf("%s\n", pdKey);
  48. return 0;
  49. }
  50. }
  51.  
  52. return 0;
  53. }
  54.  
  55. int Thingie(char* pdKey, unsigned int udKeySize, unsigned int * pudResultLen)
  56. {
  57. int digit_int_array[4] = {0, 0, 0, 0};
  58. int digit_position = udKeySize - 1, digit_count = 1;
  59. long long working_int = ALPHA_52_MAX;
  60.  
  61. if (0 == pudResultLen || 0 == pdKey || udKeySize > 4)
  62. {
  63. // need valid input!
  64. return 0;
  65. }
  66.  
  67. // always calculate the right most value
  68. digit_int_array[digit_position] = working_int % 52;
  69. while (working_int/52)
  70. {
  71. // working_int is divisible by 52, another digit is required to display characters
  72. ++digit_count;
  73. --digit_position;
  74. working_int /= 52;
  75. // value is assigned to correct position
  76. digit_int_array[digit_position] = working_int % 52;
  77. }
  78. *pudResultLen = digit_count; // set the resulting relevent length of the key
  79. // reset digit position
  80. digit_position = 3;
  81. while (digit_count > 0)
  82. {
  83. --digit_count;
  84. char current_char = itoa52(digit_int_array[digit_position]);
  85. // converted char is assigned to the correct position in the key
  86. pdKey[digit_count] = current_char;
  87. --digit_position;
  88. }
  89.  
  90. return 1;
  91. }
  92.  
  93. char itoa52(long long int_52_modulo)
  94. {
  95. return (ALPHA_52[int_52_modulo]);
  96. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
/home/ERKkD7/cc9ZKn6N.o: In function `main':
prog.c:(.text.startup+0x4b): undefined reference to `crypt'
collect2: error: ld returned 1 exit status
stdout
Standard output is empty