Thursday, October 29, 2015

11 Year Old's Algorithm in C++

Why would you roll dice and do this simple task manually?  Just write a code to do it for you if you'd like to use this algorithm.

http://www.welivesecurity.com/2015/10/29/11-year-old-sets-cryptographically-secure-password-business/

//Version: 1.0.0
//Description: 11-year-old sets up cryptographically secure password business - http://www.welivesecurity.com/2015/10/29/11-year-old-sets-cryptographically-secure-password-business/
//To begin, a dice is rolled five times, with each numbered result documented.This then leaves you with a code, such as 12345. This result is already categorized in a dictionary, next to which is a word(which is ‘apathy’ in this instance).
//DiceYou then repeat this process six times and then band all the words together.You now have at your disposal an entirely bespoke and very long password that even the most powerful of computers will struggle to crack.
//Add a random character at the end and you’ve got an even stronger password, such as : apathy / sew / fungal / title / larch / maul / %.

//Required libraries
#include<iostream>      //cin, cout, endl
//#include<conio.h>       //_getch() that replaced the old getch()
#include<string>        //string
#include<fstream>       //ifstream
#include<Windows.h>     //Sleep(), srand(), rand()
#include<time.h>        //time()
#include<cmath>         //pow()


//Namespace specified to avoid using std:: with cin, cout, endl, ...
using namespace std;

int main(int argc, char* argv[]){
    ifstream inFile;
    inFile.open(argv[1]);
    int index,value;
    const int NUM_WORDS = 6, DIE_SIDES=6;
    string word,password;
    srand(time(NULL));
    char addChar = (rand() % 15) + 33;   //generate random ASCI special character to add to the end of the password

    if (!inFile){
        cout << "Input file is missing." << endl;
        return 1;
    }
    for (int i = 0; i < NUM_WORDS; i++){
        do{
            srand(time(NULL));
            value = ((rand() % DIE_SIDES) + 1)*pow(10, 4)
                + ((rand() % DIE_SIDES) + 1)*pow(10, 3)
                + ((rand() % DIE_SIDES) + 1)*pow(10, 2)
                + ((rand() % DIE_SIDES) + 1)*pow(10, 1)
                + ((rand() % DIE_SIDES) + 1);
            inFile >> index >> word;
            if (index == value){
                break;                 //stop looking if the generated value is found
            }//end of if value found
        } while (inFile);//end of while reading the file
        inFile.clear();
        inFile.seekg(0, ios::beg);    //reset the file to its beginning for the new search
        password.append(word);
        if (i == 5){password += addChar; break;}
        password += '/';
        Sleep(1000);                                //needs to sleep for a second to catch up with file operations
    }//end of for loop of generating 5 random words

    cout << "Your generated password is: " << password << endl;
    inFile.close();
    system("pause");           //Let the user determine when the application terminates
   //_getch();
    return 0;                 //return integer data type to OS to indicate errorlevel
}



No comments:

Post a Comment