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

ed1(modlcm(p1,q1))ed\equiv1\pmod{\text{lcm}(p-1,q-1)}

i.e. for somekZk\in\mathbb{Z},

ed=1+k(p1)(q1)ed=1+k(p-1)(q-1)

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

eda1=ka(p1)(q1)edb1=kb(p1)(r1)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(p1)(q1)k_a(p-1)(q-1) and kb(p1)(r1)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 (p1)(p-1). Let's call this α(p1)\alpha(p-1).

We can write the following code to obtain α(p1)\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

eda1p1=ka(q1)\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)dmodnp_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

Was this helpful?