Advertisement
Transformator

Untitled

May 25th, 2015
342
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.62 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. // BROADCAST
  10. #include <stdio.h>
  11. #include <sys/types.h>
  12. #include <sys/socket.h>
  13. #include <netinet/in.h>
  14. #include <arpa/inet.h>
  15. #include <netdb.h>
  16.  
  17. #define MAXBUF 65536
  18.  
  19. using namespace std;
  20.  
  21. class scht {
  22.     public:
  23.         string sk;
  24.         string pk;
  25.         vector <vector<string>> friends;
  26.  
  27.         vector <string> getFriend(int);
  28.         void addFriend(string, string);
  29.         void loadFriend(string, string);
  30.         void write(string, string, int);
  31.         scht();
  32.         ~scht();
  33.         string encrypt(string, int);
  34.         string decrypt(string, int);
  35.         vector<int> getHumanicKey(string);
  36.         string getCiperKey(vector<int>);
  37. };
  38.  
  39. void scht::addFriend(string name, string publickey) {
  40.     vector<int> pk_vector = getHumanicKey(publickey);
  41.     stringstream ss;
  42.  
  43.     for(int i=0; i<pk_vector.size(); i++) {
  44.         ss << pk_vector.at(i);
  45.         if(i != pk_vector.size()-1)
  46.             ss << ".";
  47.     }
  48.  
  49.     ofstream friendsFile("friends.csv", ios::app | ios::out);
  50.     friendsFile << name << ";" << ss.str() << endl;
  51.     friendsFile.close();
  52. }
  53.  
  54. vector<int> scht::getHumanicKey(string key) {
  55.     vector<int> hk;
  56.  
  57.     for(int i=0; i<key.size(); i++) {
  58.         hk.push_back(int(key[i]));
  59.     }
  60.  
  61.     return hk;
  62. }
  63.  
  64. string scht::getCiperKey(vector<int> key) {
  65.     char ck_char[key.size()];
  66.     for(int i=0; i<key.size(); i++) {
  67.         ck_char[i] = key.at(i);
  68.     }
  69.  
  70.     string ck = ck_char;
  71.     return ck;
  72. }
  73.  
  74. string scht::encrypt(string message, int id) {
  75.     string pk_friend = friends.at(id).at(1);
  76.  
  77.     string nonce = "ac9kjalsdkjflkasjdlfkjas";
  78.     string ciper;
  79.  
  80.     try { ciper = crypto_box(message, nonce, pk_friend, sk); }
  81.     catch(char const* er) { cout << er; }
  82.     catch(...) { cout << "error in encryption"; }
  83.     return ciper;
  84. }
  85.  
  86. string scht::decrypt(string ciper, int id) {
  87.     string pk_friend = friends.at(id).at(1);
  88.  
  89.     string nonce = "ac9kjalsdkjflkasjdlfkjas";
  90.     string message;
  91.  
  92.     try { message = crypto_box_open(ciper, nonce, pk_friend, sk); }
  93.     catch(char const* er) { cout << er; }
  94.     catch(...) { cout << "error in decryption"; }
  95.     return message;
  96. }
  97.  
  98. void scht::loadFriend(string name, string pk) {
  99.     vector<string> obj;
  100.     obj.push_back(name);
  101.     obj.push_back(pk);
  102.     friends.push_back(obj);
  103. }
  104.  
  105. void scht::write(string text, string title, int id) {
  106.     vector<string> contact = friends.at(id);
  107.  
  108.     time_t ts = time(nullptr);
  109.  
  110.     string timestamp = encrypt((string)asctime(localtime(&ts)), id);
  111.     string cipertext = encrypt(text, id);
  112.     string cipertitle = encrypt(title, id);
  113.  
  114.     /* BROADCAST */
  115.  
  116.     /*
  117.     int sock, status, buflen;
  118.     unsigned sinlen;
  119.     char buffer[MAXBUF];
  120.     struct sockaddr_in sock_in;
  121.     int yes = 1;
  122.  
  123.     sinlen = sizeof(struct sockaddr_in);
  124.  
  125.     sock_in.sin_addr.s_addr = htonl(INADDR_ANY);
  126.     sock_in.sin_port = htons(0);
  127.     sock_in.sin_family = PF_INET;
  128.  
  129.     status = bind(sock, (struct sockaddr *)&sock_in, sinlen);
  130.     printf("Bind Status: %d\n", status);
  131.  
  132.     status = getsockname(sock, (struct sockaddr *)&sock_in, &sinlen);
  133.     printf("Sock Port: %d\n", htons(sock_in.sin_port));
  134.  
  135.     buflen = MAXBUF;
  136.     memset(buffer, 0, buflen);
  137.     status = recvfrom(sock, buffer, buflen, 0, (struct sockaddr *)&sock_in, &sinlen);
  138.     printf("Sendto Status: %d\n", status);
  139.  
  140.     shutdown(sock, 2);
  141.     close(sock);
  142.     */
  143.  
  144.  
  145. }
  146.  
  147. scht::scht() {
  148.     ifstream friendsFile;
  149.     string line;
  150.     string arr[2];
  151.  
  152.     friendsFile.open("friends.csv", ios::in);
  153.  
  154.     if(friendsFile.is_open()) {
  155.         while(getline(friendsFile, line)) {
  156.             for (int i=0; i<line.length(); i++) {
  157.                 if (line[i] == ';') line[i] = ' ';
  158.             }
  159.  
  160.             vector<string> array;
  161.             stringstream ss(line);
  162.             string temp;
  163.             while (ss >> temp)
  164.             array.push_back(temp);
  165.             if(array.size() < 2)
  166.                 cout << endl << "array to long / to short! please check 'friends.csv' file!" << endl;
  167.  
  168.             for (int i=0; i<array.at(1).size(); i++) {
  169.                 if (array.at(1)[i] == '.') array.at(1)[i] = ' ';
  170.             }
  171.  
  172.             vector<int> pk_vector;
  173.             int n;
  174.  
  175.             istringstream is(array.at(1));
  176.  
  177.             while(is >> n) {
  178.                 pk_vector.push_back(n);
  179.             }
  180.  
  181.             loadFriend(array.at(0), getCiperKey(pk_vector));
  182.         }
  183.     }
  184.  
  185.     friendsFile.close();
  186.  
  187.     pk = crypto_box_keypair(&sk);
  188. }
  189.  
  190. scht::~scht() { cout << "deconstructor" << endl; }
  191.  
  192. int main() {
  193.     scht prog;
  194.  
  195.     prog.write("Hi Peter! Das ist ein Test.", "Hallo Peter", 0);
  196.     prog.write("Hi Tim!", "Hallo Tim", 2);
  197.  
  198.     //prog.addFriend("Tom", prog.pk);
  199.  
  200.     return 0;
  201. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement