Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <fstream>
- #include <string>
- #include <sstream>
- #include <nacl/crypto_box.h>
- #include <ctime>
- using namespace std;
- class scht {
- public:
- string sk;
- string pk;
- vector < vector < string > > friends;
- vector <string> getFriend(int);
- void addFriend(string, string);
- void loadFriend(string, string);
- void write(string, string, int);
- scht();
- ~scht();
- string encrypt(string, int);
- string decrypt(string, int);
- };
- void scht::addFriend(string name, string publickey) {
- ofstream friendsFile("friends.csv", ios::app | ios::out);
- friendsFile << name << ";" << publickey;
- friendsFile.close();
- }
- string scht::encrypt(string message, int id) {
- string pk_friend = friends.at(id).at(1);
- string nonce = "ac9kjalsdkjflkasjdlfkjas";
- string ciper;
- try { ciper = crypto_box(message, nonce, pk_friend, sk); }
- catch(char const* er) { cout << er; }
- catch(...) { cout << "error in encryption"; }
- return ciper;
- }
- string scht::decrypt(string ciper, int id) {
- string pk_friend = friends.at(id).at(1);
- string nonce = "ac9kjalsdkjflkasjdlfkjas";
- string message;
- try { message = crypto_box_open(ciper, nonce, pk_friend, sk); }
- catch(char const* er) { cout << er; }
- catch(...) { cout << "error in decryption"; }
- return message;
- }
- void scht::loadFriend(string name, string pk) {
- vector<string> obj;
- obj.push_back(name);
- obj.push_back(pk);
- friends.push_back(obj);
- }
- void scht::write(string text, string title, int id) {
- vector<string> contact = friends.at(id);
- time_t ts = time(nullptr);
- string timestamp = encrypt((string)asctime(localtime(&ts)), id);
- string cipertext = encrypt(text, id);
- string cipertitle = encrypt(title, id);
- /* BROADCAST */
- }
- scht::scht() {
- ifstream friendsFile;
- string line;
- string arr[2];
- friendsFile.open("friends.csv", ios::in);
- if(friendsFile.is_open()) {
- while(getline(friendsFile, line)) {
- for (int i=0; i<line.length(); i++) {
- if (line[i] == ';') line[i] = ' ';
- }
- vector<string> array;
- stringstream ss(line);
- string temp;
- while (ss >> temp)
- array.push_back(temp);
- if(array.size() < 2)
- cout << endl << "array to long / to short! please check 'friends.csv' file!" << endl;
- loadFriend(array.at(0), array.at(1));
- }
- }
- friendsFile.close();
- pk = crypto_box_keypair(&sk);
- }
- scht::~scht() { cout << "deconstructor" << endl; }
- int main() {
- scht prog;
- prog.write("Hi Peter! Das ist ein Test.", "Hallo Peter", 0);
- prog.write("Hi Tim!", "Hallo Tim", 2);
- //prog.addFriend("Paul", prog.pk);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement