05 Matrices and Hill Cipher

Download as pdf or txt
Download as pdf or txt
You are on page 1of 98

Matrix & Hill cipher

Dr Faheem Bukhatwa
1. Matrices
2. Matrix Determinant
3. Matrix Inverse
4. Modulus inverse of a matrix
5. Hill Cipher

2
Introduction
DEFINITION:

A matrix is defined as an ordered


rectangular array of numbers.
They can be used to represent
systems of linear equations, as
will be explained below
• Here are some special types of matrices:

Symmetric

4
Diagonal

5
Upper Triangular

6
Lower Triangular

7
Zero

8
Identity

9
And a fully expanded mxn matrix A,
would look like this:

or in a more compact form:


10
Matrix Addition and Subtraction

• DEFINITION: Two matrices A and B can


be added or subtracted if and only if
their dimensions are the same (i.e. both
matrices have the identical amount of
rows and columns. Take:

11
12
Addition
If A and B above are matrices of the same type then the
sum is found by adding the corresponding elements
aij+bij
Here is an example of adding A and B together

13
Subtraction
If A and B are matrices of the same type then
the subtraction is found by subtracting the
corresponding elements aij-bij
Here is an example of subtracting matrices

14
Matrix Multiplication

When can Matrix Multiplication be done?

When the number of columns of the first


matrix is the same as the number of rows
in the second matrix then:
matrix multiplication can be performed.

15
Examples
A = [aij] = a11 a12 a13 B = [bi] = b1
a21 a22 a23 b2
b3

A.B = a11 a12 a13 b1 a11b1+a12b2+a13b3


a21 a22 a23 b2 = a21b1+a22b2+a23b3
b3

16
Example 2X2 matrix multiplication

17
Example 3X3 matrix multiplication

18
nxn matrix case
• Where matrix A has dimensions mxn,
• Matrix B has dimensions nxp.
• Then the product of A and B is the matrix
C, which has dimensions mxp.
• The ijth element of matrix C is found by
multiplying the entries of the ith row of A
with the corresponding entries in the jth
column of B and summing the n terms.
The elements of C are:
19
nxn matrix case
(Note: That AxB is not the same as BxA)

20
2 3 5 1 3 6 + 12 + 10 + 9 37
4 6 0 7 4 = 12 + 24 + 0 + 63 = 99
2
9

21
• In matrix multiplication A.B  B.A
• i.e. multiplication is not commutative.
(2 x 3) x (3 x 2) = (2 x 2)
(3 x 2) x (2 x 3) = (3 x 3)

22
A= 5 2 4 I= 1 0 0
1 3 8 0 1 0
7 9 6 0 0 1

A.I= ?

23
• If A.B = 0 it is not possible to assume that A = 0 or B = 0.
• Example
A= 2 1 -3 B= 1 9
6 3 -9 4 -6
2 4

A.B = 2 1 -3 1 9 2+4-6 18-6-12


6 3 -9 4 -6 = 6+12-18 54-18-36
2 4

= 0 0
0 0
• Therefore, A.B = 0. But A  0 and B  0

24
Scalar Multiplication

• To multiply a matrix by a single number


(i.e. scalar), each individual element is
multiplied by that factor. E.g

3 2 5 12 8 20
4x =
6 1 7 24 4 28
• i.e. in general k[aij] = [kaij]
25
Scalar Multiplication
• Likewise in reverse, it is possible to take a
common factor out of every element. E.g.

10 25 45 2 5 9
= 5x
35 15 50 7 3 10

i.e. [5aij] = [5[aij]


26
Transpose of Matrices
DEFINITION:
The transpose of a matrix is found by
exchanging rows for columns i.e.
Matrix A = (aij)
and the transpose of A is:
AT = (aij)
Where i is the row number and j is the column number.

27
example
The transpose of a matrix would be:

28
square matrix
• In the case of a square matrix (m=n), the
transpose can be used to check if a matrix
is symmetric.
• For a symmetric matrix A = AT

29
The Determinant of a Matrix

DEFINITION:
• Determinants play an important role in
finding the inverse of a matrix and also in
solving systems of linear equations.
• Assume we have a square matrix (m=n).
• The determinant of a matrix A will be denoted
by det(A) or |A|.

30
Determinant of a 2x2 matrix

• Assuming A is an arbitrary 2x2 matrix


A, where the elements are given by:

• Then the determinant of a this matrix is


as follows:

31
• Calculating the determinant:
3 7 = (3)(2) – (5)(7) = -29
5 2

32
Calculating Determinant
of a matrix

1. Co-factoring.
2. Diagonal technique

33
Determinant of a 3x3 matrix
Co-factoring method
• The determinant of a 3x3 matrix is a
little more tricky.
• Use the Co-Factoring method as
follows:

(assume A is an arbitrary
3x3 matrix A, where the
elements are given as follows)

34
• Its determinant is established as
follows:
• Select any row or column i.e. row 1

35
Calculating the determinant example:

• Example

1 3 2 1 5 7
4 5 7 = 4 8
2 4 8
-3 4 7
2 8

+2 4 5
2 4

= 1((5x8) – (4x7)) - 3((4x8) – (2x7)) + 2((4x4) – (2x5))


= 1(40 – 28) –3(32 – 14) + 2(16 – 10)
= 1(12) – 3(18) +2(6)
= 12 – 54 + 12
= -30

36
Determinant of a nxn matrix

• For the general case, where A is an nxn matrix the


determinant is given by:

Where the coefficients are given by the relation

where is the determinant of the (n-1) x (n-1) matrix


that is obtained by deleting row i and column j.
This coefficient is also called the cofactor of aij.

37
Determinants
1 2 3
Find the determinant of: A =  4 5 6 
 7 8 9 
Its determinant can be obtained by:
1 2 3
4 5 1 2 1 2
A = 4 5 6 =3 −6 +9
7 8 7 8 4 5
7 8 9
= 3( −3) − 6 ( −6) + 9 ( −3) = 0
You can find the determinant by using other
rows or columns 38
Determinants 2 2 1
Find the determinant of: a = 1 3 2
2 2 2

39
Determinants 2 2 1
Find the determinant of: a = 1 3 2
2 2 2

determinant of a = 4

40
Diagonals technique
The computations for 3×3 determinants

What is the determinant for matrix A:

41
Extend the determinant's grid by
rewriting the first two columns
of numbers

Then multiply along the down-diagonals:

1 2 3 1 2
0 -4 1 0 -4
0 3 -1 0 3 3x0x3

1 x -4 x -1 2 x 1 x 0

42
...and along the up-diagonals

0 x -4 x 3 3x1x1 -1 x 0 x 2
1 2 3 1 2
0 -4 1 0 -4
0 3 -1 0 3

43
Add the down-diagonals and subtract the up-diagonals:

0 x -4 x 3 3x1x1 -1 x 0 x 2
1 2 3 1 2
0 -4 1 0 -4
0 3 -1 0 3 3x0x3
2x1x0
1 x -4 x -1

Determinant = 4 + 0 + 0 – 0 – 3 – 0 = 1

44
And simplify

Then det(A)= 1.

45
Find the determinant of the following matrix:

First convert from the matrix to its determinant, with


the extra columns:

46
Find the determinant of the following matrix:

5 -2
0 2
D = (5 x 2 ) – (0 x-2)

First convert from the matrix to its determinant, with


the extra columns:

47
Then multiply down and up the diagonals:

5 -2 1 5 -2
0 3 -1 0 3
2 0 7 2 0

48
Add the down-diagonals (Blue)
Subtract the up-diagonals (Red)
Simplify for the final answer:

5 -2 1 5 -2
0 3 -1 0 3
2 0 7 2 0

49
The Inverse of a Matrix

• DEFINITION:
Assuming we have a square matrix A, which is non-
singular ( i.e. det(A) does not equal zero ),
then there exists an nxn matrix A-1 which is called
the inverse of A, such that this property holds:
• AA-1= A-1A = I where I is the identity matrix.

A square matrix has an inverse A-1 iff the


determinant |A|<>0.
• A matrix possessing an inverse is called
nopnsingular, or invertible.

50
Inverse of a Matrix
Using the determinant

51
The inverse of a 2x2 matrix

• Take for example a arbitrary 2x2 Matrix


A whose determinant (ad-bc) is not
equal to zero

• where a,b,c,d are numbers, The inverse is:

52
Example of 2X2 Inverse

53
Find inverse using
minors & determinant
• Finding inverse of a matrix by:
• Step 1: calculating the Matrix of Minors,
• Step 2: then turn that into the Matrix of
Cofactors,
• Step 3: then the Adjugate, and
• Step 4: multiply that by 1/Determinant.

54
Finding the invers using the
determinant

Find the Inverse of A:

55
Matrix of minors:

56
Matrix of Cofactors

Matrix of minors Matrix of CoFactors

57
Matrix of Adjugate

58
59
2 1 det = 2 x-2 – (1x3)
3 -2 = -4 = 3 = -7
-2 -1
2 1 adjucate=
3 -2 -3 2

1 -2 -1
Invers matrix = -7
-3 2
60
-2 -1 2/7 1/7 2 1
1 -2 -1
- = =
-7 -3 2 3/7 -2/7 3 -2
-3 2
4/7 + 3/7 2/7 -2/7
= =
6/7 - 6/7 3/7 +4/7

1 0
=
0 1

61
Determinant

D = +2[0x1 – (-2x1)] - 0[2x1 – (-2x0) ] +3[2x1 - 0x0]


= 2×2 -0x2 +3x2
Determinant = +6 - 0 + 4 = 10
62
Adjugate Inverse

63
For a 3X3 Marix

64
Inverse by minor matrix a 33 matrix
1 2 3 
Minor matrix of A =  0 4 5 
1 0 6 

The cofactor for each element of matrix A:


4 5 0 5 0 4
A11 = = 24 A12 = − =5 A13 = = −4
0 6 1 6 1 0

2 3 1 3 1 2
A21 = = −12 A22 = =3 A23 = − =2
0 6 1 6 1 0

2 3 1 3 1 2
A31 = = −2 A32 = − = −5 A33 = =4
4 5 0 5 0 4
65
1.6 Inverse of a 33 matrix
Minor or Cofactor  1 2 3 

A = 0 4 5  
matrix of A:
is then given by: 1 0 6 

 24 5 −4 
 −12 3 2 
 
 −2 −5 4 
66
1.6 Inverse of a 33 matrix
1 2 3 
Inverse matrix of A = 0 4 5  is given by:
1 0 6 

 24 5 −4   24 −12 −2 
T

−1 1   1  
A = − 12 3 2 = 5 3 −5
A  22  
 −2 −5 4   −4 2 4 

 12 11 − 6 11 −1 11 
=  5 22 3 22 −5 22 
 − 2 11 1 11 2 11  67
• 3 0 2 3 0
• 2 0 -2 2 0
• 0 1 1 0 1
• +0+0+4 -0 – (-6) -0 = 10

68
Hill Ciphers

• Polygraphic substitution cipher


• Uses matrices to encrypt and decrypt
• Uses modular arithmetic (Mod 26)

69
Before we start…
• What is the result of this calculation?

60 / 5 (7-5) = ?

70
Before we start…
• What is the result of this calculation?

60 / 5 (7-5) = ?

60 / 5 (7-5) = 24
or
60 / 5 (7-5) = 6

71
Before we start…
• What is the result of this calculation?

60 / 5 (7-5) = ?

60 / 5 (7-5) = 24 ➔ 12 * (7-5) ➔ 12 * 2
or
60 / 5 (7-5) = 6 ➔ 60 / 5 *2 ➔ 60 / 10

72
Ambiguous statements
• He shut the man with a gun!!
• You saw a man with binoculars!!
• I listened to the man with headphones.

73
Modulus for
Positive and Negative numbers
1. 80 mod 26 = 3 remains 2 = 2 ‘c’
(if remainder is negative then add +26 and repeat)
2. -80 mod 26 = 3 remains -2
= +26 - 2 = 24 ‘y’

3. -52 mod 26 = 2 remains 0 =0 ‘a’


4. -51 mod 26 = 1 remains -25
= +26 - 25 =1 ‘b’
5. -53 mod 26 = 2 remains -1
= +26 - 1 = 25 ‘z’74
A B C D E F G HI 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

57 mod 26 = 5 F
-57 mod 26 = 5 V
How? Keep adding +25 until number <= 25
-57 + 26 = -31
-31 + 26 = -5
-5 + 26 = 21 V
75
A B C D E F G HI 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

57 mod 26 = 5 F
-57 mod 26 = 5 = -5 = 21 V
How? Forget about the – sign for a moment
57 mod 26 = 5 give it the - sign now
-(5) + 26 = 21
76
MOD for negative numbers
To find –b mod N,
Just keep algebraically adding N to –b
until the number is between 0 and N

Example 1: Example 2:

- 77 mod 26 = ? -66 mod 26 =?


Add 26 to -77 = -51, +26 -66 = -40
again 26 to -51 = -25, +26 -40 =-14
again 26 to -25 = 1. +26 -14 = 12
So, −77 mod 26 = 1 So -66 mod 26 = 12
77
Modulus inverses
• Inverse of 2 is ½ ➔ (2 · ½ = 1)
• Matrix Inverse: AA-1 = I
• Modular Inverse for Mod m:
(a · a-1) Mod m = 1
• For Modular Inverses, a and m must NOT
have any prime factors in common

78
Modular Inverses of Mod 26

Example – Find the Modular Inverse of 9 for Mod 26


(determinant must be an odd number 1 to 25 except 13)
N=9
N * I mode 26 = 1
(I = 2 3 4 5 6 . . . )
9 * I = 18, 27, 36, 45, 54, . . . etc

We want the inverse which is < 26

9 · 3 = 27 79
Hill cipher

• One matrix to encrypt, one to decrypt


• Must be n x n, invertible matrices
• Decryption matrix must be modular
inverse of encryption matrix in Mod 26

80
Modularly Inverse Matrices
1. Make sure all values in module 26 (0.. 25)
2. Calculate determinant of first matrix A, det A
3. Make sure that det A has a modular inverse
for Mod 26
4. Calculate the adjugate of A, adj A
5. Multiply adj A by modular inverse of det A
6. Calculate Mod 26 of the result to get B
7. Use A to encrypt, B to decrypt

81
• If the determinant is not of those values
then the matrix has no modular inverse.

• Because a number and its inverse must


NOT have any prime factors in common

82
26 modular inverse of a matrix

83
26 modular inverse of a matrix
• Find the 26 Modular inverse of this key
matrix A and proof that it is the correct
inverse:
a = 5 8
-9 3

84
26 modular inverse of a matrix

• All values in the matrix must be between


0 and 25
• Calculate the modular of the matrix:
5 8 5 8
• A = -9 3 = 17 3 mod 26

85
1) Find Determinant and its
Multiplicative Inverse

86
1) Find Determinant and its
Multiplicative Inverse
Conditions:
The matrix can have an inverse if:
1.If the determinant is odd
2.If determinant is > 0 and is < 26
3.If determinant <> 13

