#P5451. [THUPC 2018] 密码学第三次小作业

[THUPC 2018] 密码学第三次小作业

Background

In 1977, Ronald Rivest, Adi Shamir, and Leonard Adleman proposed the RSA encryption algorithm. RSA is an asymmetric encryption algorithm, and its security depends on the difficulty of factoring very large integers. In other words, the harder it is to factor a very large integer, the more reliable RSA is. If someone finds a fast factoring algorithm, then the security of information encrypted with RSA will definitely drop a lot.

The basic idea of RSA is as follows:

  • Generating the public key and private key
    1. Randomly choose two different large primes pp and qq, and compute N=p×qN=p\times q.
    2. By the property of Euler's totient function, compute r=φ(N)=φ(p)φ(q)=(p1)(q1)r=\varphi (N)=\varphi (p)\varphi (q)=(p-1)(q-1).
    3. Choose a positive integer ee less than rr such that ee and rr are coprime. Then compute the multiplicative inverse dd of ee modulo rr, satisfying ed1(modr)ed\equiv 1 \pmod r.
    4. Destroy the records of pp and qq. At this point, (N,e)(N,e) is the public key, and (N,d)(N,d) is the private key.
  • Message encryption: First, convert the message mm into an integer nn that is less than NN and coprime with NN, in a format agreed upon by both parties. If the message is too long, it can be split into several parts (this is what we call block encryption). Then each part is encrypted using:
nec(modN)n^{e}\equiv c\pmod N
  • Message decryption: Decrypt using the key dd:
cdn(modN)c^{d}\equiv n\pmod N

Problem Description

Now there are two users who, by coincidence, have the same modulus NN but different private keys. Let their public keys be e1e_1 and e2e_2, and they are coprime. The plaintext message is mm, and the ciphertexts are:

$$\begin{matrix}c_1=m^{e_1}\bmod N\\c_2=m^{e_2}\bmod N\end{matrix}$$

Now, an attacker has intercepted c1c_1, c2c_2, e1e_1, e2e_2, and NN. Please help him recover the plaintext mm.

Input Format

The input contains multiple test cases. The first line contains an integer TT indicating the number of test cases, and it is guaranteed that 1T1041\le T\le 10^4 . Then each test case is described as follows:

  • One line contains five positive integers c1c_1, c2c_2, e1e_1, e2e_2, NN. It is guaranteed that 28<N<2632^{8}< N < 2^{63}, NN has exactly two prime factors, and the other values are generated strictly according to the RSA algorithm described above.

Output Format

For each test case, output 11 line:

  • A non-negative integer mm. Please make sure that 0m<N0\le m<N when outputting. It is guaranteed that the answer mm is coprime with NN.
1
1502992813 2511821915 653507 57809 2638352023
19260817

Hint

From the 2018 Tsinghua University Programming Contest and Intercollegiate Invitational (THUPC2018). Thanks to Pony.ai for supporting this contest.

Solutions and other resources can be found at https://github.com/wangyurzee7/THUPC2018.

Translated by ChatGPT 5