MAT2033 · Week 8 · cryptography

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.

cryptographyplaintextciphertextshift cipherCaesar cipheraffine ciphermodular inverseextended Euclidean algorithmmodular exponentiationrepeated squaringpublic keyprivate keyRSAEuler's phi function

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 an mod ma^n \bmod m”, “generate an RSA key, encrypt, decrypt”. Since everything is a hand computation, the numbers are small. Golden rules:

  1. Letters start at 0: A = 0, Z = 25. If you take A = 1, the whole answer shifts.
  2. Take the mod at every intermediate step. Never let the numbers grow.
  3. The modular inverse is found with the extended Euclidean algorithm, and you check it by multiplying and getting 1.
  4. For a big exponent use fast exponentiation: write the exponent in binary and square in turn.
  5. In RSA, dd is an inverse modulo φ(n)\varphi(n), not modulo nn.

Basic concepts

The message to be kept secret is the plaintext, and its encrypted form is the ciphertext. Encryption is a function ff, and decryption is its inverse f−1f^{-1}. 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

c=f(p)=(p+k) mod 26,p=f−1(c)=(c−k) mod 26.c = f(p) = (p + k) \bmod 26, \qquad p = f^{-1}(c) = (c - k) \bmod 26.

The special case with k=3k = 3 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, 23+3=26≡023 + 3 = 26 \equiv 0 = A, and decrypting back, 0−3=−3≡230 - 3 = -3 \equiv 23 = 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 k=7−4=3k = 7 - 4 = 3. The same idea works for the affine cipher; you just solve a system of two equations with two unknowns (aa and bb) from two letter matches.

Affine cipher

c=(ap+b) mod 26,gcd⁡(a,26)=1.c = (ap + b) \bmod 26, \qquad \gcd(a, 26) = 1.

Why is the condition needed? To decrypt you have to “divide” by aa, so you need the inverse a−1a^{-1} of aa mod 26. The inverse exists only if gcd⁡(a,26)=1\gcd(a, 26) = 1. Decryption:

p=a−1(c−b) mod 26.p = a^{-1}(c - b) \bmod 26.

Example: HI with a=5a = 5, b=8b = 8.

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 5−1 mod 265^{-1} \bmod 26. 5⋅21=105=4⋅26+15 \cdot 21 = 105 = 4 \cdot 26 + 1, so 5−1=215^{-1} = 21.

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 (c−8)/5(c - 8)/5, you multiply by 5−1=215^{-1} = 21. 9/59/5 is not even an integer, but 21⋅9 mod 2621 \cdot 9 \bmod 26 is perfectly an integer.

Modular inverse

aˉ\bar{a} is the inverse of aa mod mm: aaˉ≡1(modm)a\bar{a} \equiv 1 \pmod m.

Theorem. If gcd⁡(a,m)=1\gcd(a, m) = 1, then aa has an inverse mod mm.

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: 7−1 mod 407^{-1} \bmod 40.

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 a⋅aˉ mod ma \cdot \bar{a} \bmod m. 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 5117 mod 235^{117} \bmod 23 by multiplying 117 times. The idea: write 117117 in binary, find the powers 51,52,54,58,…5^1, 5^2, 5^4, 5^8, \dots of 55 by squaring in turn, and multiply the powers whose bit is 1. Take the mod at every step.

117=64+32+16+4+1=(1110101)2117 = 64 + 32 + 16 + 4 + 1 = (1110101)_2.

iibit52i mod 235^{2^i} \bmod 23product (x)
0155
1025≡225 \equiv 25
21445⋅4=205 \cdot 4 = 20
30161620
41256≡3256 \equiv 320⋅3=60≡1420 \cdot 3 = 60 \equiv 14
519914⋅9=126≡1114 \cdot 9 = 126 \equiv 11
6181≡1281 \equiv 1211⋅12=132≡1711 \cdot 12 = 132 \equiv 17

Result: 5117 mod 23=175^{117} \bmod 23 = 17. Instead of 117 multiplications we did about 12. That is why the algorithm uses O(log⁡n)O(\log n) multiplications.

RSA

RSA is a public key system: everyone knows the encryption key, and only the receiver knows the decryption key.

Key generation:

  1. Choose two primes: pp, qq. n=pqn = pq.
  2. Compute φ(n)=(p−1)(q−1)\varphi(n) = (p-1)(q-1).
  3. Choose an ee with gcd⁡(e,φ(n))=1\gcd(e, \varphi(n)) = 1.
  4. Find d=e−1 mod φ(n)d = e^{-1} \bmod \varphi(n).
  5. The public key is (n,e)(n, e), the private key is dd.

Encryption and decryption:

C=Me mod n,M=Cd mod n.C = M^e \bmod n, \qquad M = C^d \bmod n.

Worked example

p=13p = 13, q=19q = 19, e=5e = 5, message M=42M = 42.

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 ed≡1(modφ(n))ed \equiv 1 \pmod{\varphi(n)}, we have Med=M1+kφ(n)M^{ed} = M^{1 + k\varphi(n)}. By Fermat’s little theorem, the factors Mφ(n)M^{\varphi(n)} collapse to 1 mod pp and mod qq, leaving MM. On the exam a computation is usually asked, not this proof.

Why is it secure? To find dd you need φ(n)\varphi(n), and for that you need pp and qq. Factoring a small nn is easy, but in real RSA nn has hundreds of digits and factoring it is practically impossible.

Common mistakes

Wrong ideaThe truth
A = 1, B = 2, …A = 0, …, Z = 25
When decrypting affine, (c−b)/a(c - b)/aa−1(c−b) mod 26a^{-1}(c - b) \bmod 26, there is no division
a=13a = 13 is suitable for affinegcd⁡(13,26)=13\gcd(13, 26) = 13, no inverse
The modular inverse came out negative, I left itAdd mm: −17 mod 40=23-17 \bmod 40 = 23
In RSA, d=e−1 mod nd = e^{-1} \bmod nd=e−1 mod φ(n)d = e^{-1} \bmod \varphi(n)
φ(n)=n−1\varphi(n) = n - 1For n=pqn = pq, φ(n)=(p−1)(q−1)\varphi(n) = (p-1)(q-1)
Taking the mod only at the end of exponentiationTake the mod after every square and product
Multiplying all the powers in fast exponentiationOnly the ones whose bit is 1 in the binary expansion

Test yourself

Exam-style questions. Solve them on paper first, then reveal the answer.

Question 1easy

Encrypt the message CODE with the Caesar cipher f(p)=(p+3) mod 26f(p) = (p + 3) \bmod 26 (A = 0, ..., Z = 25).

Show answer
FRGH

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.

Question 2easy

The ciphertext OLSSV was produced by a shift cipher with key k=7k = 7. Decrypt it.

Show answer
HELLO

Decryption is p=(c−7) mod 26p = (c - 7) \bmod 26. O = 14 → 7 = H. L = 11 → 4 = E. S = 18 → 11 = L (twice). V = 21 → 14 = O. The result is HELLO.

Question 3medium

Encrypt OK with the affine cipher f(p)=(7p+3) mod 26f(p) = (7p + 3) \bmod 26.

Show answer
XV

O = 14: 7⋅14+3=1017 \cdot 14 + 3 = 101, 101 mod 26=101−78=23101 \bmod 26 = 101 - 78 = 23 → X. K = 10: 7⋅10+3=737 \cdot 10 + 3 = 73, 73 mod 26=73−52=2173 \bmod 26 = 73 - 52 = 21 → V.

Question 4medium

Find the inverse of 7 modulo 26.

Show answer
15

Euclid: 26=3⋅7+526 = 3 \cdot 7 + 5, 7=1⋅5+27 = 1 \cdot 5 + 2, 5=2⋅2+15 = 2 \cdot 2 + 1. Substitute back: 1=5−2⋅2=5−2(7−5)=3⋅5−2⋅7=3(26−3⋅7)−2⋅7=3⋅26−11⋅71 = 5 - 2\cdot 2 = 5 - 2(7 - 5) = 3 \cdot 5 - 2 \cdot 7 = 3(26 - 3\cdot 7) - 2\cdot 7 = 3 \cdot 26 - 11 \cdot 7. So 7⋅(−11)≡17 \cdot (-11) \equiv 1, −11 mod 26=15-11 \bmod 26 = 15. Check: 7⋅15=105=4⋅26+17 \cdot 15 = 105 = 4 \cdot 26 + 1.

Question 5medium

Which of a=4,9,13,15a = 4, 9, 13, 15 can be used in an affine cipher f(p)=(ap+b) mod 26f(p) = (ap + b) \bmod 26?

Show answer
9 and 15

To be decryptable, aa must have an inverse mod 26, that is gcd⁡(a,26)=1\gcd(a, 26) = 1. 26=2⋅1326 = 2 \cdot 13. 4 is even (gcd⁡=2\gcd = 2) and 13 has gcd⁡=13\gcd = 13: neither works. 9 and 15 are divisible by neither 2 nor 13: valid.

Question 6medium

Use the extended Euclidean algorithm to find 17−1 mod 6017^{-1} \bmod 60.

Show answer
53

60=3⋅17+960 = 3 \cdot 17 + 9, 17=1⋅9+817 = 1 \cdot 9 + 8, 9=1⋅8+19 = 1 \cdot 8 + 1. Back: 1=9−8=9−(17−9)=2⋅9−17=2(60−3⋅17)−17=2⋅60−7⋅171 = 9 - 8 = 9 - (17 - 9) = 2 \cdot 9 - 17 = 2(60 - 3 \cdot 17) - 17 = 2 \cdot 60 - 7 \cdot 17. The inverse is −7≡53(mod60)-7 \equiv 53 \pmod{60}. Check: 17⋅53=901=15⋅60+117 \cdot 53 = 901 = 15 \cdot 60 + 1.

Question 7medium

Compute 345 mod 73^{45} \bmod 7.

Show answer
6

