Select a language
cryptography.how
Select an Algo
Selected Algorithm

RSA

An open padlock represents the public key. Its matching private key opens the lock.

Idea of the algorithm

Multiplying two large, randomly chosen primes p and q is easy. Finding them from their product n is hard. RSA makes n public and uses the secret primes to calculate a private exponent d.

Anyone can raise a message number m to the public exponent e to get a ciphertext c. Raising c to d recovers the message. Both calculations keep the remainder after division by n.

Encryptc = mᵉ mod n
Decryptm = cᵈ mod n
  1. Message
  2. Ciphertext
  3. Recovered

Mathematical property

Number theory
Number theory studies whole numbers and their factors. A prime is a whole number greater than 1 whose only positive divisors are 1 and itself. Two numbers are coprime if their only shared positive divisor is 1.
In a grid of 1 through 15, the eight numbers coprime to 15 are 1, 2, 4, 7, 8, 11, 13 and 14. This gives φ(15) = 8.
Take 2 from the highlighted numbers. Its eighth power is 256, one more than 17 × 15.

Euler’s theorem

If m and n share no factor greater than 1, raising m to the power φ(n) leaves a remainder of 1. For a product of two distinct primes, φ(n) = (p − 1)(q − 1).

mφ(n) ≡ 1 (mod n)

RSA chooses e and d so that ed = 1 + kφ(n). For a message coprime to n, this leaves one factor of m and k factors that each equal 1 modulo n. The remainder is the original message.

See the proof for every message

When the message and modulus are coprime

Write n as the product of two distinct primes, p and q. Then φ(n) = (p − 1)(q − 1). It counts the numbers from 1 to n − 1 that are coprime to n. Choosing e coprime to φ(n) guarantees a private inverse d.

med = m(mφ(n))k ≡ m (mod n)

When the message shares a prime factor

Check each prime separately. If the prime divides m, the original and the decrypted number both have remainder zero. Otherwise, Fermat’s little theorem gives the same remainder because ed − 1 is a multiple of that prime minus one.

The Chinese remainder theorem says the two remainders determine one number from 0 to n − 1. This covers every message in that range, including zero.

Original RSA proof, section VI
Quick reference

RSA formula sheet

RSA takes its name from Rivest, Shamir, and Adleman. Select a formula to see its calculation with your current values.

View the original RSA overview diagramRSA key setup, encryption with the public key, and decryption with the private key.