VOOZH about

URL: https://www.geeksforgeeks.org/dsa/hill-cipher/

⇱ Hill Cipher - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Hill Cipher

Last Updated : 11 Mar, 2026

Hill Cipher is a polygraphic substitution cipher based on linear algebra. In this method, each letter of the alphabet is represented by a number modulo 26, commonly using the scheme A = 0, B = 1, …, Z = 25.

Example:

Input : Plaintext: ACT 
Key: GYBNQKURP
Output: Ciphertext: POH

Input: Plaintext: GFG
Key: HILLMAGIC
Output: Ciphertext: SWK

Encryption in Hill Cipher

For encryption ,

  • Divide the plaintext into blocks of n letters.
  • Treat each block as an n-dimensional vector.
  • Multiply this vector by an invertible n × n key matrix.
  • Take the result modulo 26.
  • The final values form the ciphertext.

Illustration

We have to encrypt the message 'ACT' (n=3).The key is 'GYBNQKURP' which can be written as the nxn matrix ,

👁 Cipherkey

The message 'ACT' is written as vector, 

👁 message vector

The enciphered vector is given as,

👁 enciphered vector

which corresponds to ciphertext of 'POH' 


Output
Ciphertext: POH

Decryption in Hill Cipher

For decryption,

  • Find the inverse of the key matrix (mod 26).
  • Divide the ciphertext into blocks of n letters.
  • Treat each block as an n-dimensional vector.
  • Multiply each vector by the inverse key matrix.
  • Take the result modulo 26.
  • Convert the numbers back to letters to obtain the original plaintext.

Illustration

The inverse of the matrix the key "GYBNQKURP" will be ,

👁 inverse matrix

Now the Ciphertext is "POH",

👁 Decrypt

So, the result is "ACT".


Output
Decrypted Text: ACT
Comment
Article Tags:
Article Tags: