Skip to content
Encrypt & Decrypt
step 1/7

Reading — step 1 of 7

Read

~1 min readEncrypt & Sign

Encrypt & Decrypt

Once you have keys, encryption and decryption are simple modular exponentiation:

Encrypt:  c = m^e mod n          (using public key)
Decrypt:  m = c^d mod n          (using private key)

Constraint: m < n. RSA encrypts numbers, not arbitrary text. To encrypt a long message, you typically:

  1. Generate a random AES key.
  2. Encrypt the message with AES.
  3. Encrypt the AES key with RSA.
  4. Send (encrypted_key, encrypted_message).

This is hybrid encryption. RSA's slow asymmetric op only encrypts a small key; AES does the bulk work.

Example with toy keys (p=61, q=53, n=3233, e=17, d=2753):

m = 65 (the letter 'A' as ASCII)
c = 65^17 mod 3233 = 2790
m_recovered = 2790^2753 mod 3233 = 65 ✓

Why does this work? Euler's theorem: m^φ(n) ≡ 1 (mod n) for gcd(m, n) = 1. Since e * d ≡ 1 (mod φ(n)):

m^(e*d) = m^(1 + k*φ(n)) = m * (m^φ(n))^k ≡ m * 1^k = m (mod n)

The math just works out. RSA's correctness is ~200 years of number theory.

Discussion

Ask a question, share an insight, or help someone who’s stuck.

Sign in to post a comment or reply.

Loading…