Advertisement
Transformator

Untitled

May 24th, 2015
287
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.65 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 = "ac988835";
  37.     string ciper;
  38.  
  39.     ciper = crypto_box(message, nonce, pk_friend, sk);
  40.     cout << ciper << endl; return ciper;
  41. }
  42.  
  43. string scht::decrypt(string ciper, int id) {
  44.     string pk_friend = friends.at(id).at(1);
  45.  
  46.     string nonce = "ac988835";
  47.     string message;
  48.  
  49.     message = crypto_box_open(ciper, nonce, pk_friend, sk);
  50.     cout << message << endl; return message;
  51. }
  52.  
  53. void scht::loadFriend(string name, string pk) {
  54.     vector<string> obj;
  55.     obj.push_back(name);
  56.     obj.push_back(pk);
  57.     friends.push_back(obj);
  58. }
  59.  
  60. void scht::write(string text, string title, int id) {
  61.     vector<string> contact = friends.at(id);
  62.  
  63.     time_t ts = time(nullptr);
  64.  
  65.     string timestamp = encrypt((string)asctime(localtime(&ts)), id);
  66.     string cipertext = encrypt(text, id);
  67.     string cipertitle = encrypt(title, id);
  68.  
  69.     /* BROADCAST */
  70. }
  71.  
  72. scht::scht() {
  73.     ifstream friendsFile;
  74.     string line;
  75.     string arr[2];
  76.  
  77.     friendsFile.open("friends.csv", ios::in);
  78.  
  79.     if(friendsFile.is_open()) {
  80.         while(getline(friendsFile, line)) {
  81.             for (int i=0; i<line.length(); i++) {
  82.                 if (line[i] == ';') line[i] = ' ';
  83.             }
  84.  
  85.             vector<string> array;
  86.             stringstream ss(line);
  87.             string temp;
  88.             while (ss >> temp)
  89.             array.push_back(temp);
  90.             if(array.size() < 2)
  91.                 cout << endl << "array to long / to short! please check 'friends.csv' file!" << endl;
  92.             loadFriend(array.at(0), array.at(1));
  93.         }
  94.     }
  95.  
  96.     friendsFile.close();
  97.  
  98.     pk = crypto_box_keypair(&sk);
  99. }
  100.  
  101. scht::~scht() { cout << "deconstructor" << endl; }
  102.  
  103. int main() {
  104.     scht prog;
  105.  
  106.     prog.write("Hi Peter! Das ist ein Test.", "Hallo Peter", 0);
  107.     prog.write("Hi Tom! Das ist ein Test mit dir.", "Hallo Tom", 1);
  108.  
  109. //    prog.addFriend("Tim", prog.pk);
  110.  
  111.     for(int i=0; i<prog.friends.size(); i++) {
  112.         cout << prog.friends.at(i).at(0) << " " << prog.friends.at(i).at(1) << endl;
  113.     }
  114.  
  115.     return 0;
  116. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement