RSA, one letter at a time

Type a message and watch two prime numbers turn it into ciphertext, then back again. Every number on this page is computed live by the NPcryption Python library, running in your browser.

Each character becomes a number block. Tap one to see how it was computed.

Starting Python in your browser. The first visit downloads about 12 MB.

  1. Choose two secret primes

    RSA starts with two prime numbers, P and Q. Their product N becomes part of the public key. Everything derived from P and Q stays private.

    Try

  2. Pick the public exponent

    E is the other half of the public key. It must share no factor with φ(N), because that's what guarantees a matching private exponent D exists.

    Finding D with the Extended Euclidean Algorithm

    Divide repeatedly, as when finding a greatest common divisor, while tracking how each remainder is built from φ(N) and E. When the remainder reaches 1, the D column holds the inverse of E.

    Extended Euclidean Algorithm rows
    RemainderQuotientXD

  3. Encrypt each character

    Each character's code M becomes C = ME mod N. Instead of multiplying M by itself E times, the library squares repeatedly and multiplies only the rows that match the 1-bits of E.

    Ciphertext

  4. Decrypt with the private key

    The ciphertext is cut back into blocks of the same width, and each block C becomes M = CD mod N. Only someone who knows D can do this step.

Why this is only a toy

Anyone can find your private key

Patterns show through

Real RSA uses primes hundreds of digits long and adds random padding (such as OAEP) before encrypting whole messages, not single characters.