Small powers: 31=33^1 = 3, 32=23^2 = 2, 33=63^3 = 6, 36=62=36≡1(mod7)3^6 = 6^2 = 36 \equiv 1 \pmod 7. 45=6⋅7+345 = 6 \cdot 7 + 3, so 345=(36)7⋅33≡1⋅6=63^{45} = (3^6)^7 \cdot 3^3 \equiv 1 \cdot 6 = 6.

Question 8hard

Use repeated squaring to compute 673 mod 296^{73} \bmod 29. Show the binary expansion of the exponent.

Show answer
13

73=(1001001)2=64+8+173 = (1001001)_2 = 64 + 8 + 1. Squares mod 29: 61=66^1 = 6, 62=36≡76^2 = 36 \equiv 7, 64=49≡206^4 = 49 \equiv 20, 68=400≡236^8 = 400 \equiv 23, 616=529≡76^{16} = 529 \equiv 7, 632≡206^{32} \equiv 20, 664≡236^{64} \equiv 23. Multiply the ones whose bit is 1: 6⋅23⋅236 \cdot 23 \cdot 23. 6⋅23=138≡226 \cdot 23 = 138 \equiv 22, 22⋅23=506≡13(mod29)22 \cdot 23 = 506 \equiv 13 \pmod{29}.

Question 9medium

In RSA, let p=5p = 5, q=11q = 11 and e=3e = 3. Find nn, φ(n)\varphi(n) and the private key dd.

Show answer
n = 55, phi(n) = 40, d = 27

n=5⋅11=55n = 5 \cdot 11 = 55, φ(n)=(p−1)(q−1)=4⋅10=40\varphi(n) = (p-1)(q-1) = 4 \cdot 10 = 40. gcd⁡(3,40)=1\gcd(3, 40) = 1, so the inverse exists. d=3−1 mod 40d = 3^{-1} \bmod 40: 3⋅27=81=2⋅40+13 \cdot 27 = 81 = 2 \cdot 40 + 1, so d=27d = 27.

Question 10hard

With the RSA key n=55n = 55, e=3e = 3, d=27d = 27, encrypt M=8M = 8. What do you get when you decrypt the result?

Show answer
C = 17; decrypting gives 8

C=83 mod 55=512 mod 55C = 8^3 \bmod 55 = 512 \bmod 55. 55⋅9=49555 \cdot 9 = 495, 512−495=17512 - 495 = 17. Decryption is 1727 mod 5517^{27} \bmod 55; with fast exponentiation you get 88, and the message is back. This is the check that RSA was set up correctly.

Question 11hard

An RSA public key is (n,e)=(77,7)(n, e) = (77, 7). Find dd and decrypt the ciphertext C=37C = 37.

Show answer
d = 43, M = 9

77=7⋅1177 = 7 \cdot 11, φ=6⋅10=60\varphi = 6 \cdot 10 = 60. d=7−1 mod 60d = 7^{-1} \bmod 60: 7⋅43=301=5⋅60+17 \cdot 43 = 301 = 5 \cdot 60 + 1, d=43d = 43. Decryption: 3743 mod 77=937^{43} \bmod 77 = 9. To check, encrypt in the forward direction: 92=81≡49^2 = 81 \equiv 4, 94≡169^4 \equiv 16, 97=94⋅92⋅9≡16⋅4⋅9=576≡379^7 = 9^4 \cdot 9^2 \cdot 9 \equiv 16 \cdot 4 \cdot 9 = 576 \equiv 37. It matches.

Question 12easy

Why must the RSA exponent ee satisfy gcd⁡(e,φ(n))=1\gcd(e, \varphi(n)) = 1? Illustrate with φ(n)=40\varphi(n) = 40, e=4e = 4.

Show answer
Otherwise e has no inverse d mod phi(n), so there is no decryption key.

The private key dd is the number with e⋅d≡1(modφ(n))e \cdot d \equiv 1 \pmod{\varphi(n)}. This inverse exists only if gcd⁡(e,φ(n))=1\gcd(e, \varphi(n)) = 1. For e=4e = 4, φ=40\varphi = 40 we have gcd⁡=4\gcd = 4: 4d4d is always a multiple of 4, while 40k+140k + 1 is odd, so they cannot be equal. There is no dd, 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).

Review card

Letter -> number: A=0 ... Z=25, every operation mod 26 Shift: c = (p + k) mod 26, decrypt: p = (c - k) mod 26 (Caesar: k = 3) Affine: c = (ap + b) mod 26, requires gcd(a,26)=1; decrypt: p = a^-1 (c - b) mod 26 Valid a: 1,3,5,7,9,11,15,17,19,21,23,25 (even numbers and 13 do not work) a^-1 mod m: Euclid + substitute back -> 1 = s*a + t*m -> a^-1 = s mod m If negative, add m: -11 mod 26 = 15. Check: a * a^-1 mod m = 1 Fast exponentiation: write the exponent in binary, square in turn (mod at each step), multiply the ones whose bit is 1 RSA: p, q prime; n = pq; phi = (p-1)(q-1); gcd(e, phi) = 1; d = e^-1 mod phi Public key (n, e), private d. Encrypt C = M^e mod n, decrypt M = C^d mod n Security: factoring n is hard, d cannot be found without phi Check: decrypt(encrypt(M)) = M must hold