Cryptography: Caesar, affine cipher and RSA
Shift and affine ciphers, modular inverses with the extended Euclidean algorithm, fast modular exponentiation, and RSA key generation, encryption and decryption with small primes. A final exam topic.
This week we see an application of the previous week (mod, GCD, Euclid). It is a final exam topic. The question types are fixed: “encrypt/decrypt with this cipher”, “find the inverse of a mod m”, “compute ”, “generate an RSA key, encrypt, decrypt”. Since everything is a hand computation, the numbers are small. Golden rules:
- Letters start at 0: A = 0, Z = 25. If you take A = 1, the whole answer shifts.
- Take the mod at every intermediate step. Never let the numbers grow.
- The modular inverse is found with the extended Euclidean algorithm, and you check it by multiplying and getting 1.
- For a big exponent use fast exponentiation: write the exponent in binary and square in turn.
- In RSA, is an inverse modulo , not modulo .
Basic concepts
The message to be kept secret is the plaintext, and its encrypted form is the ciphertext. Encryption is a function , and decryption is its inverse . That is why the encryption function must be a bijection (the functions of week 4 come back here): if two different letters went to the same letter, you could not decrypt.
Shift cipher
The special case with is called the Caesar cipher. Example: encrypt DERS with Caesar.
D E R S -> 3 4 17 18
+3 -> 6 7 20 21
-> G H U V
To decrypt, subtract 3. If the result is negative, add 26: for X = 23, = A, and decrypting back, = X.
This cipher is very weak: there are only 26 keys, and you can try them all. A faster way is frequency analysis: in English text the most common letter is E. If the most common letter in the ciphertext is H, then most likely . The same idea works for the affine cipher; you just solve a system of two equations with two unknowns ( and ) from two letter matches.
Affine cipher
Why is the condition needed? To decrypt you have to “divide” by , so you need the inverse of mod 26. The inverse exists only if . Decryption:
Example: HI with , .
H = 7: 5*7 + 8 = 43, 43 mod 26 = 17 -> R
I = 8: 5*8 + 8 = 48, 48 mod 26 = 22 -> W
Ciphertext: RW
To decrypt you need . , so .
R = 17: 21 * (17 - 8) = 189, 189 mod 26 = 7 -> H
W = 22: 21 * (22 - 8) = 294, 294 mod 26 = 8 -> I
PITFALL · Writing “divide by 5”. There is no division in modular arithmetic. Instead of writing , you multiply by . is not even an integer, but is perfectly an integer.
Modular inverse
is the inverse of mod : .
Theorem. If , then has an inverse mod .
Proof. Suppose gcd(a, m) = 1. By Bezout's identity there are
integers s and t with s*a + t*m = 1.
Then s*a = 1 - t*m, so m | (s*a - 1), i.e. s*a ≡ 1 (mod m).
Hence s is an inverse of a modulo m. ∎
In plain words: if you find the Bezout coefficient, you have found the inverse. The method is the extended Euclidean algorithm. Example: .
40 = 5*7 + 5
7 = 1*5 + 2
5 = 2*2 + 1
Back: 1 = 5 - 2*2
= 5 - 2*(7 - 5) = 3*5 - 2*7
= 3*(40 - 5*7) - 2*7 = 3*40 - 17*7
7 * (-17) ≡ 1 (mod 40) -> -17 + 40 = 23
Check: 7 * 23 = 161 = 4*40 + 1
TIP · check on the last line. After finding the inverse, compute . If it is not 1, a sign slipped while substituting back. A ten-second check saves the whole question.
Fast modular exponentiation (repeated squaring)
You don’t compute something like by multiplying 117 times. The idea: write in binary, find the powers of by squaring in turn, and multiply the powers whose bit is 1. Take the mod at every step.
.
| bit | product (x) | ||
|---|---|---|---|
| 0 | 1 | 5 | 5 |
| 1 | 0 | 5 | |
| 2 | 1 | ||
| 3 | 0 | 20 | |
| 4 | 1 | ||
| 5 | 1 | ||
| 6 | 1 |
Result: . Instead of 117 multiplications we did about 12. That is why the algorithm uses multiplications.
RSA
RSA is a public key system: everyone knows the encryption key, and only the receiver knows the decryption key.
Key generation:
- Choose two primes: , . .
- Compute .
- Choose an with .
- Find .
- The public key is , the private key is .
Encryption and decryption:
Worked example
, , , message .
n = 13 * 19 = 247
phi = 12 * 18 = 216
gcd(5, 216) = 1 -> e = 5 is valid
d = 5^-1 mod 216: 5 * 173 = 865 = 4*216 + 1 -> d = 173
Encrypt: C = 42^5 mod 247
42^2 = 1764 = 7*247 + 35 -> 35
42^4 = 35^2 = 1225 = 4*247 + 237 -> 237
42^5 = 237 * 42 = 9954 = 40*247 + 74 -> C = 74
Decrypt: 74^173 mod 247 = 42 (with fast exponentiation)
Why does it work? Because , we have . By Fermat’s little theorem, the factors collapse to 1 mod and mod , leaving . On the exam a computation is usually asked, not this proof.
Why is it secure? To find you need , and for that you need and . Factoring a small is easy, but in real RSA has hundreds of digits and factoring it is practically impossible.
Common mistakes
| Wrong idea | The truth |
|---|---|
| A = 1, B = 2, … | A = 0, …, Z = 25 |
| When decrypting affine, | , there is no division |
| is suitable for affine | , no inverse |
| The modular inverse came out negative, I left it | Add : |
| In RSA, | |
| For , | |
| Taking the mod only at the end of exponentiation | Take the mod after every square and product |
| Multiplying all the powers in fast exponentiation | Only the ones whose bit is 1 in the binary expansion |
Test yourself
Exam-style questions. Solve them on paper first, then reveal the answer.
Encrypt the message CODE with the Caesar cipher (A = 0, ..., Z = 25).
Show answer
Convert the letters to numbers: C = 2, O = 14, D = 3, E = 4. Add 3: 5, 17, 6, 7. All are less than 26, so no mod was needed. Convert to letters: F, R, G, H.
The ciphertext OLSSV was produced by a shift cipher with key . Decrypt it.
Show answer
Decryption is . O = 14 → 7 = H. L = 11 → 4 = E. S = 18 → 11 = L (twice). V = 21 → 14 = O. The result is HELLO.
Encrypt OK with the affine cipher .
Show answer
O = 14: , → X. K = 10: , → V.
Find the inverse of 7 modulo 26.
Show answer
Euclid: , , . Substitute back: . So , . Check: .
Which of can be used in an affine cipher ?
Show answer
To be decryptable, must have an inverse mod 26, that is . . 4 is even () and 13 has : neither works. 9 and 15 are divisible by neither 2 nor 13: valid.
Use the extended Euclidean algorithm to find .
Show answer
, , . Back: . The inverse is . Check: .
Compute .
Show answer
Small powers: , , , . , so .
Use repeated squaring to compute . Show the binary expansion of the exponent.
Show answer
. Squares mod 29: , , , , , , . Multiply the ones whose bit is 1: . , .
In RSA, let , and . Find , and the private key .
Show answer
, . , so the inverse exists. : , so .
With the RSA key , , , encrypt . What do you get when you decrypt the result?
Show answer
. , . Decryption is ; with fast exponentiation you get , and the message is back. This is the check that RSA was set up correctly.
An RSA public key is . Find and decrypt the ciphertext .
Show answer
, . : , . Decryption: . To check, encrypt in the forward direction: , , . It matches.
Why must the RSA exponent satisfy ? Illustrate with , .
Show answer
The private key is the number with . This inverse exists only if . For , we have : is always a multiple of 4, while is odd, so they cannot be equal. There is no , and the message cannot be decrypted.
Verification: All encryption/decryption results were computed letter by letter in code, modular inverses by the extended Euclidean algorithm and by brute force (two ways), exponentiation traces step by step and with BigInt, and RSA keys and ciphertext/plaintext with node; the encrypt-decrypt round trip was checked for all M values with the n = 247 key (verify/MAT2033/08.mjs).