←Back to Subject
Notes
Notes
Study material and explanations
Diagrams
Diagrams
Visualize concepts easily
Mnemonics
Mnemonics
Tricks to remember faster
One Night Prep
One Night Prep
Short and focused revision
Revision
Revision
Quick summary and key points
Important Questions
Important Questions
Practice most relevant questions
Notes

Playfair Cipher (5x5 Matrix) & Hill Cipher with Solved Examples

Study material and explanations for Unit 1

Playfair Cipher and Hill Cipher (With Step-by-Step Solved Examples)

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:

  1. Choose a Keyword (e.g., MONARCHY).
  2. Write the letters of the Keyword into the 5 × 5 grid from left to right, top to bottom, skipping any duplicate letters.
  3. Fill the remaining boxes of the grid with the rest of the alphabet (A to Z) in alphabetical order.
  4. Why 25 boxes for 26 letters? The letters I and J are 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 letter X between 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 (or Z) at the end to complete the pair.

Example: Prepare Plain Text "INSTRUMENTS"

  • Pair up: IN | ST | RU | ME | NT | S_
  • Since S is alone at the end, add X (or Z): IN | ST | RU | ME | NT | SX

Step 3: The 3 Playfair Encryption Rules

Three Playfair Cipher Encryption Rules
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)
3. Rectangle Rule
Replace each letter with the letter in its OWN ROW but at the OTHER letter's column corner

📝 Solved Example: Encrypt "MOSQUE" with Key "MONARCHY"

  1. Break into pairs: MO | SQ | UE
  2. Pair 1: M O
    • Look at the matrix: M and O are in the Same Row (Row 1).
    • Shift each 1 step to the Right: M → O, O → N.
    • MO becomes ON.
  3. Pair 2: S Q
    • S and Q are in the Same Row (Row 4).
    • Shift each 1 step to the Right: S → T, Q → S.
    • SQ becomes TS.
  4. Pair 3: U E
    • U and E are in the Same Column (Col 1).
    • Shift each 1 step Down: U (bottom of Col 1 wraps to top) → M, E → L.
    • UE becomes ML.
  • 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.)


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:

  1. Assign each letter its number: A = 0, B = 1, C = 2, ..., Z = 25.
  2. To encrypt m letters at a time, use an m × m square Key Matrix (K).
  3. Encryption Formula:

    Formula: C = (K × P) mod 26

  4. 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 = 7
  • I = 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 MONARCHY grid neatly in your answer sheet and highlight the I/J cell.
  • 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.