87
1) Find Determinant and its
Multiplicative Inverse
• Multiplicative inverse of the determinant 9
in modulo 26 is: 9
• because 9 * 3 mod 26 = 27
27 mod 26 = 1 remain 1.
Or from this table:

Determinant = 9 ➔ Multiplicative inverse=3.


88
2) Find the Adjugate Matrix

89
3) Calculate Inverse

90
Original matrix A
5 8
A = -9 3

5 8
A = 17 3 mod 26
(Use for encryption)
9 2
A-1 = 1 15 mod 26
(Use for decryption)

91
Proof? A x A-1 = Identity matrix

92
example
Q) This ciphered messaged: “IJTP” was produced after
ciphering a plain text message with Hill cipher with the
given key matrix A.
2 5
A= 5 2

i) Find the 26 modular inverse matrix A-1 and proof it is


the inverse.
ii) Use the invers matrix A-1 to decipher the message
“IJTP”. Show your work.
93
example
• Find the 26 modular inverse of A:
2 5
A= 5 2

Find the Multiplicative Inverse of the Determinant


2 x 2 – (5 x 5) = 4 - 25 = -21 mod 26 = 5
Determinant = 5

The matrix has an inverse because the determinant is one


of those: (an odd number between 1 to 25 except 13)
94
example

Multiplicative inverse of the determinant 5 in


modulo2
26 5is: 21
because 5 * 21 mod 26 = 1.
2) Find the Adjutant Matrix

2 5 2 -5

5 2 Adj = -5 2

95
3) Multiply the Multiplicative Inverse of the
Determinant by the Adjugate Matrix

2 -5 42 -105 16 26
21 * -5 2 = -105 42 = 25 16 mod 26

26 modular Inverse:
2 5 16 26
A = 5 2 A-1 = 25 16
96
Proof:

2 5 16 25 32+125 50+80 1 0
5 2 25 16 = 80+50 125+32 mod 26 = 0 1

• ii) Use the invers matrix A-1 to decipher the


message “IJTP”. Show your work.
Using values for alphabet:

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
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

97
Decrypt: IJTP I = 8, J= 9, T= 19, P = 15

16 25 I8 358 15 P
25 16 * J 9 = 344 mod 26 6 = G

16 25 19 679 3 D
25 16 * 15 = 715 mod 26 13 = N

Plaintext message: PGDN 98

You might also like