Deciphering Hill Ciphers

advertisement
Deciphering Hill Ciphers
David Arnold
September 7, 1996
Abstract
The purpose of this activity is to decipher ciphertext that has been
encoded with Hill ciphers. A review of the inverse of a matrix is included
and particular attention is paid to the inverse of a matrix modulo 26.
Prerequisites. Matrix multiplication. The inverse of a matrix. Also,
the reader should …rst complete the activity “Modular Arithmetic and
Hill Ciphers in Matlab” before attempting this activity.
1 The Multiplicative Inverse
Let’s begin with a familiar property of the real numbers.
The Multiplicative Identity Property.
a£1=a
For any real number a,
If you multiply any real number by 1, then the answer is identical to the
number you start with. Consequently, the number one is called the multiplicative
identity.
Once you establish which number is acting as the multiplicative identity,
the next question is: “What should I multiply the number a by to get the
multiplicative identity?” Of course, there is a familiar answer to this question
if you are working with real numbers.
The Multiplicative Inverse Property. If a is any nonzero real number ,
1
a£ =1
a
The number 1=a is called the multiplicative inverse of the number a. Consequently, two numbers are inverses of one another if their product is the multiplicative identity.
You might recall that a¡ = 1=a. The notation a¡ is often used to denote
the multiplicative inverse. For example, 3¡ = 1=3.
Zero
one.
has no multiplicative inverse. The product of zero and any number is zero, never
1
1.1 An Example from Modular Arithmetic
The congruence relation a ´ b(mod 8) partitions the integers into precisely
eight equivalence or residue classes : Z& = f0; 1; 2; 3; 4; 5; 6; 7g. Multiplication
of these equivalence or residue classes is performed according to the following
multiplication table.
£
0
1
2
3
4
5
6
7
0
0
0
0
0
0
0
0
0
1
0
1
2
3
4
5
6
7
2
0
2
4
6
0
2
4
6
3
0
3
6
1
4
7
2
5
4
0
4
0
4
0
4
0
4
5
0
5
2
7
4
1
6
3
6
0
6
4
2
0
6
4
2
7
0
7
6
5
4
3
2
1
Close inspection of the table reveals that a £ 1 = a for all residue classes a.
Consequently, 1 is the multiplicative identity.
²
²
²
²
²
²
²
²
0 has no multiplicative inverse.
Because 1 £ 1 = 1, the multiplicative inverse of 1 is 1.
2 has no multiplicative inverse.
Because 3 £ 3 = 1, the multiplicative inverse of 3 is 3.
4 has no multiplicative inverse.
Because 5 £ 5 = 1, the multiplicative inverse of 5 is 5.
6 has no multiplicative inverse.
Because 7 £ 7 = 1, the multiplicative inverse of 7 is 7.
Note that only those residue classes that are relatively prime! to 8 have
multiplicative inverses.
1.2 Matrix Examples
Consider the set of all two by two matrices whose entries are real numbers.
Matrix multiplication reveals the following fact.
·
1.
a b
c d
¸·
1 0
0 1
¸
·
=
a b
c d
¸
See the activity “Modular Arithmetic and Hill Ciphers in Matlab.”
integers = and > are relatively prime if and only if their greatest common divisior is
! Two
2
·
¸
Therefore, the matrix I = 10 01 is the identity matrix for two by two matrices with real entries. That is, AI = A for all A.
A simple formula is used to compute the inverse of a two by two matrix.
·
¸
·
¸ ¡
d ¡b
a b
¡
= (ad ¡ bc)
c
d
If (ad ¡ bc)¡ does not exist, then matrix
·
¡c
a b
c d
¸
a
has no inverse.
1.2.1 ·A Matrix
with an Inverse
¸
If A =
2 3
1 2
, its inverse is computed in the following manner.
·
¸
2 ¡3
¡
= ((2)(2) ¡ (3)(1)) ¡1 2
·
¸
2 ¡3
= (1)
¡1 2
·
¸
2 ¡3
=
¡1 2
·
¸
2 ¡3
A¡ =
¡1 2 . Note that
·
¸·
¸ ·
¸
2 3
2 ¡3
1 0
¡
AA =
1 2
¡1 2 = 0 1 = I
·
Consequently,
2 3
1 2
¸¡
which clearly shows that A¡ is the multiplicative inverse of matrix A.
1.2.2 ·A Matrix
with No Inverse
¸
2 4
If A = 1 2 , then (ad ¡ bc)¡ = ((2)(2) ¡ (4)(1))¡ = 0¡ . But the number
zero has no inverse. Consequently, the matrix A has no inverse.
2 Encrypting the Plaintext
Assign each letter of the alphabet a residue class modulo 26 as shown in Table
1.
Table 1 Replacing letters with equivalence classes (modulo 26)
A B C
0 1 2
N O P
13 14 15
D
3
Q
16
E
4
R
17
F
5
S
18
G
6
T
19
3
H
7
U
20
I J K
8 9 10
V W
21 22
L
11
X
23
M
12
Y Z
24 25
Break the plaintext LINEARALGEBRA into pairs of letters.
LI
NE
AR
AL
GE
BR
AZ
The dummy letter Z is used to …ll out the last pair" . Replace each letter
of each pair of plaintext with the corresponding number from Table 2. If you
place each numerical pair in a 2 £ 1 vector, then the following set of vectors
represents the paired plaintext message.
½·
P=
¸ ·
¸ ·
¸ ·
¸ · ¸ ·
¸ ·
¸¾
13
0
0
6
1
0
;
;
;
;
;
;
4
17
11
4
17
25
11
8
Choose a 2 £ 2 matrix to do the encoding.
·
E=
12 15
7 6
¸
You can encode the plaintext message by multiplying (mod 26) each vector
in P by the matrix E or you can form the matrix
·
P=
11 13 0 0 6 1 0
8 4 17 11 4 17 25
¸
and form the product C = EP (mod 26). This is easy to do in Matlab.
>> P=[11 13 0 0 6 1 0;8 4 17 11 4 17 25]
P =
11
8
13
4
0
17
0
11
6
4
1
17
0
25
9
14
2
14
7
5
11
20
>> E=[12 15;7 6]
E =
12
7
15
6
>> C=mod(E*P,26)
C =
18
21
" You
8
11
21
24
could easily use any letter you wish in place of Z.
4
Substituting letters from Table 1, the matrix C is equivalent to the ciphertext
SV
IL
VY
JO
CO
HF
LU
Consequently, the plaintext LINEARALGEBRA becomes SVILVYJOCOHFL.
3 Deciphering the Ciphertext
The ciphertext was obtained by multiplying the plaintext by the encoding matrix. That is,
C = EP
To decipher the ciphertext you will have to reverse this process. This is easily
accomplished by multiplying both sides of the equation by the inverse of matrix
E:
C = EP
= E ¡ (EP )
¢
¡
= EE ¡ P
E ¡ C
E ¡ C
E ¡ C
E ¡ C
= IP
= P
(1)
Therefore, P = E ¡ C .
3.1 Finding the Inverse of the Encoding Matrix
The inverse of the encoding matrix is computed in the following manner:
·
¸
·
¸¡
6 ¡15
12 15
= ((12)(6) ¡ (15)(7))¡
7
6
= (¡33)¡
·
6
¡7
¡15
12
¸
¡7
12
(2)
The residue classes modulo 26 are Z $ = f0; 1; 2; 3; : : : ; 25g. Also, keep in mind
that all arithmetic is performed modulo 26.
²
Because
33 + 19 = 52
= 0
(52 divided by 26 leaves a remainder of 0), 19 is the additive inverse of 33.
Therefore# , ¡33 = 19.
# The notation ¡ ! asks: “What number, when added to !, yields a sum of zero.”
5
²
Because
15 + 11 = 26
= 0
²
(26 divided by 26 leaves a remainder of 0), 11 is the additive inverse of 15.
Therefore$ , ¡15 = 11.
Because
7 + 19 = 26
= 0
(26 divided by 26 leaves a remainder of 0), 19 is the additive inverse of 7.
Therefore% , ¡7 = 19.
Replace each negative number in equation (2) with its equivalent residue
class.
·
¸¡
·
¸
12 15
6 ¡15
¡
= (¡33)
7
6
= 19¡
·
¡7
6 11
19 12
¸
12
(3)
The notation 19¡ asks: “What number, when multiplied by 19, leaves a
product of 1?” There are some very nice techniques in number theory that will
…nd the multiplicative inverse of 19 (modulo 26). However, a little experiment
(trial and error) will show that
19 £ 11 = 209
= 1
(209 divided by 26 leaves a remainder of 1)& . The multiplicative inverse of 19 is
11 (modulo 26). Therefore, 19¡ = 11. Make this substitution in equation (3).
·
¸¡
·
¸
12 15
6 ¡15
¡
= 19
7
6
·
¡7
¸
12
6 11
= 11
19 12
·
¸
66 121
=
209 132
·
¸
14 17
=
1 2
$ The
% The
notation
notation
¡# asks: “What number, when added to #, yields a sum of zero.”
¡% asks: “What number, when added to %, yields a sum of zero.”
(4)
(5)
& If =@ ¡ >? ¡ does not exist then the encoding matrix will not have an inverse. Keep this
in mind when choosing an encoding matrix.
6
Each entry in matrix (4) is the remainder when the corresponding entry in
matrix (5) is divided by 26.
Of course, you should check that matrix (5) is the inverse of the encoding
matrix.
·
¸·
¸¡
12 15
12 15
EE ¡ =
·
=
·
7
6
12 15
7 6
¸·
183 234
=
104 131
·
¸
1 0
=
0 1
¸
7
6
14 17
1 2
¸
You should check that 183 divided by 26 leaves a remainder of 1, 234 divided
by 26 leaves a remainder of 0, 104 divided by 26 leaves a remainder of 0, and
131 divided by 26 leaves a remainder of 1.
3.2 Decoding the Ciphertext
We need only perform the multiplication in equation (1) to decode the ciphertext.
P = E ¡ C
This is easy to do in Matlab' .
>> C
C =
18
21
8
11
21
24
9
14
2
14
7
5
11
20
>> F=[14 17;1 2] %Let F represent the inverse of the encoding matrix.
F =
14
1
17
2
>> mod(F*C,26)
ans =
' Anything following a % symbol in Matlab is a comment. Comments are ignored by the
Matlab interpreter.
7
11
8
13
4
0
17
0
11
6
4
1
17
0
25
Note that we have recaptured the matrix P , the matrix containing the numbers representing the plaintext LINEARALGEBRA.
4 Further Project Explorations
If you are thinking that you might like to pursue coding and encoding a bit
further, you might want to explore some potentially useful Matlab commands.
4.1 ASCII Coding
Each character on your keyboard has an associated ASCII code. If you enter
the character in Matlab as a string, then you can use the command real to …nd
the ASCII code for that particular character.
>> real(’A’)
ans =
65
You can get codes for a number of characters in the following manner.
>> s=’ABCDEFGHIJKLMNOPQRSTUVWXYZ’
s =
ABCDEFGHIJKLMNOPQRSTUVWXYZ
>> nstr=real(s)
nstr =
Columns 1 through 12
65
66
67
68
69
70
71
72
73
74
75
76
81
82
83
84
85
86
87
88
Columns 13 through 24
77
78
79
80
Columns 25 through 26
89
90
8
Note that the ASCII codes for the uppercase letters A-Z are the consecutive
integers 65-90 . If you subtract 65 from each element in nstr you can change
the ASCII codes to residue classes modulo 26.
>> res=nstr-65
res =
Columns 1 through 12
0
1
2
3
4
5
6
7
8
9
10
11
16
17
18
19
20
21
22
23
Columns 13 through 24
12
13
14
15
Columns 25 through 26
24
25
You can easily change ASCII code back to a string.
>> string(nstr)
ans =
ABCDEFGHIJKLMNOPQRSTUVWXYZ
5 Homework
1. The ciphertext MWUULNGXAPAKTTNCHGCBHGILPPQMTFWO was
encoded with Table 1 and the encoding matrix
·
E=
12 15
7 6
¸
Decipher the message.
2. The ciphertext EDQHQ VBNGEDZLJHQNQWROQSQKEDQHTHEA
(note the single space) was encoded using Table 2 and the encoding matrix
·
E=
The
9 11
4 5
¸
ASCII codes for the lowercase letters are di¤erent.
Type real(’abcdefghijklmnopqrstuvwxyz’) to see these codes.
9
Table 2 Replacing letters with equivalence classes (modulo 27)
A B C D E F G H I J K L M N
0 1 2 3 4 5 6 7 8 9 10 11 12 13
O P Q R S T U V W X Y Z space
14 15 16 17 18 19 20 21 22 23 24 25 26
Decipher the message.
3. Enter the plaintext ILOVEMATHEMATICS as a string at the Matlab
prompt.
(a) Use Matlab’s real command to express the plaintext in terms of its
ASCII code.
(b) Change the ASCII code to residue classes modulo 26 by subtracting
65 from the plaintext vector.
(c) Use Matlab’s reshape to prepare the plaintext for encoding with the
matrix
·
¸
E=
8 9
7 8
(d) Encode the plaintext to ciphertext with encoding matrix E .
(e) Reshape the ciphertext so that it is a vector.
(f) Add 65 to the ciphertext vector to replace residue classes with ASCII
code.
(g) Use the string command to change the ASCII code back to a string.
What is the resulting ciphertext?
10
Download