Playfair Cipher and Hill Cipher (With Step-by-Step Solved Examples)
Playfair treats a pair of letters as one unit and uses their positions in a square. Hill represents a block as numbers and mixes them using matrix multiplication. Its key matrix must have an inverse modulo 26 for decryption to work.
Both Playfair Cipher and Hill Cipher are famous multi-letter (Polygram) substitution ciphers that encrypt groups of letters together instead of one letter at a time. These two topics are among the highest-scoring numerical questions in DAVV BCA exams!
Part 1: Playfair Cipher
Invented by Charles Wheatstone in 1854 and promoted by Lord Playfair, the Playfair Cipher was the first practical digram (pair of 2 letters) substitution cipher. It was used by the British Army in World War I and World War II.
Step 1: Constructing the 5 × 5 Key Matrix
The Playfair Cipher uses a 5 × 5 grid (25 boxes) containing the 26 English letters:
- Choose a Keyword (e.g.,
MONARCHY). - Write the letters of the Keyword into the 5 × 5 grid from left to right, top to bottom, skipping any duplicate letters.
- Fill the remaining boxes of the grid with the rest of the alphabet (
AtoZ) in alphabetical order. - Why 25 boxes for 26 letters? The letters
IandJare combined into a single box (I/J).
5 × 5 Matrix for Keyword = "MONARCHY":
| Col 1 | Col 2 | Col 3 | Col 4 | Col 5 |
|---|---|---|---|---|
| M | O | N | A | R |
| C | H | Y | B | D |
| E | F | G | I/J | K |
| L | P | Q | S | T |
| U | V | W | X | Z |
Step 2: Preparing the Plain Text into Pairs (Digrams)
Before encrypting, break the Plain Text into pairs of 2 letters using two simple rules:
- Rule A (No Identical Twins in a Pair): If both letters in a pair are the same (e.g.,
"BALLOON"→BA,LL), insert a filler letterXbetween the repeating letters (BA,LX,LO,ON). - Rule B (Even Length): If one single letter is left alone at the end of the word, add
X(orZ) at the end to complete the pair.
Example: Prepare Plain Text "INSTRUMENTS"
- Pair up:
IN|ST|RU|ME|NT|S_ - Since
Sis alone at the end, addX(orZ):IN|ST|RU|ME|NT|SX
Step 3: The 3 Playfair Encryption Rules
Same-row and same-column pairs move along their respective lines in the square. A rectangle pair takes the other corner in each letter’s row.
- 1. Same Row Rule — Replace each letter with the letter immediately to its RIGHT (wrap around to left edge)
- 2. Same Column Rule — Replace each letter with the letter immediately BELOW it (wrap around to top edge)
📝 Solved Example: Encrypt "MOSQUE" with Key "MONARCHY"
- Break into pairs:
MO|SQ|UE - Pair 1:
M O- Look at the matrix:
MandOare in the Same Row (Row 1). - Shift each 1 step to the Right:
M→O,O→N. MObecomesON.
- Look at the matrix:
- Pair 2:
S QSandQare in the Same Row (Row 4).- Shift each 1 step to the Right:
S→T,Q→S. SQbecomesTS.
- Pair 3:
U EUandEare in the Same Column (Col 1).- Shift each 1 step Down:
U(bottom of Col 1 wraps to top) →M,E→L. UEbecomesML.
- Final Cipher Text for
"MOSQUE":O N T S M L
(Note: For Rectangle Rule, e.g., pair ST weren’t in the same row, say A R vs E A: for E [Row 3, Col 1] and A [Row 1, Col 4], E takes Col 4 in Row 3 → I, and A takes Col 1 in Row 1 → M, so EA → IM.)
View diagram · Three Playfair Cipher Encryption Rules
Part 2: Hill Cipher
Invented by mathematician Lester S. Hill in 1929, the Hill Cipher is a polygraphic substitution cipher based on Linear Algebra (Matrix Multiplication).
How Hill Cipher Works:
- Assign each letter its number:
A = 0, B = 1, C = 2, ..., Z = 25. - To encrypt m letters at a time, use an m × m square Key Matrix (K).
- Encryption Formula:
Formula: C = (K × P) mod 26
- Decryption Formula:
Formula: P = (K^-1 × C) mod 26 (where K^-1 is the modular inverse of key matrix K modulo 26).
📝 Step-by-Step Solved Example (2 × 2 Hill Cipher):
Question: Encrypt the Plain Text "HI" using the 2 × 2 Hill Cipher Key Matrix:
K = | 3 3 |
| 2 5 |
Step 1: Convert Plain Text "HI" into a 2 × 1 Column Vector (P):
H= 7I= 8
P = | 7 |
| 8 |
Step 2: Multiply Key Matrix K by Plain Text Vector P:
K × P = | (3 × 7) + (3 × 8) | = | 21 + 24 | = | 45 |
| (2 × 7) + (5 × 8) | | 14 + 40 | | 54 |
Step 3: Take mod 26 of each number:
C = | 45 mod 26 | = | 19 |
| 54 mod 26 | | 2 |
Step 4: Convert numbers back to letters:
-
19 →
T -
2 →
C -
Final Cipher Text for
"HI":T C
💡 DAVV Exam Preparation Tips
10-Mark Question: “Explain the Playfair Cipher rules with a 5 × 5 matrix using keyword ‘MONARCHY’. Also explain Hill Cipher with a 2 × 2 example.”
- Always draw the 5 × 5
MONARCHYgrid neatly in your answer sheet and highlight theI/Jcell.- State all 3 Playfair rules clearly: Same Row (Right), Same Column (Down), Rectangle (Opposite horizontal corners).
- For Hill Cipher, write C = (K · P) mod 26 and show the simple
"HI"→"TC"calculation.