Advertisement
Guest User

Untitled

a guest
May 6th, 2017
562
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.41 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <time.h>
  4.  
  5. #include "finncrypt.h"
  6.  
  7. using namespace std;
  8.  
  9. /******************************************************
  10.  * Proof of Concept - Encryption of Passwords by String
  11.  * for Password Management
  12.  *
  13.  * Coder: kingfinn
  14.  * Mail: kingfinn@hotmail.de
  15.  * Date: 11. Oct '09
  16.  *
  17.  *****************************************************/
  18.  
  19. bool getline(ifstream& file, string& line);
  20. inline void help()
  21. {
  22.     cout << "Usage: " << "pw_manager" << "\n" <<
  23.         "-p password : prints the Passwords [print]\n" <<
  24.         "-a master_password usage username password: adds an entry to file [add]\n" <<
  25.         "-g master_password pw_length username usage [generate]\n" <<
  26.         "-h or --help : displays this help\n";
  27. }
  28.  
  29.  
  30.  
  31.  
  32. int main(int argc, char** argv)
  33. {
  34.     if( argc < 2 )
  35.     {
  36.         help();
  37.         return 1;
  38.     }
  39.  
  40.  
  41.     /* So we dont need the <cstring> to compare */
  42.     string choice(argv[1]);
  43.  
  44.  
  45.     /*****************************
  46.      ***********PRINT*************
  47.      *****************************/
  48.     if(choice == "-p" && argc == 3)
  49.     {
  50.         /* Open file for reading, pointer at the end of it */
  51.         ifstream file("pws.conf", ios::ate);
  52.  
  53.  
  54.         /* Make sure file is valid */
  55.         if(file.fail() || file.tellg() < 10)
  56.         {
  57.             cerr << "File is empty or doesn't exist!\n" <<
  58.                 "use '-a' to create it and add passwords\n";
  59.             file.close();
  60.             return 1;
  61.         }
  62.  
  63.         /* Put the pointer at the beginning of the file */
  64.         file.seekg(0, ios::beg);
  65.  
  66.         string line;
  67.         string pw;
  68.         string rest;
  69.  
  70.         while(getline(file, line))
  71.         {
  72.             int count = 0;
  73.             pw.clear();
  74.             rest.clear();
  75.             for(string::iterator i = line.begin(); i != line.end(); i++)
  76.             {
  77.                 if(count>=5)
  78.                 {
  79.                     pw += *i;
  80.                     continue;
  81.                 }
  82.                 else rest += *i;
  83.                 if(*i == ':') count++;
  84.             }
  85.             pw = finnage_decrypt(pw, string(argv[2]));
  86.             cout << rest << pw << endl;
  87.         }
  88.  
  89.     }
  90.  
  91.  
  92.  
  93.     /*****************************
  94.      ********Add Passwords********
  95.      *****************************/
  96.  
  97.  
  98.     else if( choice == "-a" && argc == 6)
  99.     {
  100.         /* Open file for writing, pointer at the end of it */
  101.         ofstream file("pws.conf", ios::out | ios::ate | ios::app);
  102.  
  103.         /* Replace Chars of the Password */
  104.         string password = finnage_crypt(string(argv[5]),string(argv[2]));
  105.  
  106.         /* Write Username and Crypted Password to File */
  107.         file << "Usage:" << argv[3] << ":Username:" << argv[4] << ":Password:" << password << endl;
  108.  
  109.         /* Close the file */
  110.         file.close();
  111.     }
  112.  
  113.     /*****************************
  114.      *****Generate Passwords******
  115.      *****************************/
  116.  
  117.     else if( choice == "-g" && argc == 6 )
  118.     {
  119.         /* Open file for writing, pointer at the end of it */
  120.         ofstream file("pws.conf", ios::out | ios::ate | ios::app);
  121.  
  122.         /* The Password goes in here */
  123.         string password;
  124.  
  125.         srand(time(NULL));
  126.  
  127.         /* Replace Chars of the Password */
  128.         for(int i=1; i <= atoi(argv[3]); i++)
  129.         {
  130.             password += static_cast<char>(rand() % 93 +33);
  131.         }
  132.  
  133.         cout << "Generated Password : " << password << endl;
  134.  
  135.         /* Crypt it */
  136.         password = finnage_crypt(password, string(argv[2]));
  137.  
  138.         /* Write Username and Crypted Password to File */
  139.         file << "Usage:" << argv[5] << ":Username:" << argv[4] << ":Password:" << password << endl;
  140.  
  141.         /* Close the file */
  142.         file.close();
  143.     }
  144.  
  145.  
  146.  
  147.     /*****************************
  148.      ***********HELP**************
  149.      *****************************/
  150.  
  151.     else if( choice == "--help" || choice == "-h" )
  152.     {
  153.         help();
  154.         return 1;
  155.     }
  156.  
  157.     else
  158.     {
  159.         help();
  160.         return 1;
  161.     }
  162.  
  163.     return 0;
  164.  
  165. }
  166.  
  167.  
  168. /*********************************************
  169.  * Just a little Function to read a line
  170.  * of a file into a std::string
  171.  ********************************************/
  172.  
  173. bool getline(ifstream& file, string& line)
  174. {
  175.     char tmp;
  176.     line.clear();
  177.     while(file.get(tmp))
  178.     {
  179.         if(tmp=='\n') return true;
  180.         line+=tmp;
  181.     }
  182.     return false;
  183. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement