↓ Skip to main content

Part 8: The Story of RSA-8

·1271 words·6 mins·
Table of Contents
RSA-8 - This article is part of a series.
Part 8: This Article
Turning our RSA-8 toy into an SSH-Style RSA Public Key.

Here’s our RSA-8 toy key is:

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

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

A normal OpenSSH RSA public key looks very different:

ssh-rsa AAAA... comment

But the encoded binary payload still carries RSA values including e and n.

In this experiment we build that SSH public-key blob ourselves, Base64-encode it, decode it again, and prove that it still represents:

e = 7
n = 143

Lab warning: This 8-bit RSA key is deliberately insecure and modern OpenSSH will reject it for SSH authentication. i The goal is to understand the representation, not to bypass OpenSSH’s minimum RSA-key requirements.

8.1 SSH Public-Key Structure
#

For an RSA public key, the SSH wire-format payload can be viewed as three length-prefixed fields:

string  "ssh-rsa"
mpint   e
mpint   n

Each field is binary. OpenSSH then Base64-encodes the complete payload for the familiar one-line public-key format:

ssh-rsa <Base64 payload> <optional comment>

Conceptually:

(e=7, n=143)
      |
      v
+-------------------+
| string "ssh-rsa"  |
| mpint e = 7       |
| mpint n = 143     |
+-------------------+
      |
      v
 Base64 encoding
      |
      v
ssh-rsa AAAA...

8.1.1 Build SSH Public Key
#

This script intentionally implements the small amount of SSH binary encoding we need so we can see what is happening.

import base64
import struct


def ssh_string(data: bytes) -> bytes:
    """SSH string = 4-byte big-endian length + bytes."""
    return struct.pack(">I", len(data)) + data


def ssh_mpint(value: int) -> bytes:
    """Encode a non-negative integer as an SSH mpint field."""
    if value == 0:
        raw = b""
    else:
        raw = value.to_bytes((value.bit_length() + 7) // 8, "big")

        # SSH mpint is signed. Prefix zero if the high bit would
        # otherwise make this positive integer look negative.
        if raw[0] & 0x80:
            raw = b"\x00" + raw

    return ssh_string(raw)


# Our 8-bit RSA public key
e = 7
n = 143

blob = (
    ssh_string(b"ssh-rsa")
    + ssh_mpint(e)
    + ssh_mpint(n)
)

encoded = base64.b64encode(blob).decode("ascii")
ssh_public_key = f"ssh-rsa {encoded} rsa-8-demo"

print(ssh_public_key)

Notice that 143 is 0x8f. Its highest bit is 1, so SSH’s signed mpint representation needs a leading zero byte:

143 -> 0x8f -> 0x00 0x8f

That is a serialization detail. The mathematical value is still 143.

8.2 Decode the SSH key
#

Let’s decode the SSH key and recover e and n.

Now reverse the process.

import base64
import struct


def read_field(data: bytes, offset: int):
    length = struct.unpack(">I", data[offset:offset + 4])[0]
    offset += 4

    value = data[offset:offset + length]
    offset += length

    return value, offset


def decode_ssh_rsa_public_key(line: str):
    key_type, encoded, *_ = line.split()

    if key_type != "ssh-rsa":
        raise ValueError("Not an ssh-rsa public key")

    blob = base64.b64decode(encoded)
    offset = 0

    algorithm, offset = read_field(blob, offset)
    e_bytes, offset = read_field(blob, offset)
    n_bytes, offset = read_field(blob, offset)

    if algorithm != b"ssh-rsa":
        raise ValueError("Unexpected algorithm")

    e = int.from_bytes(e_bytes, "big", signed=False)
    n = int.from_bytes(n_bytes, "big", signed=False)

    return e, n


e2, n2 = decode_ssh_rsa_public_key(ssh_public_key)

print("Recovered e =", e2)
print("Recovered n =", n2)
print("Matches original:", (e2, n2) == (7, 143))

Expected result:

Recovered e = 7
Recovered n = 143
Matches original: True

We have now demonstrated:

(7,143)
    |
    v
SSH binary representation
    |
    v
Base64 text representation
    |
    v
decode
    |
    v
(7,143)

The long-looking SSH public key is a representation of the RSA numbers, not a replacement for them.

8.3 SSH RSA Private Key
#

A real RSA private-key serialization contains more than our shorthand (d,n). From our toy parameters we can derive the RSA private components:

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

and CRT values such as:

d mod (p - 1)
d mod (q - 1)
q^-1 mod p

So our tiny RSA mathematics contains enough information to describe a complete RSA private key mathematically.

However, producing a standards-compliant OpenSSH private-key file does not make this an acceptable SSH authentication key. Modern OpenSSH requires much larger RSA moduli and will reject our 8-bit key.

8.4 What Can the 8-bit RSA key do?
#

Yes. We can prove that the RSA mathematics works without asking OpenSSH to accept an insecure key.

The cleanest demonstration is a toy textbook RSA signature.

Assume our demonstration message has already been represented as the small integer:

m = 9

For this educational textbook-RSA exercise, sign using the private exponent:

signature = m^d mod n

Then verify using the public exponent:

recovered = signature^e mod n

8.4.1 Signature Demo
#

# Our complete toy RSA parameters
p = 11
q = 13
n = 143
e = 7
d = 103

message = 9

# Toy textbook-RSA signing
signature = pow(message, d, n)

# Toy textbook-RSA verification
recovered_message = pow(signature, e, n)

print("Message:", message)
print("Signature:", signature)
print("Recovered during verification:", recovered_message)
print("Signature valid:", recovered_message == message)

This gives us a real mathematical round trip:

message = 9
     |
     | private exponent d=103
     v
  signature
     |
     | public exponent e=7
     v
message = 9

That proves our RSA exponents perform the expected modular relationship.

Note that a real RSA signatures do not normally sign an arbitrary tiny integer this way. Production systems hash the message and use a defined signature encoding/padding scheme, such as RSA-PSS. Our modulus is far too small to hold the structures required by real secure signature schemes.

8.4.2 Another Proof: Encrypt/Decrypt
#

We already demonstrated the complementary operation:

message = 9

ciphertext = pow(message, e, n)
plaintext = pow(ciphertext, d, n)

print("Message:", message)
print("Ciphertext:", ciphertext)
print("Recovered:", plaintext)

For our key:

9 --public exponent--> 48 --private exponent--> 9

Again, this is textbook RSA, not secure real-world RSA encryption. Real RSA encryption uses an approved padding/encoding construction such as OAEP and a sufficiently large modulus.

8.5 Summary
#

We have proved that:

  • (e=7,n=143) can be represented in the SSH RSA public-key binary format.
  • That binary data can be Base64-encoded into an ssh-rsa ...-shaped public-key line.
  • We can decode that line and recover exactly e=7 and n=143.
  • The matching private exponent d=103 performs the expected RSA modular operations.
  • The toy key can demonstrate textbook RSA encryption/decryption and signing/verification mathematics.

We have not proved that:

  • the key is secure;
  • the key is acceptable to modern OpenSSH;
  • textbook RSA is a safe encryption or signature protocol;
  • an 8-bit RSA modulus can support the padding and hashing required by real RSA protocols.

8.6 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
SSH-style public encodingCan be
constructed
Yes
Textbook
sign/verify
YesMathematically possible, but not how secure protocols are defined
Secure RSA
signature padding
No roomYes, with suitable key size and scheme
OpenSSH authenticationRejectedYes, when policy/algorithm requirements are met
SecurityNoneDepends on key size, generation, algorithms, and implementation

Revision: 🔑 Key Takeaway
#

This part adds another layer to our mental model:

RSA mathematical values
      |
      | e=7, n=143
      v
SSH binary fields
      |
      v
Base64 encoding
      |
      v
ssh-rsa AAAA...

and then:

ssh-rsa AAAA...
      |
      v
Base64 decode
      |
      v
SSH field parsing
      |
      v
e=7, n=143

So an SSH public-key file is a container/representation of public-key parameters. Base64 is not the cryptography itself.

Meanwhile, the mathematical key can still demonstrate:

Encryption/decryption
m^e -> c -> c^d -> m

and

Toy signing/verification
m^d -> signature -> signature^e -> m

Our 8-bit RSA key is mathematically functional but cryptographically useless: we can serialize its public values and demonstrate RSA operations, while real protocols correctly reject it because security requires far larger keys and proper padding/signature schemes.

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

Related