What do researchers working at ICPC (Institute for Cryptographic Programming and Computing) do for fun? Well, as you probably have expected, in addition to solving algorithm-related problems on online judges, they also like to toy with various cryptographic schemes. Recently one of the researchers, Tom, has become interested in RSA algorithm implementations used in handheld devices.
Note that the description of the general RSA algorithm is as follows:
1) Choose two distinct prime numbers p and q, and let n = pq;
2) Choose an integer e such that gcd(e , (p-1)(q-1)) = 1;
3) Compute the integer d that satisfies the congruence relation
.
de ≡ 1 (mod (p-1)(q-1))
Then, if person A wants to give person B a way to send an encrypted message to him, A can follow the above steps and release (n, e) as his public key. Upon receiving A’s public key, B can simply encrypt message x (0 ≤ x < n) by computing y ≡ xe mod n . This would result in a message which ideally only A could decrypt with his private key d: x ≡ yd mod n .
As the computation power of handheld devices is usually limited, a relatively
small e is usually used to encrypt data. However this can lead to great security risks.
For example, it is quite simple to recover p and q (i.e., factor n) when you have both the public key (n, e) and the private key d. Could you help Tom write a program to demonstrate this?