Advertisement
Guest User

rsa

a guest
Oct 11th, 2012
488
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.70 KB | None | 0 0
  1. case 2:
  2.     {
  3.     //load keys for ENCRYPTION//
  4.    
  5.     ifstream loadpubkey;
  6.     loadpubkey.open("PUBLICKEY.key");
  7.     string PKn;
  8.     string PKe;
  9.  
  10.     mpz_t PKn1;
  11.     mpz_t PKe1;
  12.  
  13.     if (loadpubkey.fail())
  14.         cout << "Public key does not exist";
  15.  
  16.     else
  17.     {
  18.         getline (loadpubkey,PKn);
  19.         getline (loadpubkey,PKe);
  20.  
  21.         mpz_init_set_str (PKn1,PKn.c_str(),10);
  22.         mpz_init_set_str (PKe1,PKe.c_str(),10);
  23.  
  24.     }
  25.  
  26.     loadpubkey.close();
  27.  
  28.  
  29.     //read plaintext//
  30.  
  31.     std::ifstream in("plaintext.txt");
  32.     std::stringstream buffer;
  33.     string contents2;
  34.    
  35.     if (in.fail())
  36.         cout << "plaintext.txt does not exist";
  37.    
  38.     else
  39.     {
  40.  
  41.         buffer << in.rdbuf();
  42.         std::string contents(buffer.str());
  43.         contents2 = contents;
  44.  
  45.  
  46.     }   //Plaintext copied into contents2
  47.        
  48.     in.close();
  49.  
  50.     //encrypt//
  51.     ofstream fileencrypted ("ENCRYPTED.txt");
  52.  
  53.     mpz_t charencrypt;
  54.     mpz_init (charencrypt);
  55.  
  56.     mpz_t beforecharencrypt;
  57.     mpz_init (beforecharencrypt);
  58.    
  59.     string inhex;  
  60.    
  61.     //convert each char of plaintext into ascii hex and concatenate/   
  62.  
  63.     for (int charcounter=0;charcounter < contents2.length();charcounter++)
  64.     {
  65.         int i = contents2[charcounter];
  66.         stringstream s;
  67.         string temp5;
  68.         s << hex<<i;
  69.         temp5 = s.str();
  70.         inhex += temp5;
  71.        
  72.     }
  73.  
  74.     mpz_set_str (beforecharencrypt,inhex.c_str(),16);
  75.     mpz_powm(charencrypt,beforecharencrypt,PKe1,PKn1);               //encryption concatenation
  76.     cout << hex<<inhex;
  77.     fileencrypted <<hex<<charencrypt;
  78.  
  79.    
  80.     }
  81.     break;
  82.    
  83.     //decryption//
  84.  
  85.     case 3:
  86.     {
  87.        
  88.         //load keys for DECRYPTION//
  89.  
  90.     ifstream loadprivkey;
  91.  
  92.     loadprivkey.open("PRIVATEKEY.key");
  93.     string PrKn;
  94.     string PrKd;
  95.  
  96.     mpz_t PrKn1;
  97.     mpz_t PrKd1;
  98.  
  99.     if (loadprivkey.fail())
  100.         cout << "Public key does not exist";
  101.  
  102.     else
  103.     {
  104.         getline (loadprivkey,PrKn);
  105.         getline (loadprivkey,PrKd);
  106.  
  107.         mpz_init_set_str (PrKn1,PrKn.c_str(),10);
  108.         mpz_init_set_str (PrKd1,PrKd.c_str(),10);
  109.  
  110.     }
  111.     loadprivkey.close();
  112.  
  113.  
  114.     // load ciphertext//
  115.  
  116.     ifstream loadctext ("ENCRYPTED.txt");
  117.     ofstream decryptedfile ("DECRYPTED.txt");  
  118.  
  119.     string ctext;
  120.     getline (loadctext,ctext);   //get ciphertext (stored as hex)
  121.    
  122.     mpz_t longctext;
  123.     mpz_init_set_str (longctext,ctext.c_str(),16);
  124.    
  125.  
  126.     mpz_powm(longctext,longctext,PrKd1,PrKn1);  //decrypt
  127.    
  128.     char strlongctext[10000];
  129.     mpz_get_str(strlongctext,16,longctext); //copy decrypted number into string
  130.  
  131.    
  132.     string final (strlongctext);
  133.    
  134.     //divide number into pairs and output as ascii chars   
  135.  
  136.     for (int count=0;count < final.length();count=count+2)
  137.     {
  138.         string temp2;
  139.         temp2 +=final[count];
  140.         temp2 +=final [count+1];
  141.         int numout;
  142.  
  143.         istringstream (temp2)>>hex >> numout;
  144.         char finalout = numout;
  145.         decryptedfile << finalout;
  146.         temp2.clear();
  147.        
  148.     }
  149.  
  150.     }
  151.  
  152.     break;
  153.    
  154.    
  155.     }  
  156.  
  157. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement