Tool to decrypt/encrypt with Hill cipher, a ciphering system similar to affine cipher but using a matrix for the gradient.

Hill Cipher - dCode

Tag(s) : Cryptography, Substitution Cipher

dCode is free and its tools are a valuable help in games, puzzles and problems to solve every day!

You have a problem, an idea for a project, a specific need and dCode can not (yet) help you? You need custom development? *Contact-me*!

This page is using the new English version of dCode, *please make comments* !

Sponsored ads

Tool to decrypt/encrypt with Hill cipher, a ciphering system similar to affine cipher but using a matrix for the gradient.

Hill cipher encryption uses a matrix \( M \) (and an alphabet).

Example: Consider the plain text DCODE and the matrix \( M \) (size 2): $$ M = \begin{pmatrix} 2 & 3 \\ 5 & 7 \end{pmatrix} $$ and the latin alphabet ABCDEFGHIJKLMNOPQRSTUVWXYZ

Split the text into n-grams with \( n \) the matrix size, if needed, complete the last n-gram with any letter.

Example: The matrix \( M \) is a 2x2 matrix, DCODE, split in 2-grams, becomes DC,OD,EZ (Z letter has been added to complete the last bigram)

Substitute the letters of the plain message with a value: their rank in the alphabet starting from 0.

Example: For ABCDEFGHIJKLMNOPQRSTUVWXYZ, it leads to A=0,B=1,...,Z=25. Sometimes ZABCDEFGHIJKLMNOPQRSTUVWXY is used in order to get A=1,B=2,...Y=25,Z=0.

Groups of letters DC, OD, EZ become the groups of values (3,2), (14,3), (4,25)

For each group of values P of the plain text (equivalent to a vector of size \( n \)), compute the matrix product: $$ M.P \equiv C \mod 26 $$ where \( C \) is a vector (a group) of ciphered values and \( 26 \) the alphabet length.

Example: $$ \begin{pmatrix} 2 & 3 \\ 5 & 7 \end{pmatrix} \begin{pmatrix} 3 \\ 2 \end{pmatrix} \equiv \begin{pmatrix} 12 \\ 3 \end{pmatrix} \mod 26 $$

From cipher values C, one can find the cipher letters with their rank in the alphabet.

Example: 12 is equal to M and 3 is equal to D.

So, DCODEZ is encrypted MDLNFN.

Hill cipher decryption needs the matrix and the alphabet used. Decryption involves matrix computations such as matrix inversion, and arithmetic calculations such as modular inverse.

To decrypt hill ciphertext, compute the matrix inverse modulo 26 (where 26 is the alphabet length), requiring the matrix to be invertible.

Example: Using the example matrix, compute the inverse matrix (modulo 26) : $$ \begin{pmatrix} 2 & 3 \\ 5 & 7 \end{pmatrix}^{-1} \equiv \begin{pmatrix} -7 & 3 \\ 5 & -2 \end{pmatrix} \equiv \begin{pmatrix} 19 & 3 \\ 5 & 24 \end{pmatrix} \mod 26 $$

Decryption consists in encrypting the ciphertext with the inverse matrix.

Note that not all matrices can be adapted to hill cipher. The determinant of the matrix has to be coprime with 26. For a 2x2 matrix, the 4 numbers \( \{ a,b,c,d \} \) must satisfy the condition that \( ad-bc \) is coprime with 26.

The ciphered message has a small index of coincidence and similar ngrams can be coded using the same letters.

dCode proposes to bruteforce test around 8000 combinations of 2x2 matrices and alphabets.

Hill is already a variant of Affine cipher. Few variants, except the use of large size matrices.

In 1929 by Lester S. Hill

dCode retains ownership of the source code of the script Hill Cipher. Except explicit open source licence (indicated Creative Commons / free), any algorithm, applet, snippet, software (converter, solver, encryption / decryption, encoding / decoding, ciphering / deciphering, translator), or any function (convert, solve, decrypt, encrypt, decipher, cipher, decode, code, translate) written in any informatic langauge (PHP, Java, C#, Python, Javascript, etc.) which dCode owns rights can be transferred after sales quote. So if you need to download the Hill Cipher script for offline use, for you, your company or association, see you on contact page !

hill,cipher,affine,modulo,matrix,lester,inverse,determinant

Source : http://www.dcode.fr/hill-cipher

© 2017 dCode — The ultimate 'toolkit' to solve every games / riddles / geocaches. dCode