Advertisement
Porr011

Lesson 9 activity 4

Feb 3rd, 2019
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.95 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <cstdio>
  4. #include <cstdlib>
  5. #include <ctime>
  6. using namespace std;
  7. //varibles and using char for all possible numbers and letters for pass code cracker
  8. char chars[]={'0','1','2','3','4','5','6','7','8','9','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'};
  9. string code;
  10. void checkPassword(string password);
  11. void recurse(int width, int position, string baseString);
  12.  // varibles for clock and starting the clock
  13.     clock_t start;
  14.     double duration;
  15. int  main() {
  16.     //starting the clock for how long it takes for the code to run
  17.     start = clock();
  18.     // asking user to enter code and stores it
  19.   cout << "Enter a 4 or 5 digit password: " << endl;
  20.   cin >> code;
  21.  
  22.   // setting limits for how many digits a user can enter
  23.   while (code.length() > 5)
  24.        {
  25.            cout << "Invalid password, input again" << endl;
  26.            cin >> code;
  27.        }
  28.        
  29.        while (code.length() < 4)
  30.        {
  31.            cout << "Invalid password, input again" << endl;
  32.            cin >> code;
  33.        }
  34.        // checks each digits width
  35.   int maxChars = 5;
  36.   for(int i=0;i<maxChars+1;i++) {
  37.     cout << "checking passwords width [" << i << "]..." << endl;
  38.     recurse(i,0,"");
  39.   }
  40.  
  41.   return 0;
  42. }
  43. //looking for the given digit by looking through the 36 possible we gave
  44. void recurse(int width, int position, string baseString) {
  45.   for(int i=0;i<36;i++) {
  46.     if (position < width-1) {
  47.       recurse(width, position + 1, baseString+chars[i]);
  48.     }
  49.     checkPassword(baseString+chars[i]);
  50.   }
  51. }
  52. // finding a match
  53. void checkPassword(string password) {
  54.   if (password==code) {
  55.     cout << "match [" << password << "]" << endl;
  56. //giving the time on how long it took
  57.     duration = (clock() - start) / (double)CLOCKS_PER_SEC;
  58.    
  59.     cout<< "TIME: It took this ammount of seconds to crack your code " << duration << '\n';
  60.     exit(1);
  61.   }
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement