Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <fstream>
- #include <streambuf>
- #include <math.h>
- #include <vector>
- #define varName(x) (#x)
- using namespace std;
- bool isPrime(int);
- int calcE(int);
- int gcd(int, int);
- int calcD(int, int);
- int encryption(int, int, int);
- int decryption(int, int, int);
- template <class... Ts>
- void writeResult(ofstream &file, Ts const&... args);
- int main() {
- //Method data
- int p, q, n, t, e, d;
- bool primeFlag;
- string msg;
- string encryptedText;
- string decryptedText;
- ifstream textFile, valuesFile;
- //Opening input files
- valuesFile.open("TP_92_Gorokhovska_mkr2.dat");
- textFile.open("TP_92_Gorokhovska_mkr2.rsa");
- //Input p and q values
- if (!valuesFile.is_open()) {
- //cout << "Error!! There are not such .dat file!!" << endl;
- cout << "Enter prime values for p and q: " << endl;
- //Input p value
- cout << "p: ";
- do {
- cin >> p;
- if (!isPrime(p)) {
- cout << "Wrong input!! This number is not Prime!" << endl;
- cout << "Try again: ";
- }
- } while (!isPrime(p));
- //Input q value
- cout << "q: ";
- do {
- cin >> q;
- if (!isPrime(q)) {
- cout << "Wrong input!! This number is not Prime!" << endl;
- cout << "Try again: ";
- }
- } while (!isPrime(q));
- }
- else {
- cout << "Reading .dat file..." << endl;
- valuesFile >> p;
- valuesFile >> q;
- cout << "Reading complete!" << endl;
- cout << "Data from file: " << endl << endl;
- cout << "p: " << p << endl;
- cout << "q: " << q << endl;
- }
- //Input coding text
- if (!textFile.is_open()) {
- //cout << "Error!! There are not such .rsa file!!" << endl;
- cout << "Enter coding text: ";
- cin >> msg;
- }
- else {
- cout << "Reading .rsa file..." << endl;
- textFile >> msg;
- cout << "Reading complete!" << endl;
- cout << "Data from file: " << endl;
- cout << "Text: " << msg << endl;
- }
- //Calculate modulus - n
- n = p * q;
- cout << "\nn: " << n << endl;
- //Calculate Euler totient function - t
- t = (p - 1)*(q - 1);
- cout << "t: " << t << endl << endl;
- //Calculation of the open exponent - e
- e = calcE(t);
- //Calculation of the hidden exponent - d
- d = calcD(e, t);
- //Public RSA key
- cout << "Public RSA key: [e = " << e << ", n = " << n << "]" << endl;
- //Private RSA key
- cout << "Private RSA key: [d = " << d << ", n = " << n << "]" << endl << endl;
- //Encryption
- for (int i = 0; i < msg.length(); i++) {
- encryptedText += (char)encryption(msg[i], e, n);
- }
- //Decryption
- for (int i = 0; i < msg.length(); i++) {
- decryptedText += (char)decryption(encryptedText[i], d, n);
- }
- //Result
- cout << "Encrypted message: " << encryptedText << endl;
- cout << "Decrypted message: " << decryptedText << endl;
- //Output info to file
- ofstream outFile("TP_92_Gorokhovska_mkr2.txt");
- writeResult(outFile, p, q, n, t, e, d, encryptedText, decryptedText);
- system("pause");
- return 0;
- }
- //Check for prime number
- bool isPrime(int num) {
- int j = sqrt((double)num);
- for (int i = 2; i <= j; i++) {
- if (num % i == 0) {
- return false;
- }
- }
- return true;
- }
- //Calculation of the open exponent - e
- int calcE(int t) {
- for (int e = 2; e < t; e++) {
- if (gcd(e, t) == 1) return e;
- }
- return -1;
- }
- int gcd(int e, int t) {
- int temp;
- while (e > 0) {
- temp = e;
- e = t % e;
- t = temp;
- }
- return t;
- }
- //Calculation of the hidden exponent - d
- int calcD(int e, int t) {
- int d;
- int k = 1;
- while (true) {
- k += t;
- if (k%e == 0) {
- d = k / e;
- return d;
- }
- }
- }
- //Encryption
- int encryption(int symbol, int e, int n) {
- int current, result;
- current = symbol - 97;
- result = 1;
- for (int i = 0; i < e; i++) {
- result *= current;
- result %= n;
- }
- return result;
- }
- //Decryption
- int decryption(int symbol, int d, int n) {
- int current, result;
- current = symbol;
- result = 1;
- for (int i = 0; i < d; i++) {
- result *= current;
- result %= n;
- }
- return result + 97;
- }
- //Write all info to file
- template <class T, class... Ts>
- void writeResult(ofstream &file, T const& first, Ts const&... args) {
- /*using expander = int[];
- (void)expander {
- 0, (void(file << args), 0)...
- };*/
- file << varName(first) << ": " << first << endl;
- if constexpr (sizeof...(args) > 0) writeResult(file, args...);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement