↓ Skip to main content

Part 10: The Story of RSA-8

·1352 words·7 mins·
Table of Contents
RSA-8 - This article is part of a series.
Part 10: This Article
I Lost My RSA Public Key. Can the Private Key Recreate It?

The RSA-8 story will ends with a practical question:

If I still have my RSA private key but lose the public key, can I get the public key back?

Yes. An RSA private key normally contains enough information to derive its corresponding public key.

This is very different from losing the private key. The public key is designed to be derived and shared; the private key contains the secret material that must be protected.

Lab reminder: Our 8-bit RSA example is intentionally insecure and exists only to make the mathematics visible.

10.1 Back to RSA-8
#

Throughout this story we used:

p = 11
q = 13
n = 143
phi(n) = 120
e = 7
d = 103

Public key  = (7, 143)
Private key = (103, 143)     # simplified representation

Suppose we somehow lose our record of the public key:

Public key = ???

but still possess the private RSA parameters:

p = 11
q = 13
d = 103

Can we reconstruct the public key?

Yes.

A real RSA private key contains p, q, and d (plus n and e). Even thought this learning tutorials write the private key in shorthand as (d, n) or (103, 143).

10.2 Reconstruct n
#

The modulus is simply:

n = p * q

Therefore:

n = 11 * 13
  = 143

We have recovered the second component of the public key:

(e, 143)

We still need e.

10.3 Reconstruct phi(n)
#

Because p and q are distinct primes:

phi(n) = (p - 1)(q - 1)

So:

phi(n) = 10 * 12
       = 120

10.4 Recover e
#

This step is simpler than it first looks.

We now know:

d = 103
phi(n) = 120

RSA requires e and d to satisfy:

e * d = 1 mod phi(n)

So we need a value of e where:

e * 103 = 1 mod 120

The answer is:

e = 7

because:

7 * 103 = 721
721 mod 120 = 1

Therefore we recover:

e = 7
n = 143

Public key = (7,143)

An important point is that once we know phi(n)=120, going between e and d is easy in either direction:

e = 7  <---- modular inverse ---->  d = 103
                     mod 120

So why is public-to-private supposed to be hard? The difficulty is not simply e -> d. The public key does not reveal phi(n) directly.

From the public key:

(e,n) = (7,143)

An attacker needs:

143
 |
 | factor n
 v
11 * 13
 |
 v
phi(n) = 120
 |
 v
d = 7^-1 mod 120
 |
 v
d = 103

For our 8-bit example, factoring 143 is trivial. For properly generated large RSA keys, recovering the secret factors of n is intended to be computationally infeasible with known classical methods.

So the useful memory aid is:

Once the factor-related private information is known, moving between e and d is easy. i The security barrier is that the public key does not reveal that private factorization.

10.5 The Reconstruction
#

          RSA private information

        p=11     q=13     d=103
          |        |         |
          +---+----+         |
              |              |
              v              |
          n = p * q          |
              |              |
              v              |
            n=143            |
              |              |
              |     phi(n)=(p-1)(q-1)
              |              |
              |              v
              |           phi=120
              |              |
              |              | e=d^-1 mod 120
              |              v
              |             e=7
              |              |
              +------+-------+
                     |
                     v
              Public key
                (7,143)

This direction is easy because we still possess the secret RSA information.

10.5.1 Rebuild Public Key
#

# Our private RSA parameters
p = 11
q = 13
d = 103

# Reconstruct n
n = p * q

# Reconstruct phi(n)
phi = (p - 1) * (q - 1)

# Recover public exponent e
# e is the modular inverse of d modulo phi(n)
e = pow(d, -1, phi)

print("p =", p)
print("q =", q)
print("d =", d)
print("phi(n) =", phi)
print()
print("Recovered e =", e)
print("Recovered n =", n)
print("Recovered public key =", (e, n))

Expected output:

p = 11
q = 13
d = 103
phi(n) = 120

Recovered e = 7
Recovered n = 143
Recovered public key = (7, 143)

So our original public key is back.

