Advertisement
Guest User

Untitled

a guest
Mar 30th, 2020
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.58 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <cmath>
  4.  
  5. using namespace std;
  6.  
  7. bool isPrime(int num)
  8. {
  9.     bool prime = true;
  10.     int root = sqrt(num) + 1;
  11.  
  12.     if(num <= 1)
  13.     {
  14.         prime = false;
  15.     }
  16.     else
  17.     {
  18.         for(int i = 2; i <= root; i++)
  19.         {
  20.             if(num % i == 0)
  21.             {
  22.                 prime = false;
  23.                 break;
  24.             }
  25.         }
  26.     }
  27.  
  28.     return(prime);
  29. }
  30.  
  31. void getPrimes(int *p, int *q)
  32. {
  33.     bool prime = false;
  34.  
  35.     while(!prime)
  36.     {
  37.         cout << "Enter a prime number: " << endl;
  38.         cin >> *p;
  39.  
  40.         prime = isPrime(*p);
  41.  
  42.         if(!prime)
  43.         {
  44.             cout << "That number is not prime." << endl;
  45.         }
  46.     }
  47.  
  48.     prime = false;
  49.  
  50.     while(!prime)
  51.     {
  52.         cout << "Enter another prime number: " << endl;
  53.         cin >> *q;
  54.  
  55.         prime = isPrime(*q);
  56.  
  57.         if(!prime)
  58.         {
  59.             cout << "That number is not prime." << endl;
  60.         }
  61.     }
  62.  
  63.     return;
  64. }
  65.  
  66. int gcd(int x, int y)
  67. {
  68.     int r = 0;
  69.  
  70.     do
  71.     {
  72.         r = x % y;
  73.         x = y;
  74.         if(r != 0)
  75.         {
  76.             y = r;
  77.         }
  78.     }
  79.     while(r != 0);
  80.  
  81.     return(y);
  82. }
  83.  
  84. int lcm(int x, int y)
  85. {
  86.     int result = 0;
  87.  
  88.     result = abs(x * y) / gcd(x, y);
  89.  
  90.     return(result);
  91. }
  92.  
  93. bool isCoprime(int e, int phi)
  94. {
  95.     bool coprime = false;
  96.  
  97.     if(e > 1 && e < phi && gcd(e, phi) == 1)
  98.     {
  99.         coprime = true;
  100.     }
  101.  
  102.     return(coprime);
  103. }
  104.  
  105. int getE(int phi)
  106. {
  107.     int e = 0;
  108.     bool coprime = false;
  109.  
  110.     while(!coprime)
  111.     {
  112.         cout << "Enter a value for e such that 1 < e < " << phi << ", and e and " << phi << " are coprime (65537 is recommended if " << phi << " > 65537): " << endl;
  113.         cin >> e;
  114.  
  115.         coprime = isCoprime(e, phi);
  116.  
  117.         if(!coprime)
  118.         {
  119.             cout << "That number is not coprime with " << phi << "." << endl;
  120.         }
  121.     }
  122.  
  123.     return(e);
  124. }
  125.  
  126. /*int getD(int e, int phi)
  127. {
  128.     int d = 0;
  129.  
  130.     while(((e * d) - 1) % phi != 0)
  131.     {
  132.         d++;
  133.     }
  134.  
  135.     return(d);
  136. }*/
  137.  
  138. int modInv(int e, int phi)
  139. {
  140.     int s = 0;
  141.     int old_s = 1;
  142.     int t = 1;
  143.     int old_t = 0;
  144.     int r = phi;
  145.     int old_r = e;
  146.     int quotient = 0;
  147.     int temp = 0;
  148.  
  149.     while(r != 0)
  150.     {
  151.         quotient = old_r / r;
  152.  
  153.         temp = r;
  154.         r = old_r - quotient * temp;
  155.         old_r = temp;
  156.  
  157.         temp = s;
  158.         s = old_s - quotient * temp;
  159.         old_s = temp;
  160.  
  161.         temp = t;
  162.         t = old_t - quotient * temp;
  163.         old_t = temp;
  164.     }
  165.  
  166.     //cout << endl << s << endl << old_s << endl << t << endl << old_t << endl << r << endl << old_r << endl << quotient << endl << temp << endl;
  167.  
  168.     return(old_s);
  169. }
  170.  
  171. void writeKeys(int n, int e, int d)
  172. {
  173.     ofstream publicKeyFile ("Public Key.txt");
  174.     publicKeyFile.clear();
  175.     publicKeyFile << "(" << n << ", " << e << ")" << endl;
  176.     publicKeyFile.close();
  177.  
  178.     ofstream privateKeyFile ("Private Key.txt");
  179.     privateKeyFile.clear();
  180.     privateKeyFile << "(" << n << ", " << d << ")" << endl;
  181.     privateKeyFile.close();
  182.  
  183.     cout << "Key pairs written successfully." << endl;
  184.  
  185.     return;
  186. }
  187.  
  188. int main(void)
  189. {
  190.     int p; //Prime 1
  191.     int q; //Prime 2
  192.     int n; //Modulus
  193.     int phi;
  194.     int e; //Public Exponent
  195.     int d; //Private Exponent
  196.  
  197.     getPrimes(&p, &q);
  198.  
  199.     n = p * q;
  200.  
  201.     phi = lcm((p - 1), (q - 1));
  202.  
  203.     e = getE(phi);
  204.  
  205.     d = phi - abs(modInv(e, phi));
  206.  
  207.     cout << "d: " << d << endl;
  208.  
  209.     writeKeys(n, e, d);
  210.  
  211.     return 0;
  212. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement