Advertisement
Transformator

Untitled

May 24th, 2015
239
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.68 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <fstream>
  4. #include <string>
  5. #include <sstream>
  6. #include <nacl/crypto_box.h>
  7. #include <ctime>
  8.  
  9. using namespace std;
  10.  
  11. class scht {
  12.     public:
  13.         string sk;
  14.         string pk;
  15.         vector < vector < string > > friends;
  16.  
  17.         vector <string> getFriend(int);
  18.         void addFriend(string, string);
  19.         void loadFriend(string, string);
  20.         void write(string, string, int);
  21.         scht();
  22.         ~scht();
  23.         string encrypt(string, int);
  24.         string decrypt(string, int);
  25. };
  26.  
  27. void scht::addFriend(string name, string publickey) {
  28.     ofstream friendsFile("friends.csv", ios::app | ios::out);
  29.     friendsFile << name << ";" << publickey;
  30.     friendsFile.close();
  31. }
  32.  
  33. string scht::encrypt(string message, int id) {
  34.     string pk_friend = friends.at(id).at(1);
  35.  
  36.     string nonce = "ac9kjalsdkjflkasjdlfkjas";
  37.     string ciper;
  38.  
  39.     try { ciper = crypto_box(message, nonce, pk_friend, sk); }
  40.     catch(char const* er) { cout << er; }
  41.     catch(...) { cout << "error in encryption"; }
  42.     return ciper;
  43. }
  44.  
  45. string scht::decrypt(string ciper, int id) {
  46.     string pk_friend = friends.at(id).at(1);
  47.  
  48.     string nonce = "ac9kjalsdkjflkasjdlfkjas";
  49.     string message;
  50.  
  51.     try { message = crypto_box_open(ciper, nonce, pk_friend, sk); }
  52.     catch(char const* er) { cout << er; }
  53.     catch(...) { cout << "error in decryption"; }
  54.     return message;
  55. }
  56.  
  57. void scht::loadFriend(string name, string pk) {
  58.     vector<string> obj;
  59.     obj.push_back(name);
  60.     obj.push_back(pk);
  61.     friends.push_back(obj);
  62. }
  63.  
  64. void scht::write(string text, string title, int id) {
  65.     vector<string> contact = friends.at(id);
  66.  
  67.     time_t ts = time(nullptr);
  68.  
  69.     string timestamp = encrypt((string)asctime(localtime(&ts)), id);
  70.     string cipertext = encrypt(text, id);
  71.     string cipertitle = encrypt(title, id);
  72.  
  73.     /* BROADCAST */
  74. }
  75.  
  76. scht::scht() {
  77.     ifstream friendsFile;
  78.     string line;
  79.     string arr[2];
  80.  
  81.     friendsFile.open("friends.csv", ios::in);
  82.  
  83.     if(friendsFile.is_open()) {
  84.         while(getline(friendsFile, line)) {
  85.             for (int i=0; i<line.length(); i++) {
  86.                 if (line[i] == ';') line[i] = ' ';
  87.             }
  88.  
  89.             vector<string> array;
  90.             stringstream ss(line);
  91.             string temp;
  92.             while (ss >> temp)
  93.             array.push_back(temp);
  94.             if(array.size() < 2)
  95.                 cout << endl << "array to long / to short! please check 'friends.csv' file!" << endl;
  96.             loadFriend(array.at(0), array.at(1));
  97.         }
  98.     }
  99.  
  100.     friendsFile.close();
  101.  
  102.     pk = crypto_box_keypair(&sk);
  103. }
  104.  
  105. scht::~scht() { cout << "deconstructor" << endl; }
  106.  
  107. int main() {
  108.     scht prog;
  109.  
  110.     prog.write("Hi Peter! Das ist ein Test.", "Hallo Peter", 0);
  111.     prog.write("Hi Tim!", "Hallo Tim", 2);
  112.  
  113.     //prog.addFriend("Paul", prog.pk);
  114.  
  115.     return 0;
  116. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement