↓ Skip to main content

Part 7: The Story of RSA-8

·1314 words·7 mins·
Table of Contents
RSA-8 - This article is part of a series.
Part 7: This Article
Opening Up a Real SSH RSA Key.

So far, our RSA story has deliberately used tiny numbers:

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

Public key  = (7, 143)
Private key = (103, 143)

That representation makes the mathematics easy to see. But a real SSH RSA public key looks nothing like (7, 143):

Instead, it looks like:

ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQ...

The important realization is that the RSA mathematics has not disappeared. The SSH file is simply a serialized and encoded representation of the RSA public-key values.

In this part, we will extract the RSA parameters from keys that we own and map them directly back to our 8-bit example.

Security note: The public values e and n are intended to be public. The private values d, p, q, and the private-key file itself must remain secret. Do not publish output containing those private values from a real key.

7.1 Inside the SSH RSA Public Key
#

At the RSA mathematics level, the public key contains:

(e, n)

For our toy key:

e = 7
n = 143

Public key = (7, 143)

A real OpenSSH public-key line instead looks like:

ssh-rsa <Base64-encoded key data> optional-comment

Conceptually:

SSH RSA public-key file
         |
         v
encoded SSH structure
         |
    +----+----+
    |         |
    v         v
    e         n
 public    modulus
 exponent

The encoding changes how the values are stored, not the underlying RSA relationship.


7.2 Inside the SSH RSA Private Key
#

For learning, we have represented our private key as:

(d, n)

where:

d = 103
n = 143

A real RSA private key contains more information than this simplified pair. Implementations normally retain values including the prime factors and additional parameters that support efficient private-key operations.

For this experiment, we will inspect:

p
q
n
e
d

and verify:

p * q == n

7.3 Python Cryptography
#

The example uses the Python cryptography package:

python3 -m pip install cryptography

7.4 SSH RSA Public Key Extraction
#

Here, we will extract (e,n) from an SSH RSA public key.

Assume the public key is:

~/.ssh/id_rsa.pub

Python can parse it directly:

from cryptography.hazmat.primitives import serialization
from cryptography.hazmat.primitives.asymmetric import rsa


def read_public_key(filename):
    with open(filename, "rb") as f:
        public_key = serialization.load_ssh_public_key(f.read())

    if not isinstance(public_key, rsa.RSAPublicKey):
        raise ValueError("This is not an RSA public key")

    numbers = public_key.public_numbers()

    return numbers.e, numbers.n


e, n = read_public_key("/home/user/.ssh/id_rsa.pub")

print("e =", e)
print("n =", n)
print("n bit length =", n.bit_length())
print("Public key =", (e, n))

For many RSA keys, the public exponent will commonly appear as:

e = 65537

The modulus n will be a very large integer rather than our tiny 143.

The exact values depend on the key you generated.

7.5 SSH RSA Private Key Extraction
#

Here, we will extract (d,n) from an SSH RSA private key.

Now inspect a private key that you own:

~/.ssh/id_rsa

The script also handles a passphrase-protected OpenSSH private key by prompting without echoing the passphrase.

from cryptography.hazmat.primitives import serialization
from cryptography.hazmat.primitives.asymmetric import rsa
import getpass


def read_private_key(filename):
    with open(filename, "rb") as f:
        private_key_data = f.read()

    try:
        private_key = serialization.load_ssh_private_key(
            private_key_data,
            password=None,
        )

    except TypeError:
        password = getpass.getpass(
            "Private key passphrase: "
        ).encode()

        private_key = serialization.load_ssh_private_key(
            private_key_data,
            password=password,
        )

    if not isinstance(private_key, rsa.RSAPrivateKey):
        raise ValueError("This is not an RSA private key")

    numbers = private_key.private_numbers()

    d = numbers.d
    n = numbers.public_numbers.n

    return d, n


d, n = read_private_key("/home/user/.ssh/id_rsa")

print("d =", d)
print("n =", n)
print("Private key =", (d, n))

Again, do not publish the resulting value of d for a real key.

7.6 Put Together
#

Here is a combined script that extracts the public (e,n) and our simplified private (d,n) representations and confirms that both refer to the same modulus.

from cryptography.hazmat.primitives import serialization
from cryptography.hazmat.primitives.asymmetric import rsa
import getpass


def read_public_key(filename):
    with open(filename, "rb") as f:
        public_key = serialization.load_ssh_public_key(f.read())

    if not isinstance(public_key, rsa.RSAPublicKey):
        raise ValueError("This is not an RSA public key")

    numbers = public_key.public_numbers()
    return numbers.e, numbers.n


