Common Modulus Attack is used when the same plaintext m is encrypted twice with the same modulus N but distinct public exponent e1 and e2. In other words:
c1≡me1modNc2≡me2modN
If e1 and e2 are coprime, then Bezout's Identity tells us that ∃a,b∈Z s.t.
a⋅e1+b⋅e2=1
Here a and b can be solved using extended Euclidean Algorithm. Once we know a and b, we could compute
m≡c1a+c2bmodN
Correctness:
c1a+c2b≡(me1)a⋅(me2)b≡me1⋅a+e2⋅b≡m1≡mmodN
Implementation
#!/usr/bin/env python3from Crypto.Util.number import inverse, long_to_bytes, bytes_to_longfrom Crypto.PublicKey import RSAfrom sympy import gcdexfrom sys importexit#--------Data--------#N =<N>e1 =<e1>e2 =<e2>c1 =<c1>c2 =<c2>#--------Common modulus--------#a, b, gcd =gcdex(e1, e2)a =int(a)b =int(b)# Test if e1 and e2 are coprimeif gcd !=1:print("e1 and e2 must be coprime")exit()m = (pow(c1, a, N)*pow(c2, b, N)) % Nflag =long_to_bytes(m)print(flag)
Hastad Broadcast Attack
Theory
Hastad Broadcast Attack is used when the same plaintext m is encrypted multiple (≥3) times with the same public exponents e but distinct modulus Ni. In other words:
c1≡memodN1c2≡memodN2...cn≡memodNn
Chinese Remainder Theorem tells us the solution to the following equation exists:
ci≡memodi=1∏nNi
Denote this solution as M. Chinese Remainder Theorem also provides a method for computing M:
M≡i=1∑eci⋅ui⋅NimodN
where N=∏i=1nNi. Note that the plaintext m<Ni, hence me<∏i=1nNi=N. As a result, taking e-th root of M gives us the plaintext m. No need to worry about modulo N since me is not large enough.