아직 만들고 있는 페이지입니다.

이 페이지는 아직 만드는 중입니다. 보이는 내용은 바뀔 수 있습니다.

Cracking RSA

면접 대비

시간 제한3초메모리 제한1024 MB

요약
작은 RSA 모듈러스를 두 소인수로 분해해 오일러 파이 함수를 구한 뒤, e의 모듈러 역원을 계산한다.
난이도

보통10점 중 4점

유형
정수론, 수학, 완전 탐색, 구현
정답자
아직 제출이 없습니다

문제

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 (p−1)(q−1)(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 a−b=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 Me mod nM^e \bmod n and sending it to the receiver. The recipient will calculate (Me)d≡Med≡Mkφ(n)+1≡Mφ(n)kM≡M mod n(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)≡1 mod nM^{\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 Me mod nM^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, (1≤T≤501 \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 2≤p,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.

예제1

  1. 예제 1

    입력
    2
    33 3
    65 11
    
    예상 출력
    7
    35