Advertisement
Guest User

Untitled

a guest
Aug 21st, 2019
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.95 KB | None | 0 0
  1. #include <cs50.h>
  2. #include <stdio.h>
  3. #include <crypt.h>
  4. #include <string.h>
  5.  
  6. bool crack(char *hash, char *guess, char *salt);
  7.  
  8. int main(int argc, string argv[])
  9. {
  10. ///argv[1] is hashed password input. First two characters are salt for crypt()
  11. if(argc != 2)
  12. {
  13. printf("Usage: ./crack hash\n");
  14. return 1;
  15. }
  16.  
  17. ///put user input into array "hash" and salt into array "salt"
  18. char *hash = argv[1];
  19. char salt[] = {hash[0], hash[1]};
  20.  
  21. ///establishes array alpha to iterate through given no non-alpha chars
  22. char alpha[52] ={'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'};
  23.  
  24. //iterates through alpha[] testing for single char passwords
  25. //iterate variables correspond to array locations (a, b, c, d, e)
  26. char pw1[2];
  27. for(int a1 = 0; a1 < 52; a1++)
  28. {
  29. pw1[0] = alpha[a1];
  30. pw1[1] = '\0';
  31.  
  32. if(crack(hash, pw1, salt))
  33. {
  34. printf("%s\n", pw1);
  35. return 0;
  36. }
  37. }
  38.  
  39. //iterates through alpha[] testing for two char passwords
  40. char pw2[3];
  41. for(int b1 = 0; b1 < 52; b1++)
  42. {
  43. for (int a1 = 0; a1 < 52; a1++)
  44. {
  45. pw2[0] = alpha[b1];
  46. pw2[1] = alpha[a1];
  47. pw2[2] = '\0';
  48.  
  49. if(crack(hash, pw2, salt))
  50. {
  51. printf("%s\n", pw2);
  52. return 0;
  53. }
  54. }
  55. }
  56.  
  57. //iterate through alpha testing for three char passwords
  58. char pw3[4];
  59. for(int c1 = 0; c1 < 52; c1++)
  60. {
  61. for(int b1 = 0; b1 < 52; b1++)
  62. {
  63. for(int a1 = 0; a1 < 52; a1++)
  64. {
  65. pw3[0] = alpha[c1];
  66. pw3[1] = alpha[b1];
  67. pw3[2] = alpha[a1];
  68. pw3[3] = '\0';
  69.  
  70. if(crack(hash, pw3, salt))
  71. {
  72. printf("%s\n", pw3);
  73. return 0;
  74. }
  75. }
  76. }
  77. }
  78.  
  79. //iterate through alpha testing for four char passwords
  80. char pw4[5];
  81. for(int d1 = 0; d1 < 52; d1++)
  82. {
  83. for(int c1 = 0; c1 < 52; c1++)
  84. {
  85. for(int b1 = 0; b1 < 52; b1++)
  86. {
  87. for(int a1 = 0; a1 < 52; a1++)
  88. {
  89. pw4[0] = alpha[d1];
  90. pw4[1] = alpha[c1];
  91. pw4[2] = alpha[b1];
  92. pw4[3] = alpha[a1];
  93. pw4[4] = '\0';
  94.  
  95. if(crack(hash, pw4, salt))
  96. {
  97. printf("%s\n", pw4);
  98. return 0;
  99. }
  100. }
  101. }
  102. }
  103. }
  104.  
  105. //iterate through alpha testing for five char passwords
  106. char pw5[6];
  107. for(int e1 = 0; e1 < 52; e1++)
  108. {
  109. for(int d1 = 0; d1 < 52; d1++)
  110. {
  111. for(int c1 = 0; c1 < 52; c1++)
  112. {
  113. for(int b1 = 0; b1 < 52; b1++)
  114. {
  115. for(int a1 = 0; a1 < 52; a1++)
  116. {
  117. pw5[0] = alpha[e1];
  118. pw5[1] = alpha[d1];
  119. pw5[2] = alpha[c1];
  120. pw5[3] = alpha[b1];
  121. pw5[4] = alpha[a1];
  122. pw5[5] = '\0';
  123.  
  124. if(crack(hash, pw5, salt))
  125. {
  126. printf("%s\n", pw5);
  127. return 0;
  128. }
  129. }
  130. }
  131. }
  132. }
  133. }
  134. }
  135.  
  136. ///boolean function for comparing crypt output to hash provided by user
  137. bool crack(char *hash, char *guess, char *salt)
  138. {
  139. char *encrypt_guess = crypt(guess, salt);
  140. if(strcmp(encrypt_guess, hash) == 0)
  141. {
  142. return true;
  143. }
  144. return false;
  145. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement