RSA: the maths
The formulas of RSA — modulus, Euler’s function, exponents, encryption and decryption — and the factoring problem on which security rests.
The mathematics of RSA uses a few ideas from modular arithmetic: you work with the remainders of division by a fixed number, the modulus.
- 01
Modulus
Choose n = p · q, where p and q are two large primes.
- 02
Euler’s function
Compute φ(n) = (p − 1)(q − 1), the number of integers smaller than n that are coprime with it.
- 03
Public exponent
Choose e coprime with φ(n), typically 65537.
- 04
Private exponent
Compute d such that e · d leaves remainder 1 when divided by φ(n): it is the inverse of e modulo φ(n).
- 05
Encryption
The message, turned into a number m smaller than n, becomes c = m^e mod n.
- 06
Decryption
m = c^d mod n is obtained again.
Why it works
Euler’s theorem guarantees that raising m first to e and then to d, modulo n, gives back m. Whoever knows p and q easily computes φ(n) and therefore d. Whoever knows only n would first have to factor it, that is find p and q: for numbers over six hundred decimal digits long no known method allows this in reasonable time on classical computers.
RSA: worked example
A complete RSA example with small numbers, from choosing the primes to encrypting and decrypting a number.
The pages of this topic
Back to the overviewRSA: how it works
Key generation, encryption, decryption and signing with RSA, explained step by step without complicated formulas.
RSA: history
From Diffie and Hellman’s 1976 revolution to the invention of RSA, from the patent to the discovery of the secret British precedent.
RSA: weaknesses and attacks
Known attacks on RSA: keys that are too short, faulty random numbers, vulnerable padding, timing attacks and the threat of quantum computers.
RSA today
Where RSA is still used — certificates, digital signatures, email and code — and why it is giving way to elliptic curves and post-quantum algorithms.
RSA: pitfalls to avoid
The most common mistakes developers and administrators make with RSA, from short keys to home-made implementations.
RSA: frequently asked questions
Short answers to the most common doubts about RSA, keys, security and quantum computers.