An Introduction to Hill Ciphers Using inear Algebra Brian Worthington October 26, 2010 University of North Texas MATH 2700.002 1
Contents 1 Introduction 3 1.1 Substitution Ciphers......................... 3 1.2 Hill Ciphers.............................. 3 1.3 Alphabets............................... 6 2 Modular Arithmetic 7 2.1 Modulus................................ 7 2.2 Modular Inverses........................... 3 Matrices 3.1 Invertible Matrices.......................... 3.2 Adjugates............................... 9 4 Hill-2 Ciphers 10 4.1 Choosing Matrices.......................... 10 4.2 Encryption.............................. 4.3 Decryption.............................. 13 5 Hill-3 Ciphers 16 5.1 Choosing Matrices.......................... 16 5.2 Encryption.............................. 1 5.3 Decryption.............................. 21 6 Conclusion 23 7 References 24 2
1 Introduction Hill ciphers were invented by ester S. Hill in 1929. A Hill cipher is a type of polygraphic substitution cipher. A Hill cipher uses linear algebraic operations on an n n matrix to encrypt and decrypt messages. Hill ciphers were the rst polygraphic ciphers where it was practical to operate on more than three symbols at once. 1.1 Substitution Ciphers Substitution ciphers work by substituting one letter with another using a set of rules that can be reversed to decrypt an encrypted message. Simple substitution ciphers simply change one letter to another. You can see a simple substitution assignment table in Table I. Table I: etter to etter Substitution A B C D E F G H I J K M N O P Q R S T U V W X Y Z Q W E R T Y U I O P A S D F G H J K Z X C V B N M In order to encrypt the message HEO WORD, you simply replace each letter with the corresponding letter below it to get the message ITSSG VGKSR. The encrypted message may look like gibberish, but simple substitution ciphers are relatively easy to break using frequency analysis. Frequency analysis uses the fact that the letter E is more likely to appear in an English phrase than the letter X, etc. If you look at the encrypted message, the two 's in HEO appear as S's in ITSSG giving even more clues to someone trying to crack the code. A polygraphic cipher encrypts letters in larger groups, making frequency analysis more dicult. Encrypting HEO WORD using a polygraphic cipher might yield something like JVDNE ANAEZ. Note that the 's in HEO WORD no longer give an easy clue as to what the word might be. Polygraphic ciphers are not unbreakable and are still vulnerable to frequency analysis, but it takes a much larger sample of encrypted data to break the code than a simple substitution cipher. 1.2 Hill Ciphers Hill ciphers are a type of polygraphic substitution cipher. They use matrices and linear algebraic operations to encrypt and decrypt messages. Encrypting a message with a Hill cipher starts by assigning each letter in the alphabet a unique number between 0 and 25. You can see a typical assignment in Table II. 3
Table II: etter to Number Substitution A B C D E F G H I J K M N O P Q R S T U V W X Y Z 0 1 2 5 6 7 9 10 12 13 14 17 1 19 20 21 22 23 24 25 An n n matrix is chosen to encrypt the message. This n n matrix needs to have certain properties for an encrypted message to be able to be decrypted. These properties will be explained later on in this paper. For right now the 1 2 matrix A will be used. Once the encryption matrix has been chosen, the message to be encrypted needs to be broken into n 1 vectors. To encrypt HI CIPHER, using matrix A, break it into 2 x 1 vectors: H I C I P H E R Use Table II to convert each letter in the vectors to its representative number: H I 7 C I 2 P H 16 7 E R 4 17 4
Multiply each of these numeric vectors by the matrix A to get the encrypted vectors: 1 2 1 2 7 7 + 16 21 + 32 + 22 33 + 44 23 53 33 77 1 2 1 2 2 16 7 2 + 16 6 + 32 16 + 14 4 + 2 1 3 30 76 1 2 4 17 4 + 34 12 + 6 3 0 To get the encrypted message, you should be able to use Table II to change the encrypted message from numbers back to letters, but there is one more step. 23 If you look at the rst encrypted vector,, the rst number, 23, can be 53 changed to X using Table II, but there is no entry in Table II for 53. In order to use Table II, you need numbers between 0 and 25. If you take the Modulus 26 of 53, you will get 1, which is between 0 and 25 and can be translated to B using Table II. Modulus will be discussed later in this paper. 5
If you take the Modulus 26 of each of the encrypted vectors, you get: 23 53 33 77 1 3 30 76 Mod 26 Mod 26 Mod 26 Mod 26 23 1 7 25 1 12 4 24 3 0 12 Mod 26 2 Now you can take the encrypted vectors and get encrypted letter values from Table II to get the encrypted message XBRZSMEYMC. The example above is an example of a Hill-2 Cipher. The 2 comes from the number of columns in the encryption and decryption matrices. A Hill cipher using 4 4 matrices is known as a Hill-4 Cipher, etc. 1.3 Alphabets In the examples used in this paper Table II from section 1.2 will be used to convert letters into numbers and vice versa. This table uses the standard 26 character alphabet used in English. Because you have 26 characters that you will use to encrypt and decrypt, you need to make sure that everything can be converted into an actual letter. The letters are assigned numbers between 0 and 25 so that you can take advantage of Mod 26 arithmetic to convert any number into a number between 0 and 25. Hill ciphers do not have to use exactly 26 characters. Using Hill ciphers with non-english character sets can yield alphabets with more or less than 26 characters. You can also include space and punctuation characters. Also, the numbers do not need to be in order. It helps to start with 0 in your numbering to take advantage of modular arithmetic. Of course, if your alphabet uses more or less than 26 characters, make sure you use the appropriate Mod m instead of Mod 26. The key is that the same alphabet with the same letter to number designations need to be available for the encryptor and decryptor and that there are no gaps in the numbering. 6
2 Modular Arithmetic 2.1 Modulus A modulus of one number in terms of another number (a Mod b) for integers a and b basically means that you divide a by b and take the remainder as the answer. For example: therefore, also, 14 10 1 R 4 14 Mod 10 4 24 Mod 10 4 Here is a theorem for how to calculate modulus: THEOREM: For an integer a and modulus m, let R remainder of a m Then the residue r of a modulus m is given by: R if a 0 r m R if a < 0 and R 0 0 if a < 0 and R 0 When you are using Hill ciphers, you will use modulus (specically Mod 26 for the alphabet used in this paper) in every step to make sure that you are dealing with numbers between 0 and 25. Here are some examples: 1. 0 Mod 26 0 26 3 R 2 0 0 0 Mod 26 2 2. -0 Mod 26 3. -52 Mod 26 0 26 3 R 2-0 < 0 and 2 0-0 Mod 26 26-2 24 52 26 2 R 0-52 < 0 and 0 0-52 Mod 26 0 7
2.2 Modular Inverses In regular arithmetic, every non-zero number a has a reciprocal (or inverse) a 1 such that: a a 1 1 For example, the inverse of 23 is 1 23 because 23 1 23 1. In modular arithmetic, a number a has a modular inverse a 1 for a number m such that: (a a 1 ) Mod m 1 IMPORTANT NOTE: For modular inverses, a and m must not have any common prime factors in order for a 1 to exist. Example: Find the modular inverse of 9 for Mod 26. Solution: 9 3 27 27 Mod 26 1 3 is the modular inverse of 9 for Mod 26. In order to create matrices for Hill ciphers, you will need to know the modular inverses for Mod 26. Because 26 has only 2 prime factors (2 and 13), the numbers for which there is a modular inverse for Mod 26 include all the odd numbers between 1 and 25 except for 13. Table III shows all of the possible modular inverses for Mod 26. Table III: Modular Inverses for Mod 26 a 1 2 5 7 9 15 17 19 21 23 25 a 1 1 9 21 15 3 19 7 23 5 17 25 3 Matrices 3.1 Invertible Matrices In order for a matrix to be invertible, it must rst be a square n n matrix. It must also have a determinant 0. a b For a 2 2 matrix A, the determinant of A is ad - bc, written as c d det A ad - bc
For square matrices larger than 2 2 it gets a little more complicated: DEFINITION: For n 2, the determinant of an n n matrix A a ij is the sum of n terms of the form ±a 1j det A 1j, with plus and minus signs alternating, where the entries a, a 12,..., a 1n are from the rst row of A. In symbols det A a det A - a 12 det A 12 + + (-1) 1+n a 1n det A 1n a b c So, for a 3 3 matrix B d e f, g h i e f det B a det h i d f - b det g i d e + c g h det B a (ei - fh) - b (di - fg) + c (dh - eg) 3.2 Adjugates An adjugate of a square matrix can be used to generate an inverse of that matrix. You will need to use adjugates to calculate the modular inverse your encryption matrix to use in decryption. To create an adjugate of a square, n n invertible matrix A, take all of the cofactors of A, put them into a matrix B and then take the transpose of B, written as B T. a b For a 2 2 matrix A, the cofactors are: c d C + d C 12 - c C 21 - b C 22 + a d c The resulting matrix B b a d b and B T c a d b Therefore, adj A c a 9
4 Hill-2 Ciphers 4.1 Choosing Matrices A Hill-2 Cipher uses a 2 2 invertible matrix A to encrypt data and another 2 2 invertible matrix B to decrypt data. The relationship between A and B is that they need to be modular inverses of each other for Mod 26. This means you need to choose a matrix A where there exists a matrix B such that: (A B) Mod 26 I n where I n is the identity matrix for n n matrices. In order to create B, you must rst calculate the determinant of A. et A 2 1 det A (2 4) - (1 3) - 3 5 modular inverse of 5 for Mod 26 21 (from Table III) If det A did not have a modular inverse for Mod 26, you would NOT be able to use A to encrypt and decrypt Hill ciphers. IMPORTANT NOTE: In order for a n n matrix A to be modularly invertible for Mod m, det A MUST have a modular inverse for Mod m. a b You saw in section 3.2 that the adjudicate of a 2 2 matrix c d d b. Therefore c a adj A 4 1 3 2 To calculate B you need to multiply the adjucate of A by the modular inverse of det A for Mod m and then calculate Mod m for the entries in the resulting matrix. For example: B 21 4 1 3 2 4 21 62 B 4 21 62 Mod 26 6 5 10
6 5 Therefore 2 1 is the modular inverse of 6 5 for Mod 26. is the matrix you would use to decrypt a Hill cipher encrypted using To check that 2 1 6 5 6 5 is the modular inverse of 27 26 7 79 12 + 15 10 + 16 1 + 60 15 + 64 Mod 26 1 0 0 1 2 1 I 2 : 27 26 7 79 2 1. 4.2 Encryption Here is how to encrypt the phrase HEO WORD. You can use the matrix 2 1 to encrypt it because you know it has a modular inverse from the 6 5 previous section ( ). 1. Divide the phrase to encrypt into 2 1 vectors and assign them numbers using Table II. H E 7 4 O W O R 14 22 14 17 D 3
2 1 2. Multiply each of the numerical vectors by the encryption matrix,. 2 1 7 4 14 + 4 21 + 16 1 37 2 1 2 1 2 1 14 22 14 17 22 + 33 + 44 2 + 22 42 + 2 + 17 42 + 6 33 77 50 130 45 0 2 1 3 22 + 3 33 + 12 25 45 3. Convert each of the resulting numerical vectors to Mod 26. 1 37 33 77 Mod 26 Mod 26 1 7 25 50 130 45 0 24 Mod 26 0 Mod 26 19 6 25 45 Mod 26 25 19 12
4. Use Table II to convert each of the values in the resulting vectors back to letters. 1 S 7 25 H Z 24 0 Y A 19 6 T G 25 19 Z T 5. The encrypted message now reads SHZYATGZT. 4.3 Decryption The process to decrypt the phrase SHZYATGZT similar to encryption, except we need to use the modular inverse of the original encryption matrix, which is 6 5. 13
1. Divide the phrase to encrypt into 2 1 vectors and assign them numbers using Table II. S 1 H Z 7 25 Y A 24 0 T G 19 6 Z T 25 19 2. Multiply each of the numerical vectors by the decryption matrix, 6 5 6 5 1 7 25 10 + 55 270 + 176 42 + 125 105 + 400 163 446 167 505 6 5. 6 5 6 5 24 0 19 6 144 + 0 360 + 0 4 + 30 25 + 96 144 360 144 31 6 5 25 19 150 + 95 375 + 304 245 679 14
3. Convert each of the resulting numerical vectors to Mod 26. 163 446 167 505 144 360 144 31 7 Mod 26 4 Mod 26 Mod 26 Mod 26 14 22 14 17 245 679 Mod 26 3 4. Use Table II to convert each of the values in the resulting vectors back to letters. 7 H 4 E 14 22 14 17 O W O R 3 D 5. The decrypted message is now HEOWORD which can easily be deciphered as HEO WORD. 15
5 Hill-3 Ciphers 5.1 Choosing Matrices A Hill-3 Cipher uses a 3 3 invertible matrix A to encrypt data and another 3 x 3 invertible matrix B to decrypt data. You might choose a matrix A 3 2 1. 1 2 3 2 1 3 If you calculate the det A you get: 2 1 1 det 1 3 3 1-2 det 2 3 3 2 3 det 2 1 1 (6 + 1) 1 7 7-2 (-9-2) -2-22 3 (3-4) 3-1 -3 det A 7 + 22-3 26 Mod 26 0 If you look at Table III, there is no entry for 0, so there is no modular inverse. Because of this, matrix A won't work. While you could certainly encrypt messages with matrix A, there would be no way to decrypt them. If you modify matrix A to 3 1 1 det 1 2 2 1-2 det 3 2 2 3-2 det 3 1 1 2 2 2 3 1 3 1 2, here is the determinant: 1 (6 + 1) 1 7 7-2 (-4-3) -2-7 14-2 (2-9) -2-7 14 det A 7 + 14 + 14 35 Mod 26 9 16
If you look at Table III, you can see that the modular inverse of 9 3. To calculate B you need to multiply the adjucate of A (adj A) by the modular inverse of det A for Mod m and then calculate Mod m for the entries in the resulting matrix. To calculate adj A: 3 1 C + det 1 2 2 2 C 21 - det 1 2 2 2 C 31 + det 3 1 2 1 C 12 - det 3 2 1 2 C 22 + det 3 2 1 2 C 32 - det 2 1 2 3 C 13 + det 3 1 1 2 C 23 - det 3 1 1 2 C 33 + det 2 3 C (6 - (-1)) 7 C 12 -(-4-3) 7 C 13 (2-9) -7 C 21 -(4-2) -2 C 22 (2 - (-6)) C 23 -(-1-6) 7 C 31 (2 - (-6)) C 32 -(1-4) 3 C 33 (3 - (-4)) 7 You can take the results and put them into a new matrix C and now adj A C T. adj A 7 3 7 2 7 7 7 Now you can calculate the modular inverse of A: 7 2 21 6 24 B 3 7 3 21 24 9 7 7 7 21 21 21 B 21 6 24 21 24 9 21 21 21 Mod 26 21 20 24 Therefore 21 24 9 is the modular inverse of 5 21 21 21 20 24 Mod 26. 21 24 9 5 21 21 1 2 2 encrypted using 2 3 1. 3 1 2 21 20 24 21 24 9 5 21 21 7 7 7 2 7 3 7 1 2 2 2 3 1 3 1 2 for is the matrix you would use to decrypt a Hill cipher 17
To check that 21 20 24 21 24 9 5 21 21 2 3 1 1 2 2 3 1 2 is the modular inverse of 21 20 24 21 24 9 5 21 21 2 3 1 1 2 2 3 1 2 : 21 + 42 10 20 + 4 42 24 + 1 42 42 + 63 + 5 40 + 72 + 21 4 + 27 + 21 63 21 + 10 60 24 + 42 72 9 + 42 53 26 0 26 53 0 52 7 105 Mod 26 1 0 0 0 1 0 0 0 1 I 3 53 26 0 26 53 0 52 7 105 5.2 Encryption Here is how to encrypt thephrase HEO WORD using a Hill-3 Cipher. 1 2 2 The encryption matrix A 2 3 1 and the decryption matrix B 3 1 2 21 20 24 21 24 9. 5 21 21 1
1. Divide the phrase to encrypt into 3 1 vectors and assign them numbers using Table II. H E 7 4 O W 14 22 O R D X X 14 17 3 23 23 You will notice that there are not enough letters to completely ll out the last vector. One common solution is to just repeat the nal letter to ll out the nal vector. Another solution, the one used here, is to ll out the nal vector with uncommonly used letters, such as X. 2. Multiply each of the numerical vectors by the encryption matrix, 2 3 1 1 2 2 3 1 2 7 4 7 + 22 14 + 12 + 21 4 + 22 7 9 39 1 2 2 2 3 1 3 1 2. 1 2 2 2 3 1 3 1 2 14 22 + 2 44 22 + 42 + 22 33 14 + 44 5 42 63 1 2 2 2 3 1 3 1 2 2 3 1 1 2 2 3 1 2 14 17 3 23 23 14 + 34 + 22 2 + 51 + 42 17 + 22 3 + 46 46 6 + 69 + 23 9 23 + 46 26 34 47 3 6 32 19
3. Convert each of the resulting numerical vectors to Mod 26. 7 9 39 Mod 26 19 9 13 5 42 63 Mod 26 21 16 26 34 47 3 6 32 Mod 26 Mod 26 4. Use Table II to convert each of the values in the resulting vectors back to letters. 19 9 13 21 16 T J N V Q 0 21 3 6 0 21 A I V 3 6 5. The encrypted message now reads TJNVQAIVDIG. D I G 20
5.3 Decryption The process to decrypt the phrase TJNVQAIVDIG similar to encryption, except we need to use the modular inverse of the original encryption matrix, 21 20 24 which is 21 24 9. 5 21 21 1. Divide the phrase to encrypt into 2 1 vectors and assign them numbers using Table II. T J N V Q 19 9 13 21 16 A I V 0 21 D I G 3 6 21
2. Multiply each of the numerical vectors by the decryption matrix, 21 20 24 21 24 9 5 21 21 19 9 13 399 + 10 + 312 299 + 216 + 7 95 + 19 + 273 91 732 557 21 20 24 21 24 9 5 21 21. 21 20 24 21 24 9 5 21 21 21 16 441 + 320 + 264 441 + 34 + 99 105 + 336 + 231 1025 924 672 21 20 24 21 24 9 5 21 21 21 20 24 21 24 9 5 21 21 0 21 3 6 0 + 160 + 504 0 + 192 + 19 0 + 16 + 441 63 + 160 + 144 63 + 192 + 54 15 + 16 + 126 3. Convert each of the resulting numerical vectors to Mod 26. 91 732 557 Mod 26 7 4 664 31 609 367 309 309 1025 924 672 Mod 26 14 22 664 31 609 367 309 309 Mod 26 Mod 26 14 17 3 23 23 22
4. Use Table II to convert each of the values in the resulting vectors back to letters. 7 4 H E 14 22 O W 14 17 3 23 23 5. The decrypted message is now HEOWORDXX which can easily be deciphered as HEO WORD. O R D X X 6 Conclusion The most dicult part of creating Hill ciphers is determining the encryption and decryption matrices. Once you have a viable encryption matrix and its corresponding decryption matrix, it is relatively easy to encrypt and decrypt messages, if you have a working knowledge of matrix arithmetic and how modulus works. 23
7 References ester S. Hill, Cryptography in an Algebraic Alphabet, The American Mathematical Monthly Vol. 36, June-July 1929, pp. 306-312 ester S. Hill, Concerning Certain inear Transformation Apparatus of Cryptography, The American Mathematical Monthly Vol. 3, 1931, pp.135-154 Howard Anton, Chris Rorres, Elementary inear Algebra with Applications, John Wiley and Sons, Inc., New York, NY, pp. 719-732 David C. ay, inear Algebra and Its Applications, Pearson, Addison Wesley, Boston, Massachusetts, pp 121, 17, 203-204 http://online.redwoods.edu/instruct/darnold/aproj/fall2001/shinichi/cryptography.pdf http://www.apprendre-en-ligne.net/crypto/menu/index.html http://en.wikipedia.org/wiki/hill_cipher 24