Advertisement
Wazedrifat

RSA.cpp

Jun 9th, 2020
918
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.12 KB | None | 0 0
  1. //wazed RIfat
  2. #include <bits/stdc++.h>
  3. using namespace std;
  4.  
  5. #define IN freopen("in.txt", "r", stdin);
  6. #define OUT freopen("out.txt", "w", stdout);
  7. #define LL long long int
  8. #define MX 500001
  9. #define max3(a, b, c) max(a, max(b, c))
  10. #define min3(a, b, c) min(a, min(b, c))
  11. #define max4(a, b, c, d) max(a, max3(b, c, d))
  12. #define min4(a, b, c, d) min(a, min3(b, c, d))
  13. #define EPS 1e-9
  14. #define MOD 1000000007
  15. #define PI 2.0 * acos(0.0)
  16. #define PII pair <int, int>
  17. #define VI vector <int>
  18. #define VVI vector <VI>
  19.  
  20. bool flag[MX];
  21. vector<LL> prime;
  22. void SieveOfEratosthenes(LL limit = MX)
  23. {
  24.     prime.clear();
  25.     flag[0] = flag[1] = 1;
  26.  
  27.     prime.push_back(2);
  28.  
  29.     for (int i = 4; i <= limit; i += 2)
  30.         flag[i] = 1;
  31.  
  32.     for (int i = 3; i * i < limit; i += 2)
  33.         if (flag[i] == 0)
  34.             for (int j = i * i; j <= limit; j += 2 * i)
  35.                 flag[j] = 1;
  36.  
  37.     for (int i = 3; i <= limit; i += 2)
  38.         if (flag[i] == 0)
  39.             prime.push_back(i);
  40. }
  41.  
  42. //  Euler Phi Function : count of numbers <= N
  43. //  that are coPrime with N
  44. //  Number of elements e, such that gcd(e,n)=d is equal to ฯ•(nd).
  45. //  โˆ‘of (d/n)  [ ] = n.
  46. LL eulerPhi(LL n) {
  47.     LL res = n, sqrtn = sqrt(n);
  48.  
  49.     for (LL i = 0; i < prime.size() && prime[i] <= sqrtn; i++) {
  50.         if (n % prime[i] == 0) {
  51.             while (n % prime[i] == 0)
  52.                 n /= prime[i];
  53.  
  54.             sqrtn = sqrt(n);
  55.             res /= prime[i];
  56.             res *= prime[i] - 1;
  57.         }
  58.     }
  59.  
  60.     if (n != 1) {
  61.         res /= n;
  62.         res *= n - 1;
  63.     }
  64.     return res;
  65. }
  66.  
  67. //  returns (n^p) % mod
  68. LL bigMod(LL n, LL p, LL mod ) {
  69.     LL res = 1%mod, x = n%mod;
  70.  
  71.     while (p) {
  72.         if (p&1)
  73.             res = (res * x) % mod;
  74.        
  75.         x = (x * x) % mod;
  76.         p >>= 1;
  77.     }
  78.     return res;
  79. }
  80.  
  81. //  x = (1/a) % mod
  82. LL modInv(LL a, LL mod) {   //  mod is prime
  83.     return bigMod(a, mod - 2, mod);
  84. }
  85.  
  86. int ext_GCD(LL a, LL b, LL &X, LL &Y) {
  87.     LL x, y, x1, y1, x2, y2, r, r1, r2, q;
  88.     x1 = 0;     y1 = 1;
  89.     x2 = 1;     y2 = 0;
  90.     r1 = b;     r2 = a;
  91.  
  92.     for ( ; r1 != 0; ) {
  93.         q = r2 / r1;
  94.         r = r2 % r1;
  95.         x = x2 - (q * x1);
  96.         y = y2 - (q * x1);
  97.  
  98.         r2 = r1;
  99.         r1 = r;
  100.         x2 = x1;
  101.         y2 = y1;
  102.         x1 = x;
  103.         y1 = y;
  104.     }
  105.     X = x2;     Y = y2;
  106.     return r2;
  107. }
  108.  
  109. LL modInv2(LL a, LL mod) {  //  mod need not be prime
  110.     LL x, y;
  111.     ext_GCD(a, mod, x, y);
  112.  
  113.     x %= mod;
  114.     if (x < 0) 
  115.         x += mod;
  116.     return x;
  117. }
  118.  
  119. int main() {
  120.     IN OUT      //ThisIsForDebuggingPurposes
  121.    
  122.     SieveOfEratosthenes();
  123.  
  124.     LL publicKey, text, mod;
  125.  
  126.     // cout << "enter public text : ";
  127.     cin >> text;
  128.  
  129.     // cout << "enter public key : ";
  130.     cin >> publicKey;
  131.  
  132.     // cout << "enter mod : ";
  133.     cin >> mod;
  134.  
  135.     LL code = bigMod(text, publicKey, mod);
  136.     LL privateKey = modInv2(publicKey, eulerPhi(mod));
  137.    
  138.     cout << "text = " << text << endl;      //ThisIsForDebuggingPurposes
  139.     cout << "publicKey = " << publicKey << endl;        //ThisIsForDebuggingPurposes
  140.     cout << "mod = " << mod << endl;        //ThisIsForDebuggingPurposes
  141.     cout << endl << endl << endl;       //ThisIsForDebuggingPurposes
  142.  
  143.     cout << "code = " << code << endl;      //ThisIsForDebuggingPurposes
  144.     cout << "privateKey = " << privateKey << endl;      //ThisIsForDebuggingPurposes
  145.  
  146.     LL recoveredText = bigMod(code, privateKey, mod);
  147.     cout << "recoveredText = " << recoveredText << endl;        //ThisIsForDebuggingPurposes
  148. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement