Advertisement
agrippa1994

pwmgr

May 5th, 2014
218
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.10 KB | None | 0 0
  1. #include "AES256.h"
  2.  
  3. #include <iostream>
  4. #include <vector>
  5. #include <string>
  6. #include <sstream>
  7. #include <conio.h>
  8.  
  9. #include <fstream>
  10.  
  11. #include <boost/archive/text_oarchive.hpp>
  12. #include <boost/archive/text_iarchive.hpp>
  13. #include <boost/serialization/string.hpp>
  14. #include <boost/serialization/vector.hpp>
  15. #include <boost/lexical_cast.hpp>
  16.  
  17. struct stEntry{
  18.     std::string description;
  19.     std::string account;
  20.     std::string password;
  21. };
  22.  
  23. std::vector<stEntry> g_entries;
  24.  
  25. std::string g_key;
  26.  
  27. template<typename T> void serialize(T& ar, stEntry& e, const unsigned int version)
  28. {
  29.     ar & e.description;
  30.     ar & e.account;
  31.     ar & e.password;
  32. }
  33.  
  34. std::ostream& operator<<(std::ostream& os, stEntry& e)
  35. {
  36.     std::cout << e.description << '\t' << e.account << '\t' << e.password;
  37.     return os;
  38. }
  39.  
  40. std::string input(const std::string & text, bool passwd)
  41. {
  42.     std::cout << text;
  43.  
  44.     std::string retn;
  45.  
  46.     char ch = '\0';
  47.     while ((ch = _getch()) != 13)
  48.     {
  49.         std::cout << (char)((passwd) ? '*' : ch);
  50.         retn += ch;
  51.     }
  52.     std::cout << std::endl;
  53.     return retn;
  54. }
  55. void commandHandler(const std::string& str)
  56. {
  57.     if (str == "p") // print
  58.     {
  59.         int idx = 0;
  60.         for (auto & i : g_entries)
  61.             std::cout << (idx ++) << '\t' << i << std::endl;
  62.            
  63.     }
  64.     else if (str == "a") // add
  65.     {
  66.         stEntry e;
  67.         e.description = input("Enter description: ", false);
  68.         e.account = input("Enter nickname / email: ", false);
  69.         e.password = input("Enter password: ", true);
  70.  
  71.         g_entries.push_back(e);
  72.     }
  73.  
  74.     else if (str == "r") // remove
  75.     {
  76.         commandHandler("p");
  77.         std::cout << std::endl;
  78.  
  79.         try
  80.         {
  81.             auto idx = boost::lexical_cast<int>(input("Enter index, which should be removed: ", false));
  82.  
  83.             if (idx < 0 || idx >= g_entries.size())
  84.                 std::cout << "This index was invalid!" << std::endl;
  85.             else
  86.                 g_entries.erase(g_entries.begin() + idx);
  87.         }
  88.         catch (...)
  89.         {
  90.             std::cout << "This index was invalid!" << std::endl;
  91.         }
  92.        
  93.     }
  94.     else if (str == "w")
  95.     {
  96.         std::fstream fs("pw.db", std::ios_base::out);
  97.  
  98.         std::stringstream ss;
  99.         boost::archive::text_oarchive oa(ss);
  100.         oa << std::string("ok") << g_entries;
  101.  
  102.         fs << encode(g_key, ss.str());
  103.         fs.close();
  104.     }
  105.     else if (str == "q")
  106.         exit(0);
  107.     else if (str == "?")
  108.     {
  109.         std::cout << 'p' << '\t' << "Print all entries." << std::endl;
  110.         std::cout << 'a' << '\t' << "Append an entry." << std::endl;
  111.         std::cout << 'r' << '\t' << "Remove an entry." << std::endl;
  112.         std::cout << 'w' << '\t' << "Write the changes to the file." << std::endl;
  113.         std::cout << 'q' << '\t' << "Quit" << std::endl;
  114.         std::cout << '?' << '\t' << "Show this message." << std::endl;
  115.     }
  116.     else
  117.         std::cout << "Unknown Command, enter '?' to show basic commands" << std::endl;
  118. }
  119.  
  120.  
  121.  
  122. void waitForCommand()
  123. {
  124.     std::cout << "pwmgr> ";
  125.  
  126.     char szIn[256] = { 0 };
  127.     std::cin.getline(szIn, sizeof(szIn) -1);
  128.  
  129.     commandHandler(szIn);
  130. }
  131.  
  132. std::string input_aes256_key()
  133. {
  134.     std::cout << "Enter AES256 Key: ";
  135.  
  136.     std::string retn;
  137.     char ch = '\0';
  138.  
  139.     while ((ch = _getch()) != 13)
  140.     {
  141.         std::cout << '*';
  142.         retn += ch;
  143.     }
  144.  
  145.     std::cout << std::endl;
  146.     return retn;
  147. }
  148.  
  149. int main()
  150. {
  151.     std::cout << "Password-Manager" << std::endl;
  152.  
  153.     std::fstream fs("pw.db");
  154.     if (!fs.is_open())
  155.     {
  156.         std::cout << "Warning: The file \"pw.db\" couldn't be found!" << std::endl;
  157.     }
  158.  
  159.     g_key = input_aes256_key();
  160.    
  161.     if (fs.is_open())
  162.     {
  163.         std::string decrypted_filedata;
  164.  
  165.         try
  166.         {
  167.             std::stringstream ss;
  168.             ss << fs.rdbuf();
  169.            
  170.             decrypted_filedata =  decode(g_key, ss.str());
  171.         }
  172.         catch (...)
  173.         {
  174.             std::cout << "The file couldn't be decrypted!" << std::endl;
  175.  
  176.             std::cin.sync();
  177.             std::cin.get();
  178.             return 1;
  179.         }
  180.  
  181.         try
  182.         {
  183.             std::stringstream ss(decrypted_filedata);
  184.             boost::archive::text_iarchive ia(ss);
  185.  
  186.             std::string check;
  187.             ia >> check >> g_entries;
  188.  
  189.             if (check != "ok")
  190.             {
  191.                 std::cout << "The AES256 Key seems to be wrong..." << std::endl;
  192.             }
  193.         }
  194.         catch (...)
  195.         {
  196.             std::cout << "The file couldn't be serialized!" << std::endl;
  197.  
  198.             std::cin.sync();
  199.             std::cin.get();
  200.             return 1;
  201.         }
  202.     }
  203.    
  204.     fs.close();
  205.  
  206.     while (1)
  207.     {
  208.         waitForCommand();
  209.     }
  210. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement