Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <time.h>
- #include "finncrypt.h"
- using namespace std;
- /******************************************************
- * Proof of Concept - Encryption of Passwords by String
- * for Password Management
- *
- * Coder: kingfinn
- * Mail: kingfinn@hotmail.de
- * Date: 11. Oct '09
- *
- *****************************************************/
- bool getline(ifstream& file, string& line);
- inline void help()
- {
- cout << "Usage: " << "pw_manager" << "\n" <<
- "-p password : prints the Passwords [print]\n" <<
- "-a master_password usage username password: adds an entry to file [add]\n" <<
- "-g master_password pw_length username usage [generate]\n" <<
- "-h or --help : displays this help\n";
- }
- int main(int argc, char** argv)
- {
- if( argc < 2 )
- {
- help();
- return 1;
- }
- /* So we dont need the <cstring> to compare */
- string choice(argv[1]);
- /*****************************
- ***********PRINT*************
- *****************************/
- if(choice == "-p" && argc == 3)
- {
- /* Open file for reading, pointer at the end of it */
- ifstream file("pws.conf", ios::ate);
- /* Make sure file is valid */
- if(file.fail() || file.tellg() < 10)
- {
- cerr << "File is empty or doesn't exist!\n" <<
- "use '-a' to create it and add passwords\n";
- file.close();
- return 1;
- }
- /* Put the pointer at the beginning of the file */
- file.seekg(0, ios::beg);
- string line;
- string pw;
- string rest;
- while(getline(file, line))
- {
- int count = 0;
- pw.clear();
- rest.clear();
- for(string::iterator i = line.begin(); i != line.end(); i++)
- {
- if(count>=5)
- {
- pw += *i;
- continue;
- }
- else rest += *i;
- if(*i == ':') count++;
- }
- pw = finnage_decrypt(pw, string(argv[2]));
- cout << rest << pw << endl;
- }
- }
- /*****************************
- ********Add Passwords********
- *****************************/
- else if( choice == "-a" && argc == 6)
- {
- /* Open file for writing, pointer at the end of it */
- ofstream file("pws.conf", ios::out | ios::ate | ios::app);
- /* Replace Chars of the Password */
- string password = finnage_crypt(string(argv[5]),string(argv[2]));
- /* Write Username and Crypted Password to File */
- file << "Usage:" << argv[3] << ":Username:" << argv[4] << ":Password:" << password << endl;
- /* Close the file */
- file.close();
- }
- /*****************************
- *****Generate Passwords******
- *****************************/
- else if( choice == "-g" && argc == 6 )
- {
- /* Open file for writing, pointer at the end of it */
- ofstream file("pws.conf", ios::out | ios::ate | ios::app);
- /* The Password goes in here */
- string password;
- srand(time(NULL));
- /* Replace Chars of the Password */
- for(int i=1; i <= atoi(argv[3]); i++)
- {
- password += static_cast<char>(rand() % 93 +33);
- }
- cout << "Generated Password : " << password << endl;
- /* Crypt it */
- password = finnage_crypt(password, string(argv[2]));
- /* Write Username and Crypted Password to File */
- file << "Usage:" << argv[5] << ":Username:" << argv[4] << ":Password:" << password << endl;
- /* Close the file */
- file.close();
- }
- /*****************************
- ***********HELP**************
- *****************************/
- else if( choice == "--help" || choice == "-h" )
- {
- help();
- return 1;
- }
- else
- {
- help();
- return 1;
- }
- return 0;
- }
- /*********************************************
- * Just a little Function to read a line
- * of a file into a std::string
- ********************************************/
- bool getline(ifstream& file, string& line)
- {
- char tmp;
- line.clear();
- while(file.get(tmp))
- {
- if(tmp=='\n') return true;
- line+=tmp;
- }
- return false;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement