↓ Skip to main content

Part 1: The Story of RSA-8

·475 words·3 mins·
Table of Contents
RSA-8 - This article is part of a series.
Part 1: This Article
Shrinking RSA Until We Can See the Mathematics.

RSA is normally discussed with 2048-bit or larger keys.

Those numbers are appropriate for real security, but they make the underlying mathematics difficult to see.

So this series starts with a deliberately insecure RSA key small enough to calculate by hand.

Important: Everything in this series is for learning.

An 8-bit RSA key provides no security.

1.1 Choose 2 Primes
#

First, Let choose 2 primes, p and q:

p = 11
q = 13

RSA does not require q > p. Swapping them changes nothing because multiplication is commutative.

The primes should, however, be different in normal RSA: p != q.

1.2 Calculate the Modulus
#

n = p * q
  = 11 * 13
  = 143

143 in binary is:

10001111

Therefore n has a bit length of 8. This is our toy 8-bit RSA example.

When people say RSA-2048, the 2048 normally refers to the approximate bit length of the modulus n, not the bit length of each prime.

1.3 Calculate Euler’s totient
#

Since p and q are distinct primes:

phi(n) = (p - 1)(q - 1)
       = 10 * 12
       = 120

1.4 Choose the Public Exponent
#

To form the public key (e,n), we need to choose an e.

Choose e where 1 < e < phi(n):

e = 7

And it must be relatively prime to phi(n):

gcd(7, 120) = 1
How is it choosen

e is a free choice from a pool of valid candidates: any integer between 2 and 119 that shares no factors with 120. Other valid choices for phi(n) = 120 include: 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 49.

Our public key is now:

(e, n) = (7, 143)

1.5 Calculate the Private Exponent
#

We need d such that: e * d = 1 mod phi(n)

Resolve our values:

7 * d = 1 mod 120

The answer is:

d = 103

because:

7 * 103 = 721
721 mod 120 = 1

Therefore:

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

📌 Story Board
#

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

🔑 Key Takeaway
#

  • RSA begins with two secret primes, p and q.
  • The public modulus is n = p * q.
  • The public key contains e and n.
  • The private exponent d is mathematically related to e through the factors of n.
  • Real RSA uses vastly larger values, but the basic idea is the same.

In Part 2, we will use the key to encrypt and decrypt a number and implement the whole exercise in Python.

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

Related