Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Paul Fynn Kuemmel
- // This code gets a 4 digit code first, and then an 5 digit code. Then it uses brute force to crack the code
- // Then, once the code is cracked, it will output the code and say how much time it needed to crack the code.
- #include <stdio.h>
- #include <iostream>
- #include <cstdio>
- #include <ctime>
- #include <string>
- using namespace std;
- int main()
- {
- clock_t start, start2;
- double duration1, duration2; //These lines prepare all of the variables which will be used in the code.
- string password, guess, useableCharacs;
- duration1 = 0;
- duration2 = 0;
- start = clock(); //Line prepares the clock to start
- useableCharacs = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890";
- cout << "Please enter your 4 digit Password: "; // Instead of using an array, I created a list of all the possible characters
- cin >> password; // Gets the password
- while ((password.length() != 5) and (password.length() != 4)) {
- cout << "The password is not either 4 or 5 characters!";
- cout << "Please enter the password again: ";
- cin >> password;
- }
- guess = "";
- if (password.length() == 4) {
- guess = " ";
- }
- else {
- guess = " "; //Creates a space where the characters will be placed
- }
- for (int digit1 = 0; digit1 < useableCharacs.length(); digit1++) { // Adds one so that it cycles through all of the available characters
- guess[0] = useableCharacs[digit1]; //Takes the first digit of the guess, and stops the loop once it equals to the digit
- for (int digit2 = 0; digit2 < useableCharacs.length(); digit2++) { // Adds one so that it cycles through all of the available characters
- guess[1] = useableCharacs[digit2];//Takes the second digit of the guess, and stops the loop once it equals to the digit
- for (int digit3 = 0; digit3 < useableCharacs.length(); digit3++) { // Adds one so that it cycles through all of the available characters
- guess[2] = useableCharacs[digit3];//Takes the third digit of the guess, and stops the loop once it equals to the digit
- for (int digit4 = 0; digit4 < useableCharacs.length(); digit4++) { // Adds one so that it cycles through all of the available characters
- guess[3] = useableCharacs[digit4];//Takes the fourth digit of the guess, and stops the loop once it equals to the digit
- if (guess == password) {//Outputs the guess once it is right
- cout << "Your password is: " << guess << endl;
- duration1 = (clock() - start) / (double)CLOCKS_PER_SEC;
- cout << "The time taken to crack the code is: " << duration1 << " seconds." << endl;
- }
- }
- }
- }
- }
- start = clock();
- cout << "Now enter a five digit password: ";
- cin >> password;
- while (password.length() != 5) {
- cout << "That is not a five digit password: ";
- cout << "Please enter a five digit password: ";
- cin >> password;
- }
- guess = "";
- if (password.length() == 4) {
- guess = " ";
- }
- else {
- guess = " "; //Creates a space where the characters will be placed
- }
- for (int digit1 = 0; digit1 < useableCharacs.length(); digit1++) { // Adds one so that it cycles through all of the available characters
- guess[0] = useableCharacs[digit1]; //Takes the first digit of the guess, and stops the loop once it equals to the digit
- for (int digit2 = 0; digit2 < useableCharacs.length(); digit2++) { // Adds one so that it cycles through all of the available characters
- guess[1] = useableCharacs[digit2];//Takes the second digit of the guess, and stops the loop once it equals to the digit
- for (int digit3 = 0; digit3 < useableCharacs.length(); digit3++) { // Adds one so that it cycles through all of the available characters
- guess[2] = useableCharacs[digit3];//Takes the third digit of the guess, and stops the loop once it equals to the digit
- for (int digit4 = 0; digit4 < useableCharacs.length(); digit4++) { // Adds one so that it cycles through all of the available characters
- guess[3] = useableCharacs[digit4];//Takes the fourth digit of the guess, and stops the loop once it equals to the digit
- if (guess.length() == 5) { //If the value is 5 it runs again
- for (int digit5 = 0; digit5 < useableCharacs.length(); digit5++) { // Adds one so that it cycles through all of the available characters
- guess[4] = useableCharacs[digit5];
- if (guess == password) { //Outputs the guess once it is right
- cout << "Your password is: " << guess << endl;
- duration2 = (clock() - start) / (double)CLOCKS_PER_SEC;
- cout << "The time taken to crack the code is: " << duration2 << " seconds." << endl;
- }
- }
- }
- else {
- if (guess == password) {//Outputs the guess once it is right
- cout << "Your password is: " << guess << endl;
- duration2 = (clock() - start) / (double)CLOCKS_PER_SEC;
- cout << "The time taken to crack the code is: " << duration2 << " seconds." << endl;
- }
- }
- }
- }
- }
- cout << "The difference between a 4 digit password and a 5 digit password is: " << (duration2 - duration1) << " seconds." << endl;
- cout << "A five digit code takes " << duration2 / duration1 << " times longer to crack. ";
- return 0;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment