Leandro Junes

RSA

Generate keys, encrypt, decrypt, and explore modular arithmetic.

RSA uses a public key for encryption and a private key for decryption.

RSA Mathematics · key generation

Educational RSA — not secure for real messages. Small primes and no padding, so that every step can be followed.

Two different odd primes from 3 to 1,000,000, and 1 < e < φ(n) with gcd(e, φ(n)) = 1.

Calculated key values

n = pq
—
φ(n) = (p − 1)(q − 1)
—
d = e−1 mod φ(n)
—
Public key (n, e): —
Private exponent d: —

Encrypt / decrypt an integer

  1. Message—
  2. Ciphertext—
  3. Recovered message—

Understanding the mathematics

How were the keys generated?

    The extended Euclidean algorithm

    Each row keeps r = x·φ(n) + y·e. Dividing the two previous remainders gives the quotient and the next row. When the remainder reaches 1, its y is an inverse of e modulo φ(n).

    QuotientRemainder rxy

    Whoever knows p and q can repeat this calculation, which is why the factors of n must stay secret in real RSA. Here they are on the page on purpose.

    Show modular exponentiation steps

    StepExponent bitCurrent baseAccumulated result

    Square-and-multiply reads the exponent in binary from its last bit. The base is squared at every step and reduced modulo n; where the bit is 1 the result is multiplied by the current base. No number ever grows beyond n2, and the power itself is never written out.

    Why does RSA decryption work?

    Decrypting an encrypted message computes (me)d = med, and the key was built so that ed ≡ 1 (mod φ(n)), that is, ed = 1 + k(p − 1)(q − 1) for some whole number k.

    Modulo p. If p does not divide m, Fermat’s little theorem gives mp−1 ≡ 1, so med = m · (mp−1)k(q−1) ≡ m. If p does divide m, both sides are 0 modulo p. Either way med ≡ m (mod p).

    Modulo q. The same argument gives med ≡ m (mod q).

    Together. med − m is divisible by both p and q, which are different primes, so it is divisible by pq = n (the Chinese remainder theorem). Hence med ≡ m (mod n) for every 0 ≤ m < n, including the messages that share a factor with n. Try m = p.

    Why is textbook RSA insecure?
    • It is deterministic. There is no randomness, so equal messages always give equal ciphertexts, and anyone can encrypt a guess with the public key and compare.
    • There is no padding. Modern RSA encryption first encodes the message with a randomized padding such as RSA-OAEP; textbook RSA exponentiates the bare number.
    • It has algebraic structure. The product of two ciphertexts is the ciphertext of the product of their messages, E(a)·E(b) ≡ E(ab), which an attacker can exploit; and a small message with a small exponent may not even wrap around the modulus.
    • These moduli are tiny. A modulus below 1012 is factored at once, and its factors give d. Explore factoring this modulus in the Prime Factorization calculator: enter n = —.

    Larger primes fix only the last point. Unpadded RSA stays insecure at any size; the RSA-OAEP view shows the padded scheme that is actually used.

    RSA combines prime numbers, modular arithmetic, and public-key cryptography.

    Modular Exponentiation · Modular Inverse · Extended Euclidean Algorithm · Prime Factorization

    Diffie–Hellman   Digital Signatures Coming soon