Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- case 2:
- {
- //load keys for ENCRYPTION//
- ifstream loadpubkey;
- loadpubkey.open("PUBLICKEY.key");
- string PKn;
- string PKe;
- mpz_t PKn1;
- mpz_t PKe1;
- if (loadpubkey.fail())
- cout << "Public key does not exist";
- else
- {
- getline (loadpubkey,PKn);
- getline (loadpubkey,PKe);
- mpz_init_set_str (PKn1,PKn.c_str(),10);
- mpz_init_set_str (PKe1,PKe.c_str(),10);
- }
- loadpubkey.close();
- //read plaintext//
- std::ifstream in("plaintext.txt");
- std::stringstream buffer;
- string contents2;
- if (in.fail())
- cout << "plaintext.txt does not exist";
- else
- {
- buffer << in.rdbuf();
- std::string contents(buffer.str());
- contents2 = contents;
- } //Plaintext copied into contents2
- in.close();
- //encrypt//
- ofstream fileencrypted ("ENCRYPTED.txt");
- mpz_t charencrypt;
- mpz_init (charencrypt);
- mpz_t beforecharencrypt;
- mpz_init (beforecharencrypt);
- string inhex;
- //convert each char of plaintext into ascii hex and concatenate/
- for (int charcounter=0;charcounter < contents2.length();charcounter++)
- {
- int i = contents2[charcounter];
- stringstream s;
- string temp5;
- s << hex<<i;
- temp5 = s.str();
- inhex += temp5;
- }
- mpz_set_str (beforecharencrypt,inhex.c_str(),16);
- mpz_powm(charencrypt,beforecharencrypt,PKe1,PKn1); //encryption concatenation
- cout << hex<<inhex;
- fileencrypted <<hex<<charencrypt;
- }
- break;
- //decryption//
- case 3:
- {
- //load keys for DECRYPTION//
- ifstream loadprivkey;
- loadprivkey.open("PRIVATEKEY.key");
- string PrKn;
- string PrKd;
- mpz_t PrKn1;
- mpz_t PrKd1;
- if (loadprivkey.fail())
- cout << "Public key does not exist";
- else
- {
- getline (loadprivkey,PrKn);
- getline (loadprivkey,PrKd);
- mpz_init_set_str (PrKn1,PrKn.c_str(),10);
- mpz_init_set_str (PrKd1,PrKd.c_str(),10);
- }
- loadprivkey.close();
- // load ciphertext//
- ifstream loadctext ("ENCRYPTED.txt");
- ofstream decryptedfile ("DECRYPTED.txt");
- string ctext;
- getline (loadctext,ctext); //get ciphertext (stored as hex)
- mpz_t longctext;
- mpz_init_set_str (longctext,ctext.c_str(),16);
- mpz_powm(longctext,longctext,PrKd1,PrKn1); //decrypt
- char strlongctext[10000];
- mpz_get_str(strlongctext,16,longctext); //copy decrypted number into string
- string final (strlongctext);
- //divide number into pairs and output as ascii chars
- for (int count=0;count < final.length();count=count+2)
- {
- string temp2;
- temp2 +=final[count];
- temp2 +=final [count+1];
- int numout;
- istringstream (temp2)>>hex >> numout;
- char finalout = numout;
- decryptedfile << finalout;
- temp2.clear();
- }
- }
- break;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement