Topic Links:
http://poj.org/problem?id=2447
Main topic:
RSA is a well-known public key cryptography system. In this system, each participant has a private key that can only be known to himself and a person
Know the public key. In order to safely pass information to each other, the information should be encrypted with the public key, the other party with their own private key to decrypt.
The RSA system is described as follows:
First, select two large primes p, q, to calculate n = p * Q.
Then, select a positive integer E as the encryption key, make T = (p-1) * (q-1), and gcd (e,t) = 1.
Finally, the decryption key D is computed so that (E * D) mod t = 1, where d is the inverse of E-mode T.
The public key is represented as {e,n}, and the private key is discarded as {d,n},p and Q.
The process of encrypting data is
C = m^e MoD N
The process of decrypting the data is
M = c^d MoD N
M is a non-negative integer smaller than n becomes plaintext, and C is ciphertext.
Now the question is: give you ciphertext C and the public key {E,n}, can you find the clear text m?
Ideas:
Because n is a large integer, first using Pollardrho to decompose the number of n larger integers, to get a factor p, then another factor of element
Q = n/q, this gets two prime numbers p and Q. Then find t = (P-1) * (Q-1). And then use the extended Euclidean method to find out e about
The inverse of the modulus T D. According to the public key m = c^d MoD N, thus obtaining clear text.
AC Code:
#include <iostream> #include <algorithm> #include <ctime> #include <cmath> #include <cstdio > #include <cstring> #define LL long longusing namespace std; ll Mod_mul (ll x,ll y,ll mo) {ll t; x%= mo; for (t = 0; y; x = (x<<1)%mo,y>>=1) if (Y & 1) t = (t+x)%mo; return t;} ll Mod_mul (ll x,ll y,ll mo)//{//LL t,t,a,b,c,d,e,f,g,h,v,ans;//t = (ll) (sqrt (double (MO) +0.5));//t = T*t-mo ;//A = x/t;//b = x% t;//c = y/t;//d = y% t;//E = a*c/t;//f = a*c% t;//v = ((a*d+b*c)% Mo + e*t)% mo;//g = v/t;//h = v% t;//ans = (((f+g) *t%mo + b*d)% mo + h*t)%mo;//while (ans < 0)// Ans + = mo;//return ans;//}ll mod_exp (ll num,ll t,ll mo) {ll ret = 1, temp = num% mo; for (; t; t >>=1,temp=mod_mul (Temp,temp,mo)) if (T & 1) ret = Mod_mul (RET,TEMP,MO); return ret;} Pollarrho Large integer factorization ll gcd (ll A,ll b) {if (b = = 0) return A; ReTurn gcd (b, a% b);} ll Pollarrho (ll N, int c) {int i = 1; Srand (Time (NULL)); LL x = rand ()% (n-1) + 1; LL y = x; int k = 2; while (true) {i++; x = (Mod_exp (x,2,n) + c)% n; LL d = gcd (n+y-x,n); if (1 < d && D < n) return D; if (y = = x) return n; if (i = = k) {y = x; K *= 2; }}}void exgcd (LL a,ll b,ll &d,ll &x,ll &y) {if (b = = 0) {x = 1; y = 0; D = A; } else {EXGCD (b,a%b,d,y,x); Y-= x* (A/b); }}int Main () {LL p,q,t,c,e,n; while (CIN >> c >> e >> n) {p = Pollarrho (n,10007); Q = n/p; t = (p-1) * (q-1); LL d,x0,y0; EXGCD (E,T,D,X0,Y0); x0 = (x0%t + t)% T; cout << mod_exp (c,x0,n) << Endl; } return 0;}
POJ2447 RSA "Public Key Password"