1
EP Revision 3/3/16
2
CODER (Encryption)
DECODER (Decryption)
Destroy enemy
Destroy enemy
17,4,23,12,8,………….
…. 34 22 34 12 . .
Destroy SadMan
I am confused
3
Data that can be read without any special measures is called plaintext. The method of disguising plaintext is called encryption. Encrypting plaintext results in ciphertext. The process of reverting ciphertext back to its original plaintext is called decryption. In conventional cryptography, (I.e. symmetric-key encryption), one key is used for both encryption and decryption. This project uses the Hill Cipher algorithm which was Developed by the mathematician Lester Hill in 1929.
Conventional cryptography
Destroy SADMAN
4,5,19,20,18,15,25,27,191,4,3,1,1
Destroy SADMAN
4
HILL CIPHER ALGORITHM
This encryption algorithm takes successive plaintext letters, pi, and substitutes for them m ciphertext letters, ci. The substitution is determined by m linear equations in which each character is assigned a numerical value (I.e. a = 0, b = 1, c = 2, .... , z = 25 26 possible values). For m = 3 , the system can be described as:
(k11 p1 + k12 p2 + k13 p3) = c1 (k21 p1 + k22 p2 + k23 p3) = c2 (k31 p1 + k32 p2 + k33 p3) = c3
This linear system can be expressed in matrix format:
=
3
2
1
3
2
1
333231
232221
131211
ccc
ppp
kkkkkkkkk
P1x3 K3x3 = C1x3 whereby K3x3 is called the key
5
Encryption/decryption project
WRITE A C++ PROGRAM THAT WILL ENCRYPT AND DECRYPT A MESSAGE ENTERED AT THE KEYBOARD WITH WHITE SPACES ALLOWED. THE ENCRYPTION ALGORITHM TO ENCODE THE SENTENCE IS AS FOLLOWS (TRANSMITTER/CODER): 1) EACH LETTER IS FIRST CONVERTED TO A NUMBER: i.e. A = 0, B = 1, C = 2, …………., Z = 25, SPACE = 26 2) ARRANGE THE SEQUENCE OF NUMBERS INTO 3 x 1 MATRICES (i.e. COLUMN MATRICES P1, P2, P3, …….) 3) MULTIPLY EACH 3 x 1 MATRIX ABOVE WITH A 3 x 3 MATRIX, K, CALLED THE KEY. THE KEY MATRIX MUST BE INVERTIBLE
,......,, 332211 CKPCKPCKP ===
6
4) THE ENCODED MATRICES ARE TRANSMITTED IN THE SEQUENCE (E1: ROW 1, ROW 2, ROW 3, E2: ROW 1, ROW 2, ROW 3, E3: ROW 1, ROW 2, ROW 3, ……) THE DECRYPTION ALGORITHM TO DECODE THE SENTENCE FOLLOWS (RECEIVER/DECODER): 1) ARRANGE THE SEQUENCE OF NUMBERS BACK INTO 3 x 1 MATRICES (i.e. COLUMN MATRICES P1, P2, P3, …….) 2) MULTIPLY EACH 3 x 1 MATRIX ABOVE WITH A 3 x 3 MATRIX, K-1, THE INVERSE OF THE KEY AS SHOWN: 3) THE RESULTING 3 x 1 DECODED MATRICES REPRESENT THE ORIGINAL 3 x 1 MATRICES W1, W2, W3 ……
,......,, 331
221
111 PCKPCKPCK === −−−
Encryption/decryption project
7
4) CREATE THE SEQUENCE: (E1: ROW 1, ROW 2, ROW 3, E2: ROW 1, ROW 2, ROW 3, E3: ROW 1, ROW 2, ROW 3, ……) 5) EACH NUMBER IN THE SEQUENCE IS CONVERTED BACK TO A CHARACTER USING THE CONVERSION TABLE: i.e. A = 0, B = 1, C = 2, …………., Z = 25, SPACE = 26
FOR THIS PROJECT, THE KEY MUST BE GENERATED RANDOMLY, BE AN INVERTIBLE MATRIX AND IS TRANSMITTED ALONG WITH THE ENCRYPTED MESSAGE. MESSAGE TRANSMISSION: THE ENCRYPTED MESSAGE AND THE KEY ARE PLACED INTO A FILE. MESSAGE RECEPTION: THE FILE IS OPENED SO TO DECODE ITS CONTENT (KEY + MESSAGE).
Encryption/decryption project
8
Letters are converted to numbers A = 0, b = 1, c = 2, …………., z = 25, space = 26 Create 3 x 1 column matrices p1, p2, p3, … Generate a random 3 x 3 matrix k (k-1 must exist) Generate encoded matrices kpi = ci Transmit encoded matrices and key
Algorithm
Encryption:
Decryption:
Receive encoded matrices ci Receive key k Calculate inverse k-1 Re-create the original words k-1 ci = pi Each number is converted back to letters
9
Matrices are denoted by upper case letters (i.e. A) Each Entry of the matrix is called an Element Each element is given a position. aij. i = row#, j = column# The size of a matrix is given by n x m (n = Number of rows, m = Number of columns) The number of elements in a matrix is (n) . (m) Rectangular matrix: n != m Square matrix: n = m A column vector is an n x 1 matrix A row vector is an 1 x m matrix Two matrices are equal if all corresponding elements are equal An identity matrix ( I ) is a square matrix for which the diagonal elements are equal to 1 and all other elements are equal to zero Two square matrices A, B are inverse of each other if AB = BA = I The inverse of a matrix An is a matrix Bn such that An Bn = Bn An = In. We define A as A-1. As such A A-1 = A-1 A = I
Matrix Algebra Basics
10
A of inverse thebe tosaid isA then I A A AA if
R|A|A whereby oft determinan theas defined is |A| det(A)
tionMultiplicaScalar
size) same theof b and(A n SubtractioMatrix
size) same theof b and(A Addition Matrix
and size same theof are BA, if
1-1- ==
∈=
→∀=
→∀−=−
→∀+=+
∀==
ijijmxn
ijijijmxnmxn
ijmxnmxn
ijijijmxnmxn
KaKA
baBA
baBA
baBA
ijij
Matrix Algebra Basics
11
( ) ( ) ( )
[ ] Adjoint)(
Inverse)(Cofactor)1(
tDeterminan)det(
Transpose
tionMultiplicaMatrix
1
1 1 1
→=
→=
→+−=
→=
→== →=
→
−
= = =∑∑∑
TCAAdj
AAAdjA
MijjiCij
AA
abBTAaA
ba
nxmjiijmxnij
m
i
r
j
n
kkjik
(Amn Bnr = Cmr) I = IT
I is symmetric
Matrix Algebra Basics
12
=
333231
232221
131211
333231
232221
131211
333231
232221
131211
ccccccccc
bbbbbbbbb
xaaaaaaaaa
][][][ rxmrxnnxm =
3 x 3 3 x 3
m = 3 n = 3
n = 3 r = 3
3 x 3
m = 3 r = 3
Matrix Multiplication
13
333231
232221
131211
333231
232221
131211
bbbbbbbbb
xaaaaaaaaa
a11b11
C11 Matrix Multiplication
14
333231
232221
131211
333231
232221
131211
bbbbbbbbb
xaaaaaaaaa
a11b11+ a12b21
C11 Matrix Multiplication
15
333231
232221
131211
333231
232221
131211
bbbbbbbbb
xaaaaaaaaa
a11b11+ a12b21 + a13b31
C11 Matrix Multiplication
16
333231
232221
131211
333231
232221
131211
bbbbbbbbb
xaaaaaaaaa
a21b12
C22 Matrix Multiplication
17
333231
232221
131211
333231
232221
131211
bbbbbbbbb
xaaaaaaaaa
a21b12+ a22b22
C22 Matrix Multiplication
18
333231
232221
131211
333231
232221
131211
bbbbbbbbb
xaaaaaaaaa
a21b12+ a22b22 + a23b32
C22 Matrix Multiplication
19
333231
232221
131211
333231
232221
131211
bbbbbbbbb
xaaaaaaaaa
a31b13
C33 Matrix Multiplication
20
333231
232221
131211
333231
232221
131211
bbbbbbbbb
xaaaaaaaaa
a31b13+ a32b23
C33 Matrix Multiplication
21
333231
232221
131211
333231
232221
131211
bbbbbbbbb
xaaaaaaaaa
a31b13+ a32b23 + a33b33 ∑∑∑= = =
m
i
r
j
n
kkjikba
1 1 1
C33 Matrix Multiplication
22
Matrix Multiplication #include <iostream> using namespace std; void main() { int i, j, k, count = 0, total = 0; int A[ ] [ 3 ] = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } }; int B[ 3 ] [ 3 ] = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } }; for (i = 0; i < 3; i++) { for (j = 0; j < 3; j++){ for (k = 0; k < 3; k++) total += A[i][k] * B[k][j]; cout << total << ' '; ++count; if( count % 3 == 0) cout << endl; total = 0; } } return; }
∑∑∑= = =
3
1
3
1
3
1i j kkjikba
23
Determinant of a 2x2
Example:
11211121121)8()17()3()5(|| =
−
−⇒−=−=A
|A| is called the determinant of A and |A|-1 is called the reciprocal of |A| whereby |A| |A|-1 = 1:
=
31785
A
||1|| 1
AA =−
=
2221
1211
aaaa
A 21211211|| aaaaA −=if then
24
Determinant of An with n > 2 Laplacian Expansion
∑=
=
++=
=
3
111
131312121111
333231
232221
131211
jJj CAA
CACACAA
AAAAAAAAA
A
Expand along the first row
3x3 SQUARE MATRIX
*
*
MijjiCij +−= )1(
Cofactors Minor
25 3231
1211
23
333231
232221
131211
AA
AAM
AAAAAAAAA
A
=
=
i= 2 j = 3
3231
121123
AAAA
M =
Finding the Minor
26
=−=
=
+23
3223
333231
232221
131211
)1( MC
AAAAAAAAA
A
3231
1211
AAAA
−
Finding the Cofactor
27
EXAMPLE
−−=
434110433
A
Find |A|:
28
Determinant of A3
=
333231
232221
131211
aaaaaaaaa
A
Find |A|:
Procedure:
=
32
22
12
31
21
11
333231
232221
131211
aaa
aaa
aaaaaaaaa
A
|A| = a11a22a33 + a12a23a31 + a13a21a32 – a31a22a13 – a32a23a11 – a33a21a12
Another Approach
29
EXAMPLE Find |A|:
Procedure:
−−=
434110433
A
−−−−=
313
403
434110433
A
|A| = -12 -12 + 0 -16 +9 – 0 = -31
30
[ ]
COFACTORMijjiCij
ADJOINTTCAAdj
INVERSEA
AAdjA
→+−=
→=
→=−
)1(
)(
)(1
MINOR
0≠A
Inverse of An
Matrix of co-factors
Adjoint
31
EXAMPLE
−−=
434110433
A
Find A-1:
Adjoint
Matrix of co-factors
Determinant Inverse
32
Inverse of A2
|A|-1 Example:
Note the decimal numbers (Accuracy ISSUE)
|A| = (5)(3) – (17)(8) = -121
−
−=
−
−
−
=−
041.014.0066.0025.0
12151783
1A
A A-1 = A-1 A = I If A and A-1 are inverse:
|A| is called the determinant of A and |A|-1 is called the reciprocal of |A|
=
31785
A
33
Accuracy Issue Example:
−
−=−
125.0042.0062.0188.01K
=
=
924
9236
P
K
=
⇒=
129171
924
9236
xCPK
=
−
−⇒=−
9.815.24
129171
125.0042.0062.0188.01 xPCK
WTRANSMITTED
WRECEIVED
34
Accuracy Issue
Example:
=⇒
=
9.815.24
924
RECEIVEDDTRANSMITTE WW
Accuracy Issue: Because the inverse of a matrix will result with values of decimal format, the decryption will result with some errors in the decoding (i.e. a b) due to the decimal portion. Possible Solutions: Select only determinants with |A| = 1. The inverse is the adjoint divided by |A| (i.e. divide by 1) Round decimal numbers (i.e. if decimal portion ≥ 0.5, round up otherwise round down) Add 0.5 to each element – 2.1 + 0.5 = 2.6 Round to 2 (integer division) 2.6 + 0.5 = 3.1 Round to 3 (integer division)
⇒
924
9.815.24
35
EXAMPLE 1
36
FULL EXAMPLE
Encrypt/Decrypt Message: DESTROY SADMAN
Use matrix K below for the key:
−−=
434110433
KK is invertible:
−−−
−−=−
0968.00968.01290.0968.09032.01290.0
2258.07742.00323.01K
37
FULL EXAMPLE
Encrypt/Decrypt Message: DESTROY SADMAN
1. Assign each letter of the alphabet a number: A = 1, B = 2, C = 3, ……….X = 24, Y = 25, Z = 26, White Space = 27
DESTROY SADMAN 4,5,19,20,18,15,25,27,19,1,4,13,1,12
=
192725
3W
=
1954
1W
=
2712
15W
2. Create 3 x 1 column matrices
=
1341
4W
=
151820
2W
38
FULL EXAMPLE
Encrypt/Decrypt Message: DESTROY SADMAN
=
+++++−−
=
−−=
1072449
)761516()1950(
)761512(
1954
434110433
1KW
3. Encode each 3 x 1 column matrix using the key
Do the same for KWi = Ei Where i = 2,3,4,5
4. Transmit 49, 24, 107, ….. E1
39
FULL EXAMPLE
Encrypt/Decrypt Message: DESTROY SADMAN
5. Group the incoming numbers into 3 x 1 column vectors
1072449
……….
6. Decode each 3 x 1 column matrix by multiplying by the inverse key
=
−−
=−
1954
1072449
...1290.0
....1290.0
....0323.01
1EK DES
40
EXAMPLE 2 Acknowledgments: 18, 15, 8, 9, 20, 19,15,4,9,1
41
Step One (Encryption): Enter a message:
input =
inputnum =
“example”
example
{7
ASCII a-97
b-98
c-99
d-100
e-101
|-94
101-94=7
{7, 26, 3, 15, 18, 14, 7}
42
Step Two (Encryption): inputnum = {7, 26, 3, 15, 18, 14, 7} 0, 0
7
26
3
15
18
14
7
0
0
Generated Key 11
4
-8
2
1
-1
-8
-3
6
43
Step Three (Encryption):
11
4
-8
2
1
-1
-8
-3
6
7
26
3
105
45
-64
encrypted = {105, 45, -64 {105, 45, -64, 89, 36, -54, 77, 28, -56}
e x a m p l e filler
44
Step One (Decryption): encrypted = {105, 45, -64, 89, 36, -54, 77, 28, -56}
3
0
4
-4
2
-5
2
1
3
105
45
-64
7
26
3
Inverse of key matrix
decrypted = {7, 26, 3 , 15, 18, 14, 7, 0, 0}
45
Step Two (Decryption): decrypted = {7, 26, 3, 15, 18, 14, 7, 0, 0}
0? ASCII a-97
b-98
c-99
d-100
e-101
NO?
7+94=101
e Printed on screen: x a m p l e
26+94=120 0?
YES?
Stop printing