10.5.2 What if I only had (d,n)?
#

Our series has frequently written the private key in simplified form as: (d,n) = (103,143)

That is useful for learning RSA operations, but a real RSA private-key structure normally contains more information, including prime factors and values derived from them.

So this shorthand should not be confused with the complete contents of a real RSA private-key file.

For our tiny lab, because n=143 is trivial to factor, we could recover: 143 = 11 * 13 and continue from there.

For a real RSA key, private-key normally already has the required private parameters stored in the key structure and can derive/export the corresponding public key directly.

10.5.3 Recover SSH RSA Public Key
#

Suppose we still have: ~/.ssh/id_rsa but accidentally deleted: ~/.ssh/id_rsa.pub

You can regenerate the public-key line from the private key with:

ssh-keygen -y -f ~/.ssh/id_rsa > ~/.ssh/id_rsa.pub

We can inspect it with:

cat ~/.ssh/id_rsa.pub

10.6 The Direction Matters
#

This gives us a useful public-key cryptography mental model:

PRIVATE KEY
    |
    | derive
    v
PUBLIC KEY

    EASY

The reverse direction should not be practical for a properly generated and sufficiently large RSA key:

PUBLIC KEY
    |
    | recover private secrets
    v
PRIVATE KEY

    INTENDED TO BE HARD

Our RSA-8 key is the exception only because we intentionally made it absurdly small, and is trivial to discover.

Backup and protection efforts should focus primarily on the private key. The public key is designed to be distributed.

Revision: RSA-8 Story (final)
#

We began with two tiny primes:

p = 11
q = 13

and built:

n = p * q
  = 143

phi(n) = 120

e = 7
d = 103

giving:

Public key  = (7,143)
Private key = (103,143)     # simplified learning notation

Across the series, that tiny key let us see several RSA ideas directly:

                     p=11       q=13
                         \       /
                          \     /
                           n=143
                             |
                    +--------+--------+
                    |                 |
                    v                 v
                  e=7              d=103
                    |                 |
                    v                 v
                 PUBLIC            PRIVATE
                    |                 |
          +---------+--------+--------+---------+
          |                  |                  |
          v                  v                  v
     encryption           verification       signing
          |                                     |
          v                                     v
     ciphertext                              signature
          |
          v
     decryption

We also learned that:

  • n can be factored instantly when the key is tiny.
  • A tiny, known prime search space can be precomputed.
  • Shared primes between RSA moduli can be exposed with GCD.
  • SSH serializes RSA public values into a structured binary format and Base64 text.
  • A tiny RSA key can demonstrate the mathematics while still being completely unacceptable for real security.
  • RSA signatures and encryption need proper real-world schemes, not raw textbook operations.
  • The public key can be derived from retained private RSA key material.

This asymmetry is at the heart of why public-key cryptography is useful.

🔐 RSA Diagram
#

         PRIVATE INFORMATION
         p = 11    q = 13
              \    /
               \  /
             n = 143
                |
          phi(n) = 120
                |
       +--------+--------+
       |                 |
       v                 v
     e = 7            d = 103
       |                 |
       v                 v
 Public key          Private side
  (7,143)             (103,143)

Knowing phi(n)=120:

e = 7  <--- modular inverse --->  d = 103
                    mod 120

Public-to-private requires recovering the hidden
factor-related information behind n:

(7,143) -> factor 143 -> 11 x 13 -> phi(n)=120 -> d=103

The key point is that calculating the modular inverse is easy once phi(n) is known. The hard part for a properly generated real RSA public key is recovering the secret factorization needed to obtain that information.

🔑 Key Takeaway
#

RSA is not secure because e and d are difficult to convert between. Once the private factor information is known, that relationship is easy to calculate. RSA security depends on making it computationally infeasible to recover the secret factors behind the public modulus n.

For our RSA-8 key:

n = 143 -> 11 x 13 -> easy -> private key recoverable

For a properly generated large RSA key:

n -> p x q -> intended to be computationally infeasible

Links#

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

Related