an introduction to hill ciphers using linear algebratushar/s10linear2700...

24

Upload: vuongnhu

Post on 18-Aug-2018

227 views

Category:

Documents


0 download

TRANSCRIPT

An Introduction to Hill Ciphers Using

Linear 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 . . . . . . . . . . . . . . . . . . . . . . . . . . . 8

3 Matrices 8

3.1 Invertible Matrices . . . . . . . . . . . . . . . . . . . . . . . . . . 8

3.2 Adjugates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9

4 Hill-2 Ciphers 10

4.1 Choosing Matrices . . . . . . . . . . . . . . . . . . . . . . . . . . 10

4.2 Encryption . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11

4.3 Decryption . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13

5 Hill-3 Ciphers 16

5.1 Choosing Matrices . . . . . . . . . . . . . . . . . . . . . . . . . . 16

5.2 Encryption . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18

5.3 Decryption . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21

6 Conclusion 23

7 References 24

2

1 Introduction

Hill ciphers were invented by Lester S. Hill in 1929. A Hill cipher is a type ofpolygraphic substitution cipher. A Hill cipher uses linear algebraic operationson 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 threesymbols at once.

1.1 Substitution Ciphers

Substitution ciphers work by substituting one letter with another using a set ofrules that can be reversed to decrypt an encrypted message. Simple substitutionciphers simply change one letter to another. You can see a simple substitutionassignment table in Table I.

Table I: Letter to Letter Substitution

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 Z

Q W E R T Y U I O P A S D F G H J K L Z X C V B N M

In order to encrypt the message HELLO WORLD, you simply replace each letterwith the corresponding letter below it to get the message ITSSG VGKSR. Theencrypted message may look like gibberish, but simple substitution ciphers arerelatively easy to break using frequency analysis. Frequency analysis uses thefact that the letter E is more likely to appear in an English phrase than theletter X, etc. If you look at the encrypted message, the two L's in HELLO

appear as S 's in ITSSG giving even more clues to someone trying to crack thecode.

A polygraphic cipher encrypts letters in larger groups, making frequency anal-ysis more di�cult. Encrypting HELLO WORLD using a polygraphic ciphermight yield something like JVDNE ANAEZ. Note that the L's in HELLO

WORLD no longer give an easy clue as to what the word might be. Polygraphicciphers are not unbreakable and are still vulnerable to frequency analysis, butit takes a much larger sample of encrypted data to break the code than a simplesubstitution cipher.

1.2 Hill Ciphers

Hill ciphers are a type of polygraphic substitution cipher. They use matricesand linear algebraic operations to encrypt and decrypt messages. Encryptinga message with a Hill cipher starts by assigning each letter in the alphabet aunique number between 0 and 25. You can see a typical assignment in Table II.

3

Table II: Letter to Number Substitution

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 Z

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

An n × n matrix is chosen to encrypt the message. This n × n matrix needsto 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

matrix A =

[1 23 4

]will be used.

Once the encryption matrix has been chosen, the message to be encrypted needsto be broken into n × 1 vectors. To encrypt HILL CIPHER, using matrix A,break it into 2 x 1 vectors:[

HI

][LL

][CI

][PH

][ER

]Use Table II to convert each letter in the vectors to its representative number:[

HI

]=

[78

]

[LL

]=

[1111

]

[CI

]=

[28

]

[PH

]=

[167

]

[ER

]=

[417

]

4

Multiply each of these numeric vectors by the matrix A to get the encryptedvectors: [

1 23 4

]·[

78

]=

[7 + 1621 + 32

]=

[2353

]

[1 23 4

]·[

1111

]=

[11 + 2233 + 44

]=

[3377

]

[1 23 4

]·[

28

]=

[2 + 166 + 32

]=

[1838

]

[1 23 4

]·[

167

]=

[16 + 1448 + 28

]=

[3076

]

[1 23 4

]·[

417

]=

[4 + 3412 + 68

]=

[3880

]To get the encrypted message, you should be able to use Table II to change theencrypted message from numbers back to letters, but there is one more step.

If you look at the �rst encrypted vector,

[2353

], the �rst number, 23, can be

changed to X using Table II, but there is no entry in Table II for 53. In orderto use Table II, you need numbers between 0 and 25. If you take the Modulus26 of 53, you will get 1, which is between 0 and 25 and can be translated to Busing 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:[2353

]Mod 26 =

[231

][

3377

]Mod 26 =

[725

][

1838

]Mod 26 =

[1812

][

3076

]Mod 26 =

[424

][

3880

]Mod 26 =

[122

]Now you can take the encrypted vectors and get encrypted letter values fromTable II to get the encrypted message XBRZSMEYMC.

The example above is an example of a Hill-2 Cipher. The 2 comes from thenumber of columns in the encryption and decryption matrices. A Hill cipherusing 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 toconvert letters into numbers and vice versa. This table uses the standard 26character alphabet used in English. Because you have 26 characters that youwill use to encrypt and decrypt, you need to make sure that everything can beconverted into an actual letter. The letters are assigned numbers between 0 and25 so that you can take advantage of Mod 26 arithmetic to convert any numberinto a number between 0 and 25.

Hill ciphers do not have to use exactly 26 characters. Using Hill ciphers withnon-English character sets can yield alphabets with more or less than 26 charac-ters. You can also include space and punctuation characters. Also, the numbersdo not need to be in order. It helps to start with 0 in your numbering to takeadvantage of modular arithmetic. Of course, if your alphabet uses more or lessthan 26 characters, make sure you use the appropriate Mod m instead of Mod26. The key is that the same alphabet with the same letter to number desig-nations need to be available for the encryptor and decryptor and that there areno 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 integersa and b basically means that you divide a by b and take the remainder as theanswer. For example:

14 ÷ 10 = 1 R 4

therefore,

14 Mod 10 = 4

also,

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 =

R

m−R

0

if a ≥ 0if a < 0 and R 6= 0if a < 0 and R = 0

When you are using Hill ciphers, you will use modulus (speci�cally Mod 26 forthe alphabet used in this paper) in every step to make sure that you are dealingwith numbers between 0 and 25. Here are some examples:

1. 80 Mod 26

|80|26 = 3 R 2 ⇒80 ≥ 0 ⇒80 Mod 26 = 2

2. -80 Mod 26

|−80|26 = 3 R 2 ⇒-80 < 0 and 2 6= 0⇒-80 Mod 26 = 26 - 2 = 24

3. -52 Mod 26

|−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−1such that:

a · a−1 = 1

For example, the inverse of 23 is 123 because 23 · 1

23 = 1.

In modular arithmetic, a number a has a modular inverse a−1 for a number msuch that:

(a · a−1) Mod m = 1

IMPORTANT NOTE: For modular inverses, a and m must not have any com-mon 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 Mod26.

In order to create matrices for Hill ciphers, you will need to know the modularinverses for Mod 26. Because 26 has only 2 prime factors (2 and 13), the numbersfor which there is a modular inverse for Mod 26 include all the odd numbersbetween 1 and 25 except for 13. Table III shows all of the possible modularinverses for Mod 26.

Table III: Modular Inverses for Mod 26

a 1 2 5 7 9 11 15 17 19 21 23 25

a−1 1 9 21 15 3 19 7 23 11 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 6=0.

For a 2 × 2 matrix A =

[a bc d

], the determinant of A is ad - bc, written as

det A = ad - bc

8

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 = [aij ] is the sum of nterms of the form ±a1j det A1j , with plus and minus signs alternating, wherethe entries a11, a12,..., a1n are from the �rst row of A. In symbols

det A = a11det A11 - a12det A12 + · · · + (-1)1+na1ndet A1n

So, for a 3 × 3 matrix B =

a b cd e fg h i

,det B = a · det

[e fh i

]- b · det

[d fg i

]+ c ·

[d eg 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 thatmatrix. You will need to use adjugates to calculate the modular inverse yourencryption matrix to use in decryption.

To create an adjugate of a square, n × n invertible matrix A, take all of thecofactors of A, put them into a matrix B and then take the transpose of B,written as BT .

For a 2 × 2 matrix A =

[a bc d

], the cofactors are:

C11 = +|d | C12 = -|c|

C21 = -|b| C22 = +|a|

The resulting matrix B =

[d −c−b a

]and BT =

[d −b−c a

]Therefore, adj A =

[d −b−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 another2 × 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 meansyou need to choose a matrix A where there exists a matrix B such that:

(A · B) Mod 26 = In

where In is the identity matrix for n × n matrices.

In order to create B, you must �rst calculate the determinant of A.

Let A =

[2 13 4

]

det A = (2 · 4) - (1 · 3) = 8 - 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 touse A to encrypt and decrypt Hill ciphers.

IMPORTANT NOTE: In order for a n × n matrix A to be modularly invertiblefor Mod m, det A MUST have a modular inverse for Mod m.

You saw in section 3.2 that the adjudicate of a 2 × 2 matrix

[a bc d

]=[

d −b−c a

]. Therefore

adj A =

[4 −1−3 2

]To calculate B you need to multiply the adjucate of A by the modular inverseof det A for Mod m and then calculate Mod m for the entries in the resultingmatrix. For example:

B = 21 ·[

4 −1−3 2

]=

[84 −21−63 42

]

B =

[84 −21−63 42

]Mod 26 =

[6 515 16

]

10

Therefore

[6 515 16

]is the modular inverse of

[2 13 4

]for Mod 26.

[6 515 16

]is the matrix you would use to decrypt a Hill cipher encrypted using

[2 13 4

].

To check that

[6 515 16

]is the modular inverse of

[2 13 4

]:

[2 13 4

]·[

6 515 16

]=

[12 + 15 10 + 1618 + 60 15 + 64

]=

[27 2678 79

]

⇒[

27 2678 79

]Mod 26 =

[1 00 1

]= I 2

4.2 Encryption

Here is how to encrypt the phrase HELLO WORLD. You can use the matrix[2 13 4

]to encrypt it because you know it has a modular inverse from the

previous section (

[6 515 16

]).

1. Divide the phrase to encrypt into 2 × 1 vectors and assign them numbersusing Table II. [

HE

]=

[74

]

[LL

]=

[1111

]

[OW

]=

[1422

]

[OR

]=

[1417

]

[LD

]=

[113

]

11

2. Multiply each of the numerical vectors by the encryption matrix,

[2 13 4

].

[2 13 4

]·[

74

]=

[14 + 421 + 16

]=

[1837

]

[2 13 4

]·[

1111

]=

[22 + 1133 + 44

]=

[3377

]

[2 13 4

]·[

1422

]=

[28 + 2242 + 88

]=

[50130

]

[2 13 4

]·[

1417

]=

[28 + 1742 + 68

]=

[45110

]

[2 13 4

]·[

113

]=

[22 + 333 + 12

]=

[2545

]3. Convert each of the resulting numerical vectors to Mod 26.[

1837

]Mod 26 =

[1811

]

[3377

]Mod 26 =

[725

]

[50130

]Mod 26 =

[240

]

[45110

]Mod 26 =

[196

]

[2545

]Mod 26 =

[2519

]

12

4. Use Table II to convert each of the values in the resulting vectors back toletters. [

1811

]=

[SL

]

[725

]=

[HZ

]

[240

]=

[YA

]

[196

]=

[TG

]

[2519

]=

[ZT

]5. The encrypted message now reads SLHZYATGZT.

4.3 Decryption

The process to decrypt the phrase SLHZYATGZT similar to encryption, exceptwe need to use the modular inverse of the original encryption matrix, which is[

6 515 16

].

13

1. Divide the phrase to encrypt into 2 × 1 vectors and assign them numbersusing Table II. [

SL

]=

[1811

]

[HZ

]=

[725

]

[YA

]=

[240

]

[TG

]=

[196

]

[ZT

]=

[2519

]

2. Multiply each of the numerical vectors by the decryption matrix,

[6 515 16

].

[6 515 16

]·[

1811

]=

[108 + 55270 + 176

]=

[163446

]

[6 515 16

]·[

725

]=

[42 + 125105 + 400

]=

[167505

]

[6 515 16

]·[

240

]=

[144 + 0360 + 0

]=

[144360

]

[6 515 16

]·[

196

]=

[114 + 30285 + 96

]=

[144381

]

[6 515 16

]·[

2519

]=

[150 + 95375 + 304

]=

[245679

]

14

3. Convert each of the resulting numerical vectors to Mod 26.[163446

]Mod 26 =

[74

]

[167505

]Mod 26 =

[1111

]

[144360

]Mod 26 =

[1422

]

[144381

]Mod 26 =

[1417

]

[245679

]Mod 26 =

[113

]4. Use Table II to convert each of the values in the resulting vectors back toletters. [

74

]=

[HE

]

[1111

]=

[LL

]

[1422

]=

[OW

]

[1417

]=

[OR

]

[113

]=

[LD

]5. The decrypted message is now HELLOWORLD which can easily be deci-phered as HELLO WORLD.

15

5 Hill-3 Ciphers

5.1 Choosing Matrices

A Hill-3 Cipher uses a 3 × 3 invertible matrix A to encrypt data and another3 x 3 invertible matrix B to decrypt data. You might choose a matrix A = 1 2 3−3 2 12 −1 3

.If you calculate the det A you get:

1 · det[

2 1−1 3

]= 1 · (6 + 1) = 1 · 7 = 7

-2 · det[−3 12 3

]= -2 · (-9 - 2) = -2 · -11 = 22

3 · det[−3 22 −1

]= 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 in-verse. Because of this, matrix A won't work. While you could certainly encryptmessages with matrix A, there would be no way to decrypt them.

If you modify matrix A to

1 2 −2−2 3 13 −1 2

, here is the determinant:

1 · det[

3 1−1 2

]= 1 · (6 + 1) = 1 · 7 = 7

-2 · det[−2 13 2

]= -2 · (-4 - 3) = -2 · -7 = 14

-2 · det[−2 33 −1

]= -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 modularinverse of det A for Mod m and then calculate Mod m for the entries in theresulting matrix.

To calculate adj A:

C 11= + det

[3 1−1 2

]C 12= - det

[−2 13 2

]C 13= + det

[−2 33 −1

]C 21= - det

[2 −2−1 2

]C 22= + det

[1 −23 2

]C 23= - det

[1 23 −1

]C 31= + det

[2 −23 1

]C 32= - det

[1 −2−2 1

]C 33= + det

[1 2−2 3

]

⇒C 11= (6 - (-1)) = 7 C 12= -(-4 - 3) = 7 C 13= (2 - 9) = -7C 21= -(4 - 2) = -2 C 22= (2 - (-6)) = 8 C 23= -(-1 - 6) = 7C 31= (2 - (-6)) = 8 C 32= -(1 - 4) = 3 C 33= (3 - (-4)) = 7

You can take the results and put them into a new matrix C =

7 7 −7−2 8 78 3 −7

and now adj A = C T .

⇒ adj A =

7 −2 87 8 3−7 7 7

Now you can calculate the modular inverse of A:

B = 3

7 −2 87 8 3−7 7 7

=

21 −6 2421 24 9−21 21 21

B =

21 −6 2421 24 9−21 21 21

Mod 26 =

21 20 2421 24 9−5 21 21

Therefore

21 20 2421 24 9−5 21 21

is the modular inverse of

1 2 −2−2 3 13 −1 2

for

Mod 26.

21 20 2421 24 9−5 21 21

is the matrix you would use to decrypt a Hill cipher

encrypted using

1 2 −2−2 3 13 −1 2

.

17

To check that

21 20 2421 24 9−5 21 21

is the modular inverse of

1 2 −2−2 3 13 −1 2

: 1 2 −2−2 3 13 −1 2

· 21 20 24

21 24 9−5 21 21

=

21 + 42− 10 20 + 48− 42 24 + 18− 42−42 + 63 + 5 −40 + 72 + 21 −48 + 27 + 2163− 21 + 10 60− 24 + 42 72− 9 + 42

=

53 26 026 53 052 78 105

53 26 026 53 052 78 105

Mod 26 =

1 0 00 1 00 0 1

= I 3

5.2 Encryption

Here is how to encrypt the phrase HELLO WORLD using a Hill-3 Cipher.

The encryption matrix A =

1 2 −2−2 3 13 −1 2

and the decryption matrix B = 21 20 2421 24 9−5 21 21

.

18

1. Divide the phrase to encrypt into 3 × 1 vectors and assign them numbersusing Table II. H

EL

=

7411

L

OW

=

111422

O

RL

=

141711

D

XX

=

32323

You will notice that there are not enough letters to completely �ll out the lastvector. One common solution is to just repeat the �nal letter to �ll out the �nalvector. Another solution, the one used here, is to �ll out the �nal vector withuncommonly used letters, such as X.

2. Multiply each of the numerical vectors by the encryption matrix,

1 2 −2−2 3 13 −1 2

. 1 2 −2−2 3 13 −1 2

· 7

411

=

7 + 8− 22−14 + 12 + 1121− 4 + 22

=

−7939

1 2 −2−2 3 13 −1 2

· 11

1422

=

11 + 28− 44−22 + 42 + 2233− 14 + 44

=

−54263

1 2 −2−2 3 13 −1 2

· 14

1711

=

14 + 34 + 22−28 + 51 + 1142− 17 + 22

=

263447

1 2 −2−2 3 13 −1 2

· 3

2323

=

3 + 46− 46−6 + 69 + 239− 23 + 46

=

38632

19

3. Convert each of the resulting numerical vectors to Mod 26. −7939

Mod 26 =

19913

−5

4263

Mod 26 =

211611

26

3447

Mod 26 =

0821

3

8632

Mod 26 =

386

4. Use Table II to convert each of the values in the resulting vectors back toletters. 19

913

=

TJN

21

1611

=

VQL

0

821

=

AIV

3

86

=

DIG

5. The encrypted message now reads TJNVQLAIVDIG.

20

5.3 Decryption

The process to decrypt the phrase TJNVQLAIVDIG similar to encryption,except we need to use the modular inverse of the original encryption matrix,

which is

21 20 2421 24 9−5 21 21

.1. Divide the phrase to encrypt into 2 × 1 vectors and assign them numbersusing Table II. T

JN

=

19913

V

QL

=

211611

A

IV

=

0821

D

IG

=

386

21

2. Multiply each of the numerical vectors by the decryption matrix,

21 20 2421 24 9−5 21 21

. 21 20 24

21 24 9−5 21 21

· 19

913

=

399 + 180 + 312299 + 216 + 11795 + 189 + 273

=

891732557

21 20 24

21 24 9−5 21 21

· 21

1611

=

441 + 320 + 264441 + 384 + 99105 + 336 + 231

=

1025924672

21 20 24

21 24 9−5 21 21

· 0

821

=

0 + 160 + 5040 + 192 + 1890 + 168 + 441

=

664381609

21 20 24

21 24 9−5 21 21

· 3

86

=

63 + 160 + 14463 + 192 + 5415 + 168 + 126

=

367309309

3. Convert each of the resulting numerical vectors to Mod 26. 891

732557

Mod 26 =

7411

1025

924672

Mod 26 =

111422

664

381609

Mod 26 =

141711

367

309309

Mod 26 =

32323

22

4. Use Table II to convert each of the values in the resulting vectors back toletters. 7

411

=

HEL

11

1422

=

LOW

14

1711

=

ORL

3

2323

=

DXX

5. The decrypted message is now HELLOWORLDXX which can easily bedeciphered as HELLO WORLD.

6 Conclusion

The most di�cult part of creating Hill ciphers is determining the encryptionand decryption matrices. Once you have a viable encryption matrix and itscorresponding decryption matrix, it is relatively easy to encrypt and decryptmessages, if you have a working knowledge of matrix arithmetic and how mod-ulus works.

23

7 References

Lester S. Hill, Cryptography in an Algebraic Alphabet, The American Mathe-

matical Monthly Vol. 36, June-July 1929, pp. 306-312

Lester S. Hill, Concerning Certain Linear Transformation Apparatus of Cryp-tography, The American Mathematical Monthly Vol. 38, 1931, pp.135-154

Howard Anton, Chris Rorres, Elementary Linear Algebra with Applications,John Wiley and Sons, Inc., New York, NY, pp. 719-732

David C. Lay, Linear Algebra and Its Applications, Pearson, Addison Wesley,Boston, Massachusetts, pp 121, 187, 203-204

http://online.redwoods.edu/INSTRUCT/darnold/LAPROJ/Fall2001/Shinichi/Cryptography.pdf

http://www.apprendre-en-ligne.net/crypto/menu/index.html

http://en.wikipedia.org/wiki/Hill_Cipher

24