Professional Documents
Culture Documents
05 Matrices and Hill Cipher
05 Matrices and Hill Cipher
05 Matrices and 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:
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:
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
15
Examples
A = [aij] = a11 a12 a13 B = [bi] = b1
a21 a22 a23 b2
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
= 0 0
0 0
• Therefore, A.B = 0. But A 0 and B 0
24
Scalar Multiplication
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
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
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
36
Determinant of a nxn matrix
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
41
Extend the determinant's grid by
rewriting the first two columns
of numbers
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:
46
Find the determinant of the following matrix:
5 -2
0 2
D = (5 x 2 ) – (0 x-2)
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.
50
Inverse of a Matrix
Using the determinant
51
The inverse of a 2x2 matrix
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
55
Matrix of minors:
56
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
63
For a 3X3 Marix
64
Inverse by minor matrix a 33 matrix
1 2 3
Minor matrix of A = 0 4 5
1 0 6
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 33 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 33 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
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’
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:
78
Modular Inverses of Mod 26
9 · 3 = 27 79
Hill cipher
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.
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
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:
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
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
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