def read_private_key(filename):
    with open(filename, "rb") as f:
        private_key_data = f.read()

    try:
        private_key = serialization.load_ssh_private_key(
            private_key_data,
            password=None,
        )

    except TypeError:
        password = getpass.getpass(
            "Private key passphrase: "
        ).encode()

        private_key = serialization.load_ssh_private_key(
            private_key_data,
            password=password,
        )

    if not isinstance(private_key, rsa.RSAPrivateKey):
        raise ValueError("This is not an RSA private key")

    numbers = private_key.private_numbers()
    return numbers.d, numbers.public_numbers.n


public_file = "/home/user/.ssh/id_rsa.pub"
private_file = "/home/user/.ssh/id_rsa"

e, public_n = read_public_key(public_file)
d, private_n = read_private_key(private_file)

print("PUBLIC KEY")
print("e =", e)
print("n =", public_n)
print("n bit length =", public_n.bit_length())

print()
print("PRIVATE KEY")
print("d =", d)
print("n =", private_n)

print()
print("Same modulus:", public_n == private_n)

For a matching key pair, the final check should be:

Same modulus: True

This gives us a direct bridge from:

Toy public key = (7,143)

to:

Real RSA public key = (65537, very-large-n)

while retaining exactly the same conceptual roles for e and n.

7.7 Go Deeper
#

We can go deeper by extracting p and q.

Because we own the private key, we can inspect the secret prime factors as a learning exercise.

Add:

numbers = private_key.private_numbers()

p = numbers.p
q = numbers.q
d = numbers.d

e = numbers.public_numbers.e
n = numbers.public_numbers.n

print("p =", p)
print("q =", q)
print("n =", n)
print("e =", e)
print("d =", d)

print()
print("p bit length =", p.bit_length())
print("q bit length =", q.bit_length())
print("n bit length =", n.bit_length())
print("Check p * q == n:", p * q == n)

For our original toy example, that same inspection would conceptually show:

p = 11
q = 13
n = 143
e = 7
d = 103

p * q == n  -> True

For a real key, the values are enormously larger, but the relationship remains recognizable.

7.8 RSA-8 Toy vs Real SSH RSA key
#

RSA component8-bit ToyReal SSH RSA key
p11A large secret prime
q13A large secret prime
n143A large public modulus
e7Commonly 65537
d103A large secret exponent
Public
representation
(7,143)(e,n) encoded in SSH format
Private
representation
in our model
(103,143)(d,n),
plus other private RSA parameters

The key lesson is:

The SSH file format looks different.

The RSA mathematics underneath does not.

7.9 Connect Back to ssh-keygen
#

When we run something such as:

ssh-keygen -t rsa -b 2048

we are not asking ssh-keygen to create the literal textual form:

(e,n)

Instead, the RSA implementation generates the RSA key material and OpenSSH stores it in its defined key formats.

Our Python exercise simply opens those formats back up so that we can see familiar mathematical components again:

           ssh-keygen RSA key
                   |
                   v
        OpenSSH key representation
                   |
                   v
            Python parsing
                   |
          +--------+--------+
          |                 |
          v                 v
        Public            Private
        e, n              d, p, q
          |                 |
          +--------+--------+
                   |
                   v
           familiar RSA math

7.10 RSA Lab Key
#

A safer way to perform the experiment.

Rather than inspecting an important production or administrative SSH key, create a disposable lab key specifically for this exercise:

ssh-keygen -t rsa -b 2048 -f ./rsa_lab_key

This produces files such as:

rsa_lab_key
rsa_lab_key.pub

Point the Python script at those files, inspect the values, and delete the lab key when finished if it is no longer required.

Do not publish the private key or the extracted values d, p, or q.

Revision: 🔑 Key Takeaway
#

Our tiny example gave us:

p = 11
q = 13
     |
     v
n = 143
     |
 +---+---+
 |       |
 v       v
e = 7   d = 103
 |       |
 v       v
Public  Private

A real RSA SSH key still contains the same classes of mathematical values:

large p
large q
   |
   v
large n
   |
 +--+--+
 |     |
 v     v
e      d

But instead of printing them as:

(e,n)
(d,n)

OpenSSH serializes them into standard key-file formats.

Python lets us peel that representation back and see the RSA numbers underneath.

An SSH RSA key may look like a long encoded string, but underneath it still contains the same RSA mathematical components we learned with p=11, q=13, e=7, d=103, and n=143.

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

Related