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>
- // BROADCAST
- #include <stdio.h>
- #include <sys/types.h>
- #include <sys/socket.h>
- #include <netinet/in.h>
- #include <arpa/inet.h>
- #include <netdb.h>
- #define MAXBUF 65536
- 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);
- vector<int> getHumanicKey(string);
- string getCiperKey(vector<int>);
- };
- void scht::addFriend(string name, string publickey) {
- vector<int> pk_vector = getHumanicKey(publickey);
- stringstream ss;
- for(int i=0; i<pk_vector.size(); i++) {
- ss << pk_vector.at(i);
- if(i != pk_vector.size()-1)
- ss << ".";
- }
- ofstream friendsFile("friends.csv", ios::app | ios::out);
- friendsFile << name << ";" << ss.str() << endl;
- friendsFile.close();
- }
- vector<int> scht::getHumanicKey(string key) {
- vector<int> hk;
- for(int i=0; i<key.size(); i++) {
- hk.push_back(int(key[i]));
- }
- return hk;
- }
- string scht::getCiperKey(vector<int> key) {
- char ck_char[key.size()];
- for(int i=0; i<key.size(); i++) {
- ck_char[i] = key.at(i);
- }
- string ck = ck_char;
- return ck;
- }
- 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 */
- /*
- int sock, status, buflen;
- unsigned sinlen;
- char buffer[MAXBUF];
- struct sockaddr_in sock_in;
- int yes = 1;
- sinlen = sizeof(struct sockaddr_in);
- sock_in.sin_addr.s_addr = htonl(INADDR_ANY);
- sock_in.sin_port = htons(0);
- sock_in.sin_family = PF_INET;
- status = bind(sock, (struct sockaddr *)&sock_in, sinlen);
- printf("Bind Status: %d\n", status);
- status = getsockname(sock, (struct sockaddr *)&sock_in, &sinlen);
- printf("Sock Port: %d\n", htons(sock_in.sin_port));
- buflen = MAXBUF;
- memset(buffer, 0, buflen);
- status = recvfrom(sock, buffer, buflen, 0, (struct sockaddr *)&sock_in, &sinlen);
- printf("Sendto Status: %d\n", status);
- shutdown(sock, 2);
- close(sock);
- */
- }
- 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;
- for (int i=0; i<array.at(1).size(); i++) {
- if (array.at(1)[i] == '.') array.at(1)[i] = ' ';
- }
- vector<int> pk_vector;
- int n;
- istringstream is(array.at(1));
- while(is >> n) {
- pk_vector.push_back(n);
- }
- loadFriend(array.at(0), getCiperKey(pk_vector));
- }
- }
- 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("Tom", prog.pk);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement