alice_bob_dave

Common factor in RSA modulus.

Problem

Alice and Bob are sending their flags to Dave. But sadly Dave lost the modulus :( Try to retrieve the flag!

Solution

We are given the following source code:

from Crypto.Util.number import *
from secret import msg_a,msg_b

e=65537
p,q,r=[getStrongPrime(1024,e) for _ in range(3)]
pt_a=bytes_to_long(msg_a)
pt_b=bytes_to_long(msg_b)

n_a=p*q
n_b=p*r
phin_a=(p-1)*(q-1)
phin_b=(p-1)*(r-1)
d_a=inverse(e,phin_a)
d_b=inverse(e,phin_b)

ct_a=pow(pt_a,e,n_a)
ct_b=pow(pt_b,e,n_b)

print(f"{ct_a=}\n{ct_b=}\n{d_a=}\n{d_b=}\n{e=}")

And the output:

Notice the way that the modulus is chosen. Three primes (p, q, and r) are chosen. The first modulus is na=pΓ—qn_a=p\times q , and the second is nb=pΓ—rn_b=p\times r.

Our first observation should be that both moduli share pp as a common factor.

Recall that in RSA, for n=pΓ—qn=p\times q, the private exponentdd is chosen such that

ed≑1(modlcm(pβˆ’1,qβˆ’1))ed\equiv1\pmod{\text{lcm}(p-1,q-1)}

i.e. for somek∈Zk\in\mathbb{Z},

ed=1+k(pβˆ’1)(qβˆ’1)ed=1+k(p-1)(q-1)

For na=pΓ—qn_a=p\times q and nb=pΓ—rn_b=p\times r,

edaβˆ’1=ka(pβˆ’1)(qβˆ’1)edbβˆ’1=kb(pβˆ’1)(rβˆ’1)ed_a-1=k_a(p-1)(q-1) \newline ed_b-1=k_b(p-1)(r-1)

Since we are given ee, dad_a and dbd_b, we know the values of ka(pβˆ’1)(qβˆ’1)k_a(p-1)(q-1) and kb(pβˆ’1)(rβˆ’1)k_b(p-1)(r-1). It is trivial to deduce that the greatest common divisor (GCD) between these two values is a multiple of (pβˆ’1)(p-1). Let's call this Ξ±(pβˆ’1)\alpha(p-1).

We can write the following code to obtain Ξ±(pβˆ’1)\alpha(p-1):

We could then iteratively test for the value of Ξ±\alpha by asserting that pp must be prime.

After finding pp, we simply note that

edaβˆ’1pβˆ’1=ka(qβˆ’1)\frac{ed_a-1}{p-1}=k_a(q-1)

and use a similar method to find qq. Then, we attempt to decode the ciphertext:

pa=(ca)dmod  np_a=(c_a)^d\mod{n}

The method to find rr and decode the second part of the flag is exactly the same.

Here's the output:

Last updated