↓ Skip to main content

Part 6: The Story of RSA-8

·1216 words·6 mins·
Table of Contents
RSA-8 - This article is part of a series.
Part 6: This Article
3 Ways how our tiny RSA-8 keys can fail.

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:

  1. Factor n directly.
  2. Precompute a table when the possible primes come from a tiny known set.
  3. 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 = 143

The goal is to recover the factors of n:

143 = 11 * 13

Once p and q are known:

phi(n) = (11 - 1)(13 - 1)
       = 120

Then recover the private exponent:

d = e^-1 mod phi(n)
  = 7^-1 mod 120
  = 103

The 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
     d

Method 2 🥀
#

The second failure can be done via precomputed tiny keyspace.

Now deliberately impose this lab rule:

Both p and q must 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 = 300

Thus, 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, 120

When (e=7,n=143) arrives, lookup 143 and calculate:

d = 7^-1 mod 120
  = 103

Precomputed 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 = 143

and:

Bob:
p = 11
q = 17
n2 = 187

The 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 = 187

Calculate their greatest common divisor:

gcd(143, 187) = 11

The shared prime falls straight out.

Then:

143 / 11 = 13
187 / 11 = 17

Both 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 * 17
What 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 17

Why 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 * q2

then mathematically:

gcd(n1, n2) = p

Computing 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
#

MethodAttacker hasWeakness being demonstrated
Direct factoringOne public modulusModulus is tiny or otherwise factorable
Precomputed tableOne modulus and a known tiny prime spaceKey-generation search space is tiny/predictable
Shared-prime GCDMultiple public moduliDifferent 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 q

Once 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.

MySeq
Author
MySeq
myseq.github.io
RSA-8 - This article is part of a series.
Part 6: This Article

Related