Advertisement
Guest User

Untitled

a guest
May 12th, 2020
46
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.27 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <fstream>
  4. #include <streambuf>
  5. #include <math.h>
  6. #include <vector>
  7.  
  8. #define varName(x) (#x)
  9.  
  10. using namespace std;
  11.  
  12. bool isPrime(int);
  13.  
  14. int calcE(int);
  15. int gcd(int, int);
  16. int calcD(int, int);
  17.  
  18. int encryption(int, int, int);
  19. int decryption(int, int, int);
  20.  
  21. template <class... Ts>
  22. void writeResult(ofstream &file, Ts const&... args);
  23.  
  24. int main() {
  25.     //Method data
  26.     int p, q, n, t, e, d;
  27.     bool primeFlag;
  28.  
  29.     string msg;
  30.     string encryptedText;
  31.     string decryptedText;
  32.  
  33.     ifstream textFile, valuesFile;
  34.  
  35.     //Opening input files
  36.     valuesFile.open("TP_92_Gorokhovska_mkr2.dat");
  37.     textFile.open("TP_92_Gorokhovska_mkr2.rsa");
  38.  
  39.     //Input p and q values
  40.     if (!valuesFile.is_open()) {
  41.         //cout << "Error!! There are not such .dat file!!" << endl;
  42.         cout << "Enter prime values for p and q: " << endl;
  43.  
  44.         //Input p value
  45.         cout << "p: ";
  46.         do {
  47.             cin >> p;
  48.  
  49.             if (!isPrime(p)) {
  50.                 cout << "Wrong input!! This number is not Prime!" << endl;
  51.                 cout << "Try again: ";
  52.             }
  53.         } while (!isPrime(p));
  54.  
  55.         //Input q value
  56.         cout << "q: ";
  57.         do {
  58.             cin >> q;
  59.  
  60.             if (!isPrime(q)) {
  61.                 cout << "Wrong input!! This number is not Prime!" << endl;
  62.                 cout << "Try again: ";
  63.             }
  64.         } while (!isPrime(q));
  65.     }
  66.     else {
  67.         cout << "Reading .dat file..." << endl;
  68.         valuesFile >> p;
  69.         valuesFile >> q;
  70.         cout << "Reading complete!" << endl;
  71.         cout << "Data from file: " << endl << endl;
  72.         cout << "p: " << p << endl;
  73.         cout << "q: " << q << endl;
  74.     }
  75.  
  76.     //Input coding text
  77.     if (!textFile.is_open()) {
  78.         //cout << "Error!! There are not such .rsa file!!" << endl;
  79.         cout << "Enter coding text: ";
  80.         cin >> msg;
  81.     }
  82.     else {
  83.         cout << "Reading .rsa file..." << endl;
  84.         textFile >> msg;
  85.         cout << "Reading complete!" << endl;
  86.         cout << "Data from file: " << endl;
  87.         cout << "Text: " << msg << endl;
  88.     }
  89.  
  90.     //Calculate modulus - n
  91.     n = p * q;
  92.     cout << "\nn: " << n << endl;
  93.  
  94.     //Calculate Euler totient function - t
  95.     t = (p - 1)*(q - 1);
  96.     cout << "t: " << t << endl << endl;
  97.  
  98.     //Calculation of the open exponent - e
  99.     e = calcE(t);
  100.  
  101.     //Calculation of the hidden exponent - d
  102.     d = calcD(e, t);
  103.  
  104.     //Public RSA key
  105.     cout << "Public RSA key: [e = " << e << ", n = " << n << "]" << endl;
  106.  
  107.     //Private RSA key
  108.     cout << "Private RSA key: [d = " << d << ", n = " << n << "]" << endl << endl;
  109.  
  110.     //Encryption
  111.     for (int i = 0; i < msg.length(); i++) {
  112.         encryptedText += (char)encryption(msg[i], e, n);
  113.     }
  114.    
  115.     //Decryption
  116.     for (int i = 0; i < msg.length(); i++) {
  117.         decryptedText += (char)decryption(encryptedText[i], d, n);
  118.     }
  119.  
  120.     //Result
  121.     cout << "Encrypted message: " << encryptedText << endl;
  122.     cout << "Decrypted message: " << decryptedText << endl;
  123.  
  124.     //Output info to file
  125.     ofstream outFile("TP_92_Gorokhovska_mkr2.txt");
  126.     writeResult(outFile, p, q, n, t, e, d, encryptedText, decryptedText);
  127.  
  128.  
  129.     system("pause");
  130.     return 0;
  131. }
  132.  
  133. //Check for prime number
  134. bool isPrime(int num) {
  135.     int j = sqrt((double)num);
  136.  
  137.     for (int i = 2; i <= j; i++) {
  138.         if (num % i == 0) {
  139.             return false;
  140.         }
  141.     }
  142.  
  143.     return true;
  144. }
  145.  
  146. //Calculation of the open exponent - e
  147. int calcE(int t) {
  148.     for (int e = 2; e < t; e++) {
  149.         if (gcd(e, t) == 1) return e;
  150.     }
  151.     return -1;
  152. }
  153.  
  154. int gcd(int e, int t) {
  155.     int temp;
  156.     while (e > 0) {
  157.         temp = e;
  158.         e = t % e;
  159.         t = temp;
  160.     }
  161.     return t;
  162. }
  163.  
  164. //Calculation of the hidden exponent - d
  165. int calcD(int e, int t) {
  166.     int d;
  167.     int k = 1;
  168.  
  169.     while (true) {
  170.         k += t;
  171.  
  172.         if (k%e == 0) {
  173.             d = k / e;
  174.             return d;
  175.         }
  176.     }
  177. }
  178.  
  179. //Encryption
  180. int encryption(int symbol, int e, int n) {
  181.     int current, result;
  182.  
  183.     current = symbol - 97;
  184.     result = 1;
  185.  
  186.     for (int i = 0; i < e; i++) {
  187.         result *= current;
  188.         result %= n;
  189.     }
  190.  
  191.     return result;
  192. }
  193.  
  194. //Decryption
  195. int decryption(int symbol, int d, int n) {
  196.     int current, result;
  197.  
  198.     current = symbol;
  199.     result = 1;
  200.  
  201.     for (int i = 0; i < d; i++) {
  202.         result *= current;
  203.         result %= n;
  204.     }
  205.  
  206.     return result + 97;
  207. }
  208.  
  209. //Write all info to file
  210. template <class T, class... Ts>
  211. void writeResult(ofstream &file, T const& first, Ts const&... args) {
  212.     /*using expander = int[];
  213.     (void)expander {
  214.         0, (void(file << args), 0)...
  215.     };*/
  216.  
  217.     file << varName(first) << ": " << first << endl;
  218.  
  219.     if constexpr (sizeof...(args) > 0) writeResult(file, args...);
  220. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement