Advertisement
Guest User

Untitled

a guest
Dec 13th, 2017
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.11 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <ctype.h>
  4. #include <cs50.h>
  5. #include <unistd.h>
  6. #include <crypt.h>
  7. #define _XOPEN_SOURCE
  8.  
  9. char *crackh(char *current, char *encrypt, char *salt, int i, int limit);
  10. char *crack(char *encrypt, char *salt, int len);
  11.  
  12. int main(int argsc, char *argsv[])
  13. {
  14. if (argsc != 2)
  15. {
  16. printf("Expected 1 command line argument");
  17. return 1;
  18. }
  19. char salt[3];
  20. salt[0] = argsv[1][0];
  21. salt[1] = argsv[1][1];
  22. salt[2] = '\0';
  23. //printf("");
  24. printf("%s\n", crack(argsv[1], salt, 4));
  25. return 0;
  26. }
  27.  
  28. char *crackh(char *s, char *encrypt, char *salt, int i, int limit)
  29. {
  30. for (char c = 'a'; c < 'z'; c++)
  31. {
  32. //Runs each letter twice; Uppercase and lowercase
  33. for (int u = 0; u < 2; u++)
  34. {
  35. if (u == 0)
  36. {
  37. s[i] = c;
  38. }
  39. else
  40. {
  41. s[i] = toupper(c);
  42. }
  43. //This means that it is looking at passwords of the correct length and should start testing them
  44. if (i == limit)
  45. {
  46. if (strcmp(crypt(s, salt), encrypt) == 0)
  47. {
  48. return s;
  49. }
  50. }
  51. //If you aren't at the correct length of password, go deeper
  52. else
  53. {
  54. char *plain = crackh(s, encrypt, salt, i + 1, limit);
  55. if (strcmp(plain, "") != 0)
  56. {
  57. return plain;
  58. }
  59. }
  60. }
  61. }
  62. return "";
  63. }
  64.  
  65. char *crack(char *encrypt, char *salt, int len)
  66. {
  67. char s[len + 2];
  68. //Calls crack for a 1 digit password, than a 2 digit password...
  69. for (int i = 0; i < len; i++)
  70. {
  71. //Sets \0 to be 1 after the last character that will be changed.
  72. s[i + 1] = '\0';
  73. char *plainText = crackh(s, encrypt, salt, 0, i);
  74. //If plaintext is "" crack couldn't find a password
  75. if (strcmp(plainText, "") != 0)
  76. {
  77. return plainText;
  78. }
  79. }
  80. return "";
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement