№ 56 · cryptography

How RSA locks and unlocks a message

RSA lets you publish a key anyone can use to lock a message, while only you hold the key that unlocks it. Both are the same operation — raise a number to a power and keep the remainder — and the trick is in how the two powers are chosen.

Two keys, one operation

A message is a number M smaller than a fixed modulus n. To encrypt, raise M to a public power e and take the remainder mod n; that is the ciphertext C. To decrypt, raise C to a secret power d, mod n again. The public key is (e, n); the private key is (d, n). Same arithmetic both ways — the paper notes one piece of hardware does both jobs.

Why it matters

Every classical cipher the paper lists needed the parties to share a secret key first, by courier or another private channel — the key distribution problem. Here the encryption key can sit in a public directory. Anyone can lock a message to you; only you can open it. And because decrypting an unencrypted message also "makes sense", the same machinery gives a signature: apply your private power to a message, and anyone can check it with your public power.

Interactive Walk one round trip through RSA: two primes make the modulus n and φ(n), a secret d gets its public partner e, then a message is raised to e, and the result raised to d, all modulo n. Start from the paper's own example, or a tiny illustrative pair small enough to factor in your head. Drag M to any message. The right-hand column is the eavesdropper's view.

Every figure is computed live with exact integer arithmetic (BigInt), including the powers, which are done by a repeated-squaring procedure like the one the paper gives, modulo n, the procedure the paper gives. The public numbers are the ones an eavesdropper sees; the secret ones never leave the owner. In the paper's example the primes, d, e and the default message block 920 are the authors' own (Section VIII); the second preset is ours, chosen only so that n can be factored by hand.

Why the second power undoes the first

The modulus n is the product of two primes, p and q. Let φ(n) = (p − 1)(q − 1). The identity due to Euler and Fermat says that for any M with no factor in common with n, Mφ(n) leaves remainder 1 when divided by n. So if e·d ≡ 1 (mod φ(n)) — one more than a multiple of φ(n) — then Me·d = Mkφ(n)+1 = (Mφ(n))k · M ≡ M. The paper extends the argument to every M from 0 to n − 1 by checking modulo p and q separately, so encryption is a permutation of the messages and decryption its exact inverse.

The paper's own small example: p = 47, q = 59, so n = 2773 and φ(n) = 46 · 58 = 2668. The authors pick d = 157 and run Euclid's algorithm to find its inverse, e = 17; check: 17 · 157 = 2669 = 2668 + 1. Their message block M = 920 encrypts to 92017 mod 2773 = 948, and 948157 mod 2773 = 920 again. Large powers are cheap: the paper computes them by repeated squaring, one per bit of the exponent.

What the attacker holds

An eavesdropper has n and e. To find d she needs φ(n), which comes from p and q. With the factors, everything falls out in a few lines — so the scheme rests on keeping them hidden inside n. The paper shows the obvious attacks — computing φ(n), or finding d by another route — are each at least as hard as factoring n, because any of them would let you factor n. It does not prove factoring is hard, and says so: factoring "is not provably difficult", and the security "rests in part" on that difficulty. The authors recommend primes of about 100 digits, so that n has 200, and note that no known algorithm factored numbers of that size in reasonable time.

In short

RSA encrypts by raising a message to a public power modulo n and decrypts with a private power. It works because the powers multiply to one more than a multiple of φ(n), and the identity due to Euler and Fermat returns the original message. Anyone who can factor n can find the private power. Whether that is the only way in is a conjecture the authors state but cannot prove — the paper says outright that factoring large numbers is "not provably difficult" — and they leave breaking the system as a challenge to the reader.

Where this comes from

  1. A Method for Obtaining Digital Signatures and Public-Key Cryptosystems (Communications of the ACM 21(2), 120-126) linked only, not reproduced
    R. L. Rivest, A. Shamir, L. Adleman · 1978
    people.csail.mit.edu/rivest/Rsapaper.pdf