Advertisement
artemgf

Взлом RSA

Jun 11th, 2018
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.97 KB | None | 0 0
  1. #include <iostream>
  2. #include <cmath>
  3. using namespace std;
  4. long long gcd (long long a, long long b, long long & x, long long & y) {
  5.     if (a == 0) {
  6.         x = 0; y = 1;
  7.         return b;
  8.     }
  9.     long long x1, y1;
  10.     long long d = gcd (b%a, a, x1, y1);
  11.     x = y1 - (b / a) * x1;
  12.     y = x1;
  13.     return d;
  14. }
  15.  
  16. long long phi (long long n) {
  17.     long long result = n;
  18.     for (int i=2; i*i<=n; ++i)
  19.         if (n % i == 0) {
  20.             while (n % i == 0)
  21.                 n /= i;
  22.             result -= result / i;
  23.         }
  24.     if (n > 1)
  25.         result -= result / n;
  26.     return result;
  27. }
  28. long long binpow (long long a, long long n, long long m) {
  29.     long long res = 1;
  30.     while (n) {
  31.         if (n & 1)
  32.             {
  33.         res *= a;
  34.         res%=m;
  35.       }
  36.         a *= a;
  37.     a%=m;
  38.         n >>= 1;
  39.     }
  40.     return res;
  41. }
  42.  
  43. int main() {
  44.   long long p;
  45.   cin >> p;
  46.   for(int i=1;i<=p;i++)
  47.   {
  48.   long long e, n, c;
  49.   cin >> e >> n >> c;
  50.   long long x, y;
  51.   long long m=phi(n);
  52.   long long  a= gcd(e,m,x,y);
  53.   x = (x % m + m) % m;
  54.   cout << binpow(c,x, n) << endl;
  55.   }
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement