More Modular Arithmetic: Multiplicative Inverses and Afine Ciphers The integers 2, 3, 5, 7, 11, 13, 17, 19, 23, . . . are primes, that is, each has no proper divisors other than 1 and itself. Fundamental Theorem of Arithmetic Every positive integer is either a prime or product of powers of primes. Moreover, there is only one such factorization, up to the order of the factors. Example 588 = 2 · 294 = 2 · 2 · 147 = 2 · 2 · 3 · 49 = 2 · 2 · 3 · 7 · 7 = 22 · 3 · 72. This is the prime factorization of 588. 1 Two positive integer integers a and b are relatively prime if they have no common prime factors. Equivalently, a and b are relatively prime if their greatest common divisor is 1: a and b are relatively prime if gcd(a, b) = 1. . Example 588 and 605 are relatively prime because 588 = 22 ·3·72 and 605 = 5 · 112 have no common prime factors. 2 Solving Congruences ax ≡ 1 (mod m) Multiplication Tables can be helpful for this process Multiplication Tables · (mod 2) 0 1 0 0 0 1 0 1 · (mod 4) 0 1 2 3 0 0 0 0 0 1 0 1 2 3 2 0 2 0 2 3 0 3 2 1 · (mod 3) 0 1 2 0 0 0 0 1 0 1 2 2 0 2 1 · (mod 5) 0 1 2 3 4 0 0 0 0 0 0 1 0 1 2 3 4 2 0 2 4 1 3 3 0 3 1 4 2 4 0 4 3 2 1 3 Example: Solve 3x ≡ 1 (mod 5). Solution: By the mod-5 multiplication table x = 2. Also x = 7, 12, . . . are solutions. ! Example: Solve 5x ≡ 1 (mod 6). Solution: Only need to look at possible values x = 0, 1, 2, 3, 4, 5. 5 · 0 ≡ 0, 5 · 1 ≡ 5, 5 · 2 ≡ 10 ≡ 4 5 · 3 ≡ 15 ≡ 3, 5 · 4 ≡ 20 ≡ 2, 5 · 5 ≡ 25 ≡ 1 So x ≡ 5 is a solution. Also x ≡ 11, 17, . . .. ! 4 Example: Solve 4x ≡ 1 (mod 6). Solution: 4 · 0 ≡ 0, 4 · 1 ≡ 4, 4 · 2 ≡ 8 ≡ 2, 4 · 3 ≡ 12 ≡ 0, 4 · 4 ≡ 16 ≡ 4, 4 · 5 ≡ 20 ≡ 2. The product 4x is never 1; there is no solution. ! 5 When is there a solution x of the congruence a · x ≡ 1 (mod m)? I.e. What is the relationship between a and m? Example: Is there a solution of 13x ≡ 1 (mod 64)? Solution: Yes. Look at 13 · 1 13 · 2 13 · 3 13 · 63 MOD 64 MOD 64 ... MOD 64 MOD 64 All of these numbers are in the range 1, . . ., 63. 6 If 1 is not in this list, then two numbers must be the same. That is, 13 · i ≡ 13 · j (mod 64) for some i "= j. Then 13(j − i) ≡ 0 (mod 64), so 13(j − i) is a multiple of 64. Because 13 and 64 are relatively prime, j − i is a multiple of 64, which is not possible. Thus 1 is in the list: 13 · x ≡ 1 (mod 64) for some x among 1, 2, . . ., 63. ! 7 Note: The proof type we use is by contradiction or reductio ad absurdum. The same line of reasoning proves the following theorem. Theorem: The congruence ax ≡ 1 (mod m) has a solution x if and only if a and m are relatively prime. The solution in the range 1 to m − 1 is denoted a−1 or a−1 (mod m), and called the multiplicative inverse of a modulo m. Property: a · a−1 ≡ 1 (mod m). 8 Example: Find 5−1 (mod 14). Solution: Solve 5x ≡ 1 (mod 14). 5 · 1 ≡ 5, 5 · 2 ≡ 10, 5 · 3 ≡ 15 ≡ 1 (mod 14), so the solution is x = 3 and thus 5−1 ≡ 3 (mod 14). (Check: 3 · 5 ≡ 15 ≡ 1 (mod 14).) 9 Property: If and then a ≡ b (mod m) c ≡ d (mod m) ac ≡ bd (mod m) Example: Solve 5x ≡ 12 (mod 14). Solution: Because 5−1 ≡ 3 (mod 14), multiply the congruences: 5−1 · 5x ≡ 3 · 12 (mod 14) 1 · x ≡ 36 (mod 14) x ≡ 8 (mod 14) 10 Example: Solve 4x + 2 ≡ 1 (mod 9). Solution: Strategy: substract 2 from both sides of the congruence and multiply both sides by the multiplicative inverse of 4 modulo 9. The first yields 4x ≡ 1 − 2 ≡ −1 Also 4 · 7 ≡ 28 ≡ 1 (mod 9) so 4−1 ≡ 7 (mod 9). Then 4−1 · 4 · x ≡ (−1) · 7 (mod 9) x ≡ −7 ≡ 2 (mod 9). ! 11 Cryptography with Modular Arithmetic Key Questions: Which numbers a in the range 0 to 25 have multiplicative inverses modulo 26? What are those inverses? Partial Answer: By the Theorem, the numbers with multiplicative inverses are those in the range 0 to 25 relatively prime to 26. So 1, 3, 5, . . . have multiplicative inverses. 12 Table of Multiplicative inverses Modulo 26 a a−1 (mod 26) 1 1 3 9 5 21 7 15 9 3 11 19 15 7 17 23 19 11 21 5 23 17 25 25 Idea: Use an affine cipher E(x) = (ax + b) MOD 26 to encipher plaintext letter numerical equivalent x, where a is relatively prime to 26. 13 To find the decipherment formula solve y ≡ ax + b (mod 26) for x in terms of y: y − b ≡ ax a−1(y − b) ≡ a−1 · a · x a−1(y − b) ≡ x x ≡ a−1(y − b) (mod 26) So decipherment of letter y is D(y) = a−1(y − b) MOD 26. 14 Example: Encipher ITS COOL with E(x) = (5x + 8) MOD 26. Solution: x 5x + 8 (5x + 8) MOD 26 y I 8 48 22 W T 19 103 25 Z S 18 98 20 U C 2 18 18 S O 14 78 0 A O 14 78 0 A L 11 63 11 L 15 This can also be done in a spreadsheet, as shown here A 1 2 3 4 5 6 7 B 5 C D E F G H I J 8 I W T Z S U C S O A O A L L =CHAR(65 + MOD($A$1 * (CODE(B2)-65) + $B$1, 26)) 16 Example: If H P C C X A Q was enciphered with E(x) = (5x + 8) MOD 26, find the plaintext. Solution: Find the decipherment formula: y y−8 5−1(y − 8) 21(y − 8) ≡ ≡ ≡ ≡ 5x + 8 (mod 26) 5x 5−1 · 5 · x x Decipherment: D(y) = 21(y − 8) MOD 26: cipher y y−8 21(y − 8) MOD 26 plain H 7 -1 -21 5 F P 15 7 147 17 R C 2 -6 -126 4 E C 2 -6 -126 4 E X 23 15 315 3 D A 0 -8 -168 14 O Q 16 8 168 12 M 17 The decipherment can also be implemented in a spreadsheet as shown here 1 2 3 4 5 6 7 8 9 A 21 B 8 H F C P R D C E E C E F X D G A O H I J K L M Q M =CHAR(65 + MOD($A$1*(CODE(B3) - 65 - $B$1),26)) Notice that the decryption formula is condensed from earlier ones. 18 Example: (Cryptanalysis) Suppose an affine cipher E(x) = (ax + b) MOD 26 enciphers H as X and Q as Y. Find the cipher. Solution: H → X means E(7) = 23 Q → Y means E(16) = 24 I.e. a · 7 + b ≡ 23 (mod 26) (1) a · 16 + b ≡ 24 (2) 19 Subtract (1) from (2) to eliminate b: a · 16 − a · 7 ≡ 24 − 23 a(16 − 7) ≡ 1 9a ≡ 1 a ≡ 9−1 ≡ 3 So b ≡ 23 − 7a ≡ 23 − 7 · 3 ≡ 23 − 21 ≡ 2 (mod 26) Thus E(x) = (3x + 2) MOD 26. 20 Brute-force Cryptanalysis of an Affine Cipher Suppose that Eve has intercepted WLMHL UPUHI FMIEC LNMWK CVU which she suspects to have come from an affine cipher E(x) = (ax + b) MOD 26. What is involved in a brute force cryptanalysis in which she tries out choices of a and b until she hits one that yields recognizable plaintext? 21 Solution The decipherment formula is D(y) = a−1(y − b) MOD 26 = (a−1)y + (a−1b) MOD 26, so Eve can decipher the message with every possible choice of a−1 and a−1b and hope that she can recognize plaintext. The choices of these two numbers will tell her the right values for a and b. The spreadsheet on the next slide implements this strategy. By plugging in values of 1, 3, 5, . . . to the cell marked a−1, she eventually arrives at the page shown. There is recognizable plaintext in spreadsheet row 25. 22 A B 1 C D E F G H I J K L M N O P Q R S T U V W X Y Cryptanalysis of an affine encipherment: W L M H L U P U H I F M I E C L N M W K C V U came from E(x) = (a x + b) MOD 26. Find a and b by brute force. Decipherment is D(y) = a-1 (y - b) MOD 26 = (a-1 y + (- a-1 b) ) MOD 26. Try deciphering systematically using every possible b value for each possible a. a-1-value 2 3 7 4 5 6 7 8 9 10 11 1 2 -1 1 3 a b-values 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 =CHAR(65+MOD($A$4*(CODE(C$4)-CODE("A"))+$B5,26)) Fill C5 right and then down, and then change the value in A4 until a plaintext is visible one on of the lines in the array. 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 W Y Z A B C D E F G H I J K L M N O P Q R S T U V W X L Z A B C D E F G H I J K L M N O P Q R S T U V W X Y M G H I J K L M N O P Q R S T U V W X Y Z A B C D E F H X Y Z A B C D E F G H I J K L M N O P Q R S T U V W L Z A B C D E F G H I J K L M N O P Q R S T U V W X Y U K L M N O P Q R S T U V W X Y Z A B C D E F G H I J P B C D E F G H I J K L M N O P Q R S T U V W X Y Z A U K L M N O P Q R S T U V W X Y Z A B C D E F G H I J H X Y Z A B C D E F G H I J K L M N O P Q R S T U V W I E F G H I J K L M N O P Q R S T U V W X Y Z A B C D F J K L M N O P Q R S T U V W X Y Z A B C D E F G H I M G H I J K L M N O P Q R S T U V W X Y Z A B C D E F I E F G H I J K L M N O P Q R S T U V W X Y Z A B C D E C D E F G H I J K L M N O P Q R S T U V W X Y Z A B C O P Q R S T U V W X Y Z A B C D E F G H I J K L M N L Z A B C D E F G H I J K L M N O P Q R S T U V W X Y N N O P Q R S T U V W X Y Z A B C D E F G H I J K L M M G H I J K L M N O P Q R S T U V W X Y Z A B C D E F W Y Z A B C D E F G H I J K L M N O P Q R S T U V W X K S T U V W X Y Z A B C D E F G H I J K L M N O P Q R C O P Q R S T U V W X Y Z A B C D E F G H I J K L M N V R S T U V W X Y Z A B C D E F G H I J K L M N O P Q U K L M N O P Q R S T U V W X Y Z A B C D E F G H I J 23 Thus a−1 = 7, so a = 15; then a−1b = 20, so b ≡ 20a ≡ 300 ≡ 14 (mod 26). 24