Cracking RSA

아직 제출이 없습니다시간 제한3초메모리 제한1024 MB

문제

Image by Sean MacEntee (Flickr) [CC BY 2.0]

RSA is a widely used public-key cryptosystem that allows two parties (such as people or computers) to exchange secret messages without revealing information to anyone else listening on the conversation. Many websites use RSA when visited over a secure https connection. In RSA, each party has two different keys: a public key that is published and a private key that is kept secret. To encrypt a message intended for a specific recipient, the sender will use the recipient's public key to encrypt the message. The recipient will use their private key to decrypt the message.

An RSA public key (n,e)(n, e) consists of two numbers nn and ee. The number nn is a product of two distinct prime numbers, pp and qq. In real applications, nn would be hundreds of decimal digits long for security.

Let φ(n)\varphi(n) be Euler's totient function, which in this case is equal to (p1)(q1)(p-1) (q-1). The private key consists of (n,d)(n, d), where nn is the same as in the public key and dd is the solution to the congruence \[de \equiv 1 \bmod \varphi(n)\] Formally, a congruence \[a \equiv b \bmod c\] holds for three integers aa, bb, and cc, if there exists an integer kk such that ab=kca - b = k c.

The sender will encrypt a message MM (which, for simplicity, is assumed to be an integer smaller than both pp and qq) by computing MemodnM^e \bmod n and sending it to the receiver. The recipient will calculate (Me)dMedMkφ(n)+1Mφ(n)kMMmodn(M^e)^d \equiv M^{e d} \equiv M^{k \varphi(n) + 1} \equiv M^{\varphi(n) k} M \equiv M \bmod n since by Euler's theorem Mφ(n)1modnM^{\varphi(n)} \equiv 1 \bmod n. This will reconstruct the original message. Without the private key, no practical way has been found for a potential attacker to recover MM from the knowledge of MemodnM^e \bmod n and (n,e)(n, e).

Your task is to crack RSA by finding the private key related to a specific public key.

입력

The first line of input has the number of test cases TT, (1T501 \le T \le 50). Each test case has one line that contains the two numbers nn and ee. You may assume that nn is the product of two primes p,qp, q such that 2p,q<10002 \le p,q < 1000. Also, ee will be chosen so that dd exists and is unique, and 1<d,e<φ(n)1 < d, e < \varphi(n). Note that the product ded e may not fit into a 32-bit integer (e.g. Java's int type).

출력

For each test case, output the single number dd.