nitrodog96

Password Cracker

Nov 6th, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.68 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <sstream>
  4. #include <stdlib.h>
  5. #include <thread>
  6. #include <cstdio>
  7. #include <ctime>
  8.  
  9. std::string password; //DO NOT ACCESS
  10. bool cracked = false;
  11. std::string legalChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890!@#$%^&*()_";
  12. std::string crackedPassword;
  13.  
  14. int countChar(std::string str, char c)
  15. {
  16.     int rtn = 0;
  17.     for(unsigned i = 0; i < str.length(); i++)
  18.     {
  19.         if(str[i] == c)
  20.         {
  21.             rtn++;
  22.         }
  23.     }
  24.     return rtn;
  25. }
  26.  
  27. std::string charToString(char c)
  28. {
  29.     std::stringstream ss;
  30.     std::string target;
  31.     ss << c;
  32.     ss >> target;
  33.     return target;
  34. }
  35.  
  36. int testFirstChar(char c)
  37. {
  38.     std::string toTest = charToString(c);
  39.     if(toTest == password)
  40.     {
  41.         cracked = true;
  42.         crackedPassword = toTest;
  43.         return 1;
  44.     }
  45.     else
  46.     {
  47.         bool doneTwo;
  48.         bool doneThr;
  49.         bool doneFou;
  50.         for(int i = 0; i < 73; i++)
  51.         {
  52.             doneTwo = false;
  53.             for(int j = 0; j < 73; j++)
  54.             {
  55.                 doneThr = false;
  56.                 for(int k = 0; k < 73; k++)
  57.                 {
  58.                     doneFou = false;
  59.                     for(int l = 0; l < 73; l++)
  60.                     {
  61.                         if(cracked)
  62.                         {
  63.                             return 1;
  64.                         }
  65.  
  66.                         toTest = charToString(c) + charToString((char) legalChars[i]);
  67.                         if(!doneTwo)
  68.                         {
  69.                             if(toTest == password)
  70.                             {
  71.                                 cracked = true;
  72.                                 crackedPassword = toTest;
  73.                                 return 1;
  74.                             }
  75.                             doneTwo = true;
  76.                         }
  77.  
  78.                         toTest += charToString((char) legalChars[j]);
  79.                         if(!doneThr)
  80.                         {
  81.                             if(toTest == password)
  82.                             {
  83.                                 cracked = true;
  84.                                 crackedPassword = toTest;
  85.                                 return 1;
  86.                             }
  87.                             doneThr = true;
  88.                         }
  89.  
  90.                         toTest += charToString((char) legalChars[k]);
  91.                         if(!doneFou)
  92.                         {
  93.                             if(toTest == password)
  94.                             {
  95.                                 cracked = true;
  96.                                 crackedPassword = toTest;
  97.                                 return 1;
  98.                             }
  99.                             doneFou = true;
  100.                         }
  101.  
  102.                         toTest += charToString((char) legalChars[l]);
  103.                         if(toTest == password)
  104.                         {
  105.                             cracked = true;
  106.                             crackedPassword = toTest;
  107.                             return 1;
  108.                         }
  109.                     }
  110.                 }
  111.             }
  112.         }
  113.     }
  114.     std::cout << "oh well" << std::endl;
  115. }
  116.  
  117. void testFirstChars(std::string str)
  118. {
  119.     for(unsigned i = 0; i < str.length(); i++)
  120.     {
  121.         if(!cracked)
  122.         {
  123.             int crack = testFirstChar((char) str[i]);
  124.             if(crack == 1)
  125.             {
  126.                 break;
  127.             }
  128.         }
  129.     }
  130. }
  131.  
  132. int main() {
  133.     std::clock_t start;
  134.     double duration;
  135.     start = std::clock();
  136.  
  137.     bool acceptPassword = true;
  138.     std::cout << "Please input a password. Maximum length is 5 characters. Alphanumeric characters and !@#$%^&*()_ only." << std::endl;
  139.     std::cin >> password;
  140.     if(password.length() > 5)
  141.     {
  142.         acceptPassword = false;
  143.     }
  144.     else
  145.     {
  146.         for(unsigned i = 0; i < password.length(); i++)
  147.         {
  148.             char cur = (char) password[i];
  149.             if(countChar(legalChars, cur) == 0)
  150.             {
  151.                 acceptPassword = false;
  152.             }
  153.         }
  154.     }
  155.     while(!acceptPassword)
  156.     {
  157.         acceptPassword = true;
  158.         std::cout << "Invalid password. Please try again." << std::endl;
  159.         std::cout << "Please input a password. Maximum length is 5 letters. Permitted characters: Letters (upper and lower case); !@#$%^&*()_" << std::endl;
  160.         std::cin >> password;
  161.         if(password.length() > 5)
  162.         {
  163.             acceptPassword = false;
  164.         }
  165.         else
  166.         {
  167.             for(unsigned i = 0; i < password.length(); i++)
  168.             {
  169.                 char cur = password[i];
  170.                 if(countChar(legalChars, cur) == 0)
  171.                 {
  172.                     acceptPassword = false;
  173.                 }
  174.             }
  175.         }
  176.     }
  177.     std::cout << "Working. Please wait for completion." << std::endl;
  178.  
  179.     std::thread t1(testFirstChars, "ABCDEFGH1");
  180.     std::thread t2(testFirstChars, "IJKLMNOP2");
  181.     std::thread t3(testFirstChars, "QRSTUVWX3");
  182.     std::thread t4(testFirstChars, "YZabcdef4");
  183.     std::thread t5(testFirstChars, "ghijklmn5");
  184.     std::thread t6(testFirstChars, "opqrstuv6");
  185.     std::thread t7(testFirstChars, "wxyz!@#$7");
  186.     std::thread t8(testFirstChars, "%^&*()_890");
  187.     t1.join();
  188.     t2.join();
  189.     t3.join();
  190.     t4.join();
  191.     t5.join();
  192.     t6.join();
  193.     t7.join();
  194.     t8.join();
  195.  
  196.     duration = (std::clock() - start) / ((double) CLOCKS_PER_SEC);
  197.     std::cout << "Cracked password: " + crackedPassword << std::endl;
  198.     std::cout << " in " + std::to_string(duration) + "ms." << std::endl;
  199.     return 0;
  200. }
Add Comment
Please, Sign In to add comment