In the earlier parts, we built an intentionally tiny RSA system around:
p = 11
q = 13
n = 143
Public key = (7, 143)
Private key = (103, 143)Now we can turn the example around and look at RSA from the attacker’s perspective.
This chapter keeps three techniques separate because they illustrate three different failure conditions:
- Factor
ndirectly. - Precompute a table when the possible primes come from a tiny known set.
- Compare many RSA moduli with GCD to discover accidentally shared primes.
Lab note: The small-number examples and Python code here are for understanding RSA mathematics. Properly generated modern RSA keys do not have this tiny search space.
Method 1 🥀#
The first failure is to factor the modulus directly.
Suppose the attacker has only:
Public key = (7, 143)They know:
e = 7
n = 143The goal is to recover the factors of n:
143 = 11 * 13Once p and q are known:
phi(n) = (11 - 1)(13 - 1)
= 120Then recover the private exponent:
d = e^-1 mod phi(n)
= 7^-1 mod 120
= 103The private key in our simplified representation is therefore:
(103, 143)Factor with Python#
import math
e = 7
n = 143
print("Public key:", (e, n))
p = None
for candidate in range(2, math.isqrt(n) + 1):
if n % candidate == 0:
p = candidate
break
if p is None:
raise ValueError("Could not factor n")
q = n // p
phi = (p - 1) * (q - 1)
d = pow(e, -1, phi)
print("p =", p)
print("q =", q)
print("phi(n) =", phi)
print("d =", d)
print("Recovered private key:", (d, n))The important path is:
public (e,n)
|
v
factor n
|
v
p and q
|
v
phi(n)
|
v
dMethod 2 🥀#
The second failure can be done via precomputed tiny keyspace.
Now deliberately impose this lab rule:
Both
pandqmust be distinct primes below 100.
There are only 25 primes below 100. Ignoring order, this gives:
25 choose 2 = 300
or
(25 x 24 ) / 2 = 300Thus, 300 possible (p,q) pairs.
That is small enough to enumerate completely.
Instead of waiting to see n=143 and factoring it, we can precompute:
n -> p, q, phi(n)For example:
143 -> 11, 13, 120When (e=7,n=143) arrives, lookup 143 and calculate:
d = 7^-1 mod 120
= 103Precomputed Weak-modulus#
Here’s the way to implement precomputed weak-modules table in Python.
import math
def is_prime(number):
if number < 2:
return False
for candidate in range(2, math.isqrt(number) + 1):
if number % candidate == 0:
return False
return True
primes = [
number
for number in range(2, 100)
if is_prime(number)
]
rsa_table = {}
for i, p in enumerate(primes):
for q in primes[i + 1:]:
n = p * q
phi = (p - 1) * (q - 1)
rsa_table[n] = {
"p": p,
"q": q,
"phi": phi,
}
print("Primes below 100:", len(primes))
print("Precomputed entries:", len(rsa_table))
# Target public key
e = 7
n = 143
if n in rsa_table:
entry = rsa_table[n]
p = entry["p"]
q = entry["q"]
phi = entry["phi"]
d = pow(e, -1, phi)
print("Found n in table")
print("p =", p)
print("q =", q)
print("phi(n) =", phi)
print("d =", d)
print("Recovered private key:", (d, n))
else:
print("n is not in the precomputed table")This can be described as a precomputed weak RSA modulus table. We are enumerating a deliberately tiny RSA factor space, not reversing a password hash function.
Method 3 🥀#
The 3th failure is called The Shared-Prime GCD Attack.
This attack teaches a different lesson.
Imagine two RSA keys:
Alice:
p = 11
q = 13
n1 = 143and:
Bob:
p = 11
q = 17
n2 = 187The mistake is that both keys accidentally reuse the secret prime 11.
An observer does not know that. They only see the 2 public moduli:
n1 = 143
n2 = 187Calculate their greatest common divisor:
gcd(143, 187) = 11The shared prime falls straight out.
Then:
143 / 11 = 13
187 / 11 = 17Both moduli are now factored and both key-pairs for Alice and Bob are recovered.
Public n1=143 Public n2=187
| |
+----------+-----------+
|
v
gcd(n1,n2)
|
v
11
shared prime
/ \
v v
143 / 11 187 / 11
| |
v v
13 17
11 * 13 11 * 17What is GCD?
GCD stands for Greatest Common Divisor (also commonly called Greatest Common Factor, or GCF). It is the largest positive integer that divides two or more given integers evenly without leaving a remainder.
Python Demo#
import math
alice_n = 143
bob_n = 187
shared_prime = math.gcd(alice_n, bob_n)
print("gcd =", shared_prime)
if 1 < shared_prime < min(alice_n, bob_n):
alice_q = alice_n // shared_prime
bob_q = bob_n // shared_prime
print("Shared prime =", shared_prime)
print("Alice factors =", shared_prime, alice_q)
print("Bob factors =", shared_prime, bob_q)
else:
print("No non-trivial shared factor found")Expected result:
gcd = 11
Shared prime = 11
Alice factors = 11 13
Bob factors = 11 17Why the GCD Attack Matters in Real RSA#
Unlike our direct factoring and precomputation experiments, the shared-prime problem is not merely a toy idea.
If two properly large RSA moduli are:
n1 = p * q1
n2 = p * q2then mathematically:
gcd(n1, n2) = pComputing a GCD is easy compared with factoring a properly generated RSA modulus from scratch.
The failure is therefore not that RSA’s factoring assumption has suddenly been defeated. The key-generation process has leaked structure by reusing a secret prime.
Historically, researchers have found real deployed RSA keys sharing factors, particularly in systems where key generation suffered from inadequate randomness. Large-scale studies used efficient batch-GCD techniques to compare many public moduli and recover vulnerable factors.
The security lesson is simple:
Key size alone is not enough. RSA also depends on high-quality, independent randomness during key generation.
Comparing the 3 Experiments#
| Method | Attacker has | Weakness being demonstrated |
|---|---|---|
| Direct factoring | One public modulus | Modulus is tiny or otherwise factorable |
| Precomputed table | One modulus and a known tiny prime space | Key-generation search space is tiny/predictable |
| Shared-prime GCD | Multiple public moduli | Different keys accidentally reuse a prime |
The first two fail because we deliberately made RSA tiny.
The third illustrates an implementation/key-generation failure that has occurred with real RSA deployments.
🔑 Key Takeaway#
For revision, as an addition to RSA-8 story. We started with:
p = 11
q = 13
|
v
n = 143
|
v
Public key = (7,143)At first, RSA looked like an encryption/decryption exercise.
Now we can see three different questions an attacker might ask:
Can I factor this n?
Can I precompute its factors because the possible primes
come from a tiny predictable set?
Can I compare this n with other public moduli and discover
that two keys accidentally share a factor?All three eventually aim at the same critical RSA secrets:
p and qOnce those factors are recovered, the private-key mathematics can be reconstructed.
RSA does not only need large primes; it needs large, independently and securely generated primes. A shared prime can turn an otherwise enormous RSA modulus into an easy GCD problem.


