The text below is selected, press Ctrl+C to copy to your clipboard. (⌘+C on Mac) No line numbers will be copied.
Guest
Y
By Guest on 13th March 2023 07:45:03 AM | Syntax: TEXT | Views: 21



New paste | Download | Show/Hide line no. | Copy text to clipboard
  1. #include <iostream>
  2. #include <cmath>
  3.  
  4. using namespace std;
  5.  
  6. int gcd(int a, int b) {
  7.     if (a == 0)
  8.         return b;
  9.     return gcd(b % a, a);
  10. }
  11.  
  12. int modInverse(int a, int m) {
  13.     a = a % m;
  14.     for (int x = 1; x < m; x++)
  15.         if ((a * x) % m == 1)
  16.             return x;
  17.     return 1;
  18. }
  19.  
  20. int main() {
  21.     int p, q;
  22.     cout << "Enter prime numbers p and q: ";
  23.     cin >> p >> q;
  24.    
  25.     int n = p * q;
  26.     int phi = (p-1) * (q-1);
  27.    
  28.     int e;
  29.     for (e = 2; e < phi; e++) {
  30.         if (gcd(e, phi) == 1)
  31.             break;
  32.     }
  33.    
  34.     int d = modInverse(e, phi);
  35.    
  36.     cout << "Public key: (" << e << ", " << n << ")" << endl;
  37.     cout << "Private key: (" << d << ", " << n << ")" << endl;
  38.    
  39.     return 0;
  40. }



  • Recent Pastes