RSA (Rivest–Shamir–Adleman) is one of the first public-key cryptosystems and is widely used for secure data transmission. It’s also used in TLS handshake. The foundation of RSA lies in asymmetric encryption, where a key pair is involved. Specifically, data encrypted with a private key can be decrypted using the corresponding public key, and vice versa.

Euler Phi

Before deep dive to the RSA, we need some math to understand the key generation process.

What is co-prime number

The co-prime numbers are the pair of numbers that only have 1 common factor is 1. For example, 7 and 2 are co-prime numbers.

Euler Phi

\(\text{φ(n) = how many integers from 1 to n are coprime to n}\) \(\\\) \(\text{φ(n) = (p − 1)(q − 1), with n = p*q}\)

Picture 1. Euler Phi example

If you want get more detail about the Euler Phi please refer to this link: Euler’s Totient Function (mathsisfun.com)

Key Generation

Following the steps below to generate the keys:

  1. Select 2 prime numbers p, q. Calculate N = p*q.
  2. Euler Phi calculation Φ(N) = (P-1)(Q-1).
  3. Select e:  1< e < Φ(N), co-prime with N, Φ(N).
  4. Select d: d*e (mod Φ(N)) = 1.
  5. Finally, we get the publish key (e, N) and private key (d, N).

For example:

  1. Select 2 prime numbers: p = 2, q = 7.
  2. Euler Phi calculation Φ(14) = (2-1)(7-1) = 6.
  3. Select e:  1< e < Φ(14), co-prime with 14, Φ(14) -> e = 5.
  4. Select d: d*e (mod Φ(N)) = 1 <=> 5*d (mod 6) = 1 -> d = 11.
  5. Publish key is (5, 14), private key is (11, 14).

Encryption/Decryption

Encryption with private is (d, N).

\(\text{Convert the text to a number t}\) \(\\\) \(cipher \ text(ct) = t^{d} \ mod \ N\)

Decryption with publish key is (e, N).

\(plain \ text = ct^{e} \ mod \ N\)

Example

Sender side: original text: B – 2, private key (11, 14)

\(cipher \ text = t^{d} \ mod \ N\ = 2^{11} \ mod \ 14 = 4\)

Receiver side: publish key is (5, 14)

\(plain \ text = ct^{e} \ mod \ N\ = 4^{5} \ mod \ 14 = 2\)

In the real world, the private key and the public key are large numbers (e.g. 2048 bits long) for more security. In this topic, I just used small numbers to see how the RSA works. Thanks for reading!