RSA · 03 The maths

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.

  1. 01
    Modulus

    Choose n = p · q, where p and q are two large primes.

  2. 02
    Euler’s function

    Compute φ(n) = (p − 1)(q − 1), the number of integers smaller than n that are coprime with it.

  3. 03
    Public exponent

    Choose e coprime with φ(n), typically 65537.

  4. 04
    Private exponent

    Compute d such that e · d leaves remainder 1 when divided by φ(n): it is the inverse of e modulo φ(n).

  5. 05
    Encryption

    The message, turned into a number m smaller than n, becomes c = m^e mod n.

  6. 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.

Continue with
04 Worked example

RSA: worked example

A complete RSA example with small numbers, from choosing the primes to encrypting and decrypting a number.

↑