Skip to content

Resource · Guide or article

Hill Cipher

Walks through the Hill cipher, a polygraphic substitution cipher that encrypts blocks of letters by multiplying them with an invertible key matrix modulo 26. It assumes you can multiply matrices and work with remainders, so it suits candidates reviewing math-heavy algorithm topics.

Reviewed

Open the original: Hill Cipher (opens the original site in a new tab)

Add to a roadmap

Keep this resource with a roadmap you follow. It appears as your own extra resource; the roadmap's steps don't change and it doesn't count toward your tracked steps.

Source content: English

The Hill cipher is a polygraphic substitution cipher based on linear algebra. Each letter is represented by a number modulo 26. Though this is not an essential feature of the cipher, this simple scheme is often used: To encrypt a message, each block of n letters (considered as an n-component vector) is multiplied by an invertible n × n matrix, against modulus 26.

PrepKit overview

PrepKit editors wrote and reviewed this overview from the source repository. It is not text from the resource itself.

Suited to
The source does not state a level
Format
Article

What it covers

  • Table mapping the letters A to Z onto the numbers 0 to 25
  • Encrypts ACT and then CAT with the same 3 by 3 key, showing that reordering the same letters changes every output letter
  • Decryption with the inverse key matrix modulo 26, recovering ACT from the ciphertext POH
  • Two conditions for a usable key: a nonzero determinant that shares no factor with the modulus, so not divisible by 2 or 13 when working mod 26

How to use it

PrepKit study advice, not a claim made by the source.

Redo the ACT to POH example by hand, then code a 2 by 2 version that refuses keys whose determinant shares a factor with 26. You are finished when decrypt round-trips several random messages.

Scope and limitations

The README does not show how to compute a modular matrix inverse; it sends you to Wikipedia, MathIsFun and GeeksForGeeks for that. There is no complexity analysis and no exercise set.

Access and format

README on GitHub in the MIT-licensed trekhleb/javascript-algorithms repository; English text, free to read.

Reviewed by PrepKit on Oct 2, 2026. Listing source commit: 85293e3e2b88

Evidence behind this overview
  • src/algorithms/cryptography/hill-cipher/README.md

Pages that include this

Get the PrepKit newsletter

Optional. You don't need it to use PrepKit.

How we handle your email