Cryptography: four methods, from Caesar to Alice and Bob

Substitution, transposition, XOR and RSA: full encryption and decryption examples with an interactive laboratory.

Section: Number theory Updated:
AI assistant

Articles /cryptography-four-methods-alice-bob
Cryptography: four methods, from Caesar to Alice and Bob

11 min

Cryptography transforms a readable message into text that requires a key to interpret. The first three methods can be explored with pencil and paper or a spreadsheet; the fourth introduces two different keys. Try each step in the interactive laboratory. The cover illustrates the passage from letter transformations to public and private keys; it is a conceptual image, not a technical diagram.

Illustration for the Number theory section
Section: Number theory

1. Substitution: shifting letters

Choose k=2 and use A–Z. Replace each letter by the one two positions later, wrapping around after Z: S→U, A→C, L→N. Thus SALVATORE becomes UCNXCVQTG. To decrypt, move each letter two positions back: U→S, C→A, until SALVATORE returns. This is the principle of the Caesar cipher, but letter frequencies remain visible, so it does not protect real data.

2. Transposition: change the order, not the letters

With key MOSAI and plaintext SALVATORE, write the letters from left to right under five columns:

M O S A I
S A L V A
T O R E

The original columns are ST | AO | LR | VE | A. Reading them in alphabetical key order, A,I,M,O,S, gives VE | A | ST | AO | LR, or VEASTAOLR. Bob knows the same key: he determines each column length, fills the columns in alphabetical order and reads across the rows to recover SALVATORE. This version removes spaces, accents and punctuation before encryption; they cannot be automatically restored.

3. XOR: the same numeric key encrypts and decrypts

Take the ASCII byte for the first letter, S=83, and a one-byte key 113. Bitwise combination yields 83 XOR 113 = 34, or 22 in hexadecimal. Apply the key again and 34 XOR 113 = 83: S reappears. For SALVATORE the complete hexadecimal sequence is 22303D2730253E2334. The laboratory applies XOR to UTF-8 bytes, so it can also process non-ASCII messages. Reusing one key byte is insecure: this only demonstrates (m XOR k) XOR k = m.

4. Public-key cryptography: Alice writes to Bob

In the first three methods Alice and Bob need to know the same key. RSA works differently: Bob prepares a public key, which Alice can use to encrypt, and a private key, which only Bob uses to decrypt. We shall build them with deliberately small numbers so that every calculation can be checked.

Step 1: Bob chooses two primes

He chooses p=3 and q=11. Their product is n=p×q=3×11=33. The statement 50 mod 33 = 17 says that 17 is the remainder in 50=33×1+17: encryption and decryption will retain remainders modulo 33.

Step 2: he computes φ(n)

For two distinct primes, φ(n)=(p−1)(q−1). Here φ(33)=(3−1)(11−1)=2×10=20. The 20 counts the integers from 1 through 33 that are coprime to 33, meaning they share no divisor greater than 1 with 33. This number links the two keys mathematically.

Step 3: he chooses the public key

Bob chooses e=7: it is greater than 1, less than 20 and coprime to 20. The public key is (n,e)=(33,7). Bob can show it to everyone; Alice needs no secret number to encrypt.

Step 4: he finds the private key

Bob seeks a number d such that dividing e×d by φ(n) leaves remainder 1. Try 7×1=7, remainder 7 on division by 20; 7×2=14, remainder 14; 7×3=21=20+1, remainder 1. Thus d=3. The private key in this example is (n,d)=(33,3). The number 33 appears in both keys; the 3 used as the exponent must remain secret. In a real system Bob also protects the prime factors.

Step 5: Alice encrypts a letter

To keep this example distinct from the bytes used in the laboratory, assign numbers to letters: A=1, B=2, C=3, D=4, E=5, …, Z=26. Alice wants to send E, so the numeric message is m=5, less than 33. Using the public key, she computes c=m^e mod n=5^7 mod 33. Since 5^7=78125=33×2367+14, the remainder is c=14. Alice sends Bob the number 14, not the letter E.

Step 6: Bob decrypts

Bob uses the private key: m=c^d mod n=14^3 mod 33. He calculates 14^3=2744=33×83+5; the remainder is 5. Under our alphabet convention, 5 denotes E. The original letter has returned.

Why do the two calculations undo each other?

We chose the exponents so that e×d=7×3=21=1+φ(33). In this case, encrypting and then decrypting therefore amounts to raising the message to m^21=m×m^20. Because 5 is coprime to 33, Euler’s theorem gives 5^20 mod 33=1; the result has the same remainder as 5×1=5. More generally, RSA also recovers messages not coprime to 33 by applying the Chinese remainder theorem separately to the primes 3 and 11.

Summary: Bob chooses 3,11, obtains n=33 and φ(n)=20, publishes (33,7) and keeps (33,3) private. Alice sends 5^7 mod 33=14; Bob recovers 14^3 mod 33=5, or E. The interactive laboratory instead uses the earlier modulus 3233 to represent full UTF-8 bytes: with 33 we can encode only numbers from 0 to 32, not the ASCII byte for E, which is 69.

What this does and does not prove

This example illustrates the difference between a shared key and a public/private pair; it is not usable protection. The primes can be factored by hand, each byte is encrypted separately and there is no padding. For real applications, RFC 8017 specifies RSAES-OAEP: do not copy this elementary RSA into a security system. Also, knowing a public key does not itself prove that it belongs to Bob; its identity must be verified. Open the laboratory and try another message →