Course - Network Security (SSZG513)
Topic - Hill Cipher Lab Exercise & Detailed Example
Author and Instructor - Vineet Garg
Lab Exercise: Write a C/C++/Java program to implement Hill Substitution Cipher.
Instructions and Steps:
(1) Prompt the user to provide a plaintext - a user string of English alphabets (lower or upper
case).
(2) Prompt the user to provide the dimension of square matrix (m x m) for storing Hill Cipher
Key (K).
(3) Store the plaintext in numeral order (a/A to z/Z as 0 to 25) in the p x m matrix. Where m
is dimension of the key matrix supplied by the user.
(4) Prompt (or generate) a Z26 key and store it in the m x m matrix. This key is to be supplied
(or generated) in such a ways that it has a multiplicative inverse in Z 26.
(5) Calculate ciphertext.
(6) Calculate multiplicative inverse matrix of K.
(7) Restore plaintext from ciphertext using multiplicative inverse and compare it with the
user input of plaintext.
Prerequisite:
Review Hill Cipher and Matrix operations from contact session slides thoroughly.
Detailed Example:
(audience is encouraged to solve it and compare the results side by side):
Matrix Determinant in modulo-26 form for the Hill Cipher Key (K):
K=
det (A) = (-1)1+1.17. + (-1)1+2.17. + (-1)1+3.5.
= 17(18x19 - 2x21) - 17(21x19 - 21x 2) + 5 (21x2 - 18x2)
= 5100 - 6069 + 30
= - 939
In modulo 26 arithmetic -939 = -939 mod 26 = -3 = 23
Multiplicative Inverse of Matrix Determinant (23) in modulo-26 form:
q n1 n2 r t1 t2 t
1 26 23 3 0 1 -1
7 23 3 2 1 -1 8
1 3 2 1 -1 8 -9
2 2 1 0 8 -9 26
1 0 -9 26
t = t1-q.t2
BITS Pilani Work Integrated Learning Programme (WILP)
Page 1 of 4
Course - Network Security (SSZG513)
Topic - Hill Cipher Lab Exercise & Detailed Example
Author and Instructor - Vineet Garg
Since GCD of 26 and 23 is 1, so multiplicative inverse of 23 exists.
Multiplicative inverse = (last t1) mod 26 = -9 mod 26 = 17
Coding of the message "paymoremoney" in matrix form:
P=
Arranging, a.....z in 0...25 order.
Cipher Text using key K:
C = P.K mod 26
= x mod 26
= mod 26
= mod 26
So the encrypted message (ciphertext) is: RRLMWBKASPDH. Arranging, A.....Z in 0...25
order.
Multiplicative Inverse of the Key (K):
[A-1]ij = (det (A))-1.(-1)i+j.Dji
Where,
(det (A))-1 = Multiplicative inverse of det(A) mod 26 in Z26.
Dji = Determinant of the matrix deleting jth row and ith column.
BITS Pilani Work Integrated Learning Programme (WILP)
Page 2 of 4
Course - Network Security (SSZG513)
Topic - Hill Cipher Lab Exercise & Detailed Example
Author and Instructor - Vineet Garg
We have already calculated (det (A))-1 as 17.
[A-1]ij = 17.(-1)i+j
= 17.(-1)i+j
= 17
= (in mod 26 arithmatic)
Plaintext restored from the key inverse:
P = C.K-1 mod 26
= x mod 26
= mod 26
Which is same as the plaintext code and yields the decrypted text as "paymoremoney".
BITS Pilani Work Integrated Learning Programme (WILP)
Page 3 of 4
Course - Network Security (SSZG513)
Topic - Hill Cipher Lab Exercise & Detailed Example
Author and Instructor - Vineet Garg
Practice Exercise
Using Hill Cipher, encrypt the plaintext codeisready using the key (K) as given below and
verify your answer decrypting it after finding out the multiplicative inverse of K. You can use
dummy character z as padding if required.
K=
BITS Pilani Work Integrated Learning Programme (WILP)
Page 4 of 4