Guest User

Untitled

a guest
Feb 5th, 2019
297
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.11 KB | None | 0 0
  1. // Paul Fynn Kuemmel
  2. // This code gets a 4 digit code first, and then an 5 digit code. Then it uses brute force to crack the code
  3. // Then, once the code is cracked, it will output the code and say how much time it needed to crack the code.
  4. #include <stdio.h>
  5. #include <iostream>
  6. #include <cstdio>
  7. #include <ctime>
  8. #include <string>
  9. using namespace std;
  10. int main()
  11. {
  12.     clock_t start, start2;
  13.     double duration1, duration2; //These lines prepare all of the variables which will be used in the code.
  14.     string password, guess, useableCharacs;
  15.     duration1 = 0;
  16.     duration2 = 0;
  17.     start = clock(); //Line prepares the clock to start
  18.     useableCharacs = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890";
  19.     cout << "Please enter your 4 digit Password: "; // Instead of using an array, I created a list of all the possible characters
  20.     cin >> password; // Gets the password
  21.     while ((password.length() != 5) and (password.length() != 4)) {
  22.         cout << "The password is not either 4 or 5 characters!";
  23.         cout << "Please enter the password again: ";
  24.         cin >> password;
  25.     }
  26.  
  27.     guess = "";
  28.     if (password.length() == 4) {
  29.         guess = "    ";
  30.     }
  31.     else {
  32.         guess = "     "; //Creates a space where the characters will be placed
  33.     }
  34.     for (int digit1 = 0; digit1 < useableCharacs.length(); digit1++) { // Adds one so that it cycles through all of the available characters
  35.         guess[0] = useableCharacs[digit1]; //Takes the first digit of the guess, and stops the loop once it equals to the digit
  36.         for (int digit2 = 0; digit2 < useableCharacs.length(); digit2++) { // Adds one so that it cycles through all of the available characters
  37.             guess[1] = useableCharacs[digit2];//Takes the second digit of the guess, and stops the loop once it equals to the digit
  38.             for (int digit3 = 0; digit3 < useableCharacs.length(); digit3++) { // Adds one so that it cycles through all of the available characters
  39.                 guess[2] = useableCharacs[digit3];//Takes the third digit of the guess, and stops the loop once it equals to the digit
  40.                 for (int digit4 = 0; digit4 < useableCharacs.length(); digit4++) { // Adds one so that it cycles through all of the available characters
  41.                     guess[3] = useableCharacs[digit4];//Takes the fourth digit of the guess, and stops the loop once it equals to the digit
  42.                     if (guess == password) {//Outputs the guess once it is right
  43.                         cout << "Your password is: " << guess << endl;
  44.                         duration1 = (clock() - start) / (double)CLOCKS_PER_SEC;
  45.                         cout << "The time taken to crack the code is: " << duration1 << " seconds." << endl;
  46.                     }
  47.  
  48.                 }
  49.  
  50.             }
  51.         }
  52.     }
  53.     start = clock();
  54.     cout << "Now enter a five digit password: ";
  55.     cin >> password;
  56.     while (password.length() != 5) {
  57.         cout << "That is not a five digit password: ";
  58.         cout << "Please enter a five digit password: ";
  59.         cin >> password;
  60.     }
  61.     guess = "";
  62.     if (password.length() == 4) {
  63.         guess = "    ";
  64.     }
  65.     else {
  66.         guess = "     "; //Creates a space where the characters will be placed
  67.     }
  68.     for (int digit1 = 0; digit1 < useableCharacs.length(); digit1++) { // Adds one so that it cycles through all of the available characters
  69.         guess[0] = useableCharacs[digit1]; //Takes the first digit of the guess, and stops the loop once it equals to the digit
  70.         for (int digit2 = 0; digit2 < useableCharacs.length(); digit2++) { // Adds one so that it cycles through all of the available characters
  71.             guess[1] = useableCharacs[digit2];//Takes the second digit of the guess, and stops the loop once it equals to the digit
  72.             for (int digit3 = 0; digit3 < useableCharacs.length(); digit3++) { // Adds one so that it cycles through all of the available characters
  73.                 guess[2] = useableCharacs[digit3];//Takes the third digit of the guess, and stops the loop once it equals to the digit
  74.                 for (int digit4 = 0; digit4 < useableCharacs.length(); digit4++) { // Adds one so that it cycles through all of the available characters
  75.                     guess[3] = useableCharacs[digit4];//Takes the fourth digit of the guess, and stops the loop once it equals to the digit
  76.                     if (guess.length() == 5) { //If the value is 5 it runs again
  77.                         for (int digit5 = 0; digit5 < useableCharacs.length(); digit5++) { // Adds one so that it cycles through all of the available characters
  78.                             guess[4] = useableCharacs[digit5];
  79.                             if (guess == password) { //Outputs the guess once it is right
  80.                                 cout << "Your password is: " << guess << endl;
  81.                                 duration2 = (clock() - start) / (double)CLOCKS_PER_SEC;
  82.                                 cout << "The time taken to crack the code is: " << duration2 << " seconds." << endl;
  83.                             }
  84.                         }
  85.  
  86.                     }
  87.                     else {
  88.                         if (guess == password) {//Outputs the guess once it is right
  89.                             cout << "Your password is: " << guess << endl;
  90.                             duration2 = (clock() - start) / (double)CLOCKS_PER_SEC;
  91.                             cout << "The time taken to crack the code is: " << duration2 << " seconds." << endl;
  92.                         }
  93.  
  94.                     }
  95.  
  96.                 }
  97.             }
  98.         }
  99.  
  100.         cout << "The difference between a 4 digit password and a 5 digit password is: " << (duration2 - duration1) << " seconds." << endl;
  101.         cout << "A five digit code takes " << duration2 / duration1 << " times longer to crack. ";
  102.         return 0;
  103.     }
  104. }
Advertisement
Add Comment
Please, Sign In to add comment