This article includes a list ofgeneral references, butit lacks sufficient correspondinginline citations. Please help toimprove this article byintroducing more precise citations.(February 2012) (Learn how and when to remove this message) |

Inclassical cryptography, theHill cipher is apolygraphic substitution cipher based onlinear algebra. Invented byLester S. Hill in 1929, it was the first polygraphic cipher in which it was practical (though barely) to operate on more than three symbols at once.
The following discussion assumes an elementary knowledge ofmatrices.
Each letter is represented by a numbermodulo 26. Though this is not an essential feature of the cipher, this simple scheme is often used:
| Letter | A | B | C | D | E | F | G | H | I | J | K | L | M | N | O | P | Q | R | S | T | U | V | W | X | Y | Z |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Number | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 |
To encrypt a message, each block ofn letters (considered as ann-componentvector) is multiplied by aninvertiblen ×nmatrix, againstmodulus 26. To decrypt the message, each block is multiplied by the inverse of the matrix used for encryption.
The matrix used for encryption is the cipherkey, and it should be chosen randomly from the set of invertiblen ×n matrices (modulo 26). The cipher can, of course, be adapted to an alphabet with any number of letters; all arithmetic just needs to be done modulo the number of letters instead of modulo 26.
Consider the message 'ACT', and the key below (or GYB/NQK/URP in letters):
Since 'A' is 0, 'C' is 2 and 'T' is 19, the message is the vector:
Thus the enciphered vector is given by:
which corresponds to aciphertext of 'POH'. Now, suppose that our message is instead 'CAT', or:
This time, the enciphered vector is given by:
which corresponds to a ciphertext of 'FIN'. Every letter has changed. The Hill cipher has achievedShannon'sdiffusion, and ann-dimensional Hill cipher can diffuse fully acrossn symbols at once.
In order to decrypt, we turn the ciphertext back into a vector, then simply multiply by theinverse matrix of the key matrix (IFK/VIV/VMI in letters). We find that,modulo 26, the inverse of the matrix used in the previous example is:
Taking the previous example ciphertext of 'POH', we get:
which gets us back to 'ACT', as expected.
One complication exists in picking the encrypting matrix:
Thus, if we work modulo 26 as above, the determinant must be nonzero, and must not be divisible by 2 or 13. If the determinant is 0, or has common factors with the modular base, then the matrix cannot be used in the Hill cipher, and another matrix must be chosen (otherwise it will not be possible to decrypt). Fortunately, matrices which satisfy the conditions to be used in the Hill cipher are fairly common.
For our example key matrix:
So, modulo 26, the determinant is 25. Since and, 25 has no common factors with 26, and this matrix can be used for the Hill cipher.
The risk of the determinant having common factors with the modulus can be eliminated by making the modulusprime. Consequently, a useful variant of the Hill cipher adds 3 extra symbols (such as a space, a period and a question mark) to increase the modulus to 29 as 27 is 3 cubed and 28 is 2 times 14 or 4 times 7 .
Let
be the key and suppose the plaintext message is 'HELP'. Then this plaintext is represented by two pairs
Then we compute
and continue encryption as follows:
The matrixK is invertible, hence exists such that.The inverse ofK can be computed by using theformula
This formula still holds after a modular reduction if amodular multiplicative inverse is used to compute. Hence in this case, we compute
Then we compute
Therefore,
The basic Hill cipher is vulnerable to aknown-plaintext attack because it is completelylinear. An opponent who intercepts plaintext/ciphertext character pairs can set up a linear system which can (usually) be easily solved; if it happens that this system is indeterminate, it is only necessary to add a few more plaintext/ciphertext pairs. Calculating this solution by standard linear algebra algorithms then takes very little time.
While matrix multiplication alone does not result in a secure cipher it is still a useful step when combined with othernon-linear operations, because matrix multiplication can providediffusion. For example, an appropriately chosen matrix can guarantee that small differences before the matrix multiplication will result in large differences after the matrix multiplication. Indeed, some modern ciphers use a matrix multiplication step to provide diffusion. For example, the MixColumns step inAES is a matrix multiplication. The functiong inTwofish is a combination of non-linear S-boxes with a carefully chosen matrix multiplication (MDS).
Thekey space is the set of all possible keys. The key space size is the number of possible keys. The effectivekey size, in number of bits, is thebinary logarithm of the key space size.
There are matrices of dimensionn ×n. Thus or about is an upper bound on the key size of the Hill cipher usingn ×n matrices. This is only an upper bound because not every matrix is invertible and thus usable as a key. The number of invertible matrices can be computed via theChinese Remainder Theorem. I.e., a matrix is invertible modulo 26 if and only if it is invertible both modulo 2 and modulo 13.The number of invertiblen ×n matrices modulo 2 is equal to the order of thegeneral linear group GL(n,Z2). It is
Equally, the number of invertible matrices modulo 13 (i.e. the order of GL(n,Z13)) is
The number of invertible matrices modulo 26 is the product of those two numbers. Hence it is
Additionally it seems to be prudent to avoid too many zeroes in the key matrix, since they reduce diffusion. The net effect is that the effective keyspace of a basic Hill cipher is about. For a 5 × 5 Hill cipher, that is about 114 bits. Of course, key search is not the most efficient known attack.
When operating on 2 symbols at once, a Hill cipher offers no particular advantage overPlayfair or thebifid cipher, and in fact is weaker than either, and slightly more laborious to operate by pencil-and-paper. As the dimension increases, the cipher rapidly becomes infeasible for a human to operate by hand.
A Hill cipher of dimension 6 was implemented mechanically. Hill and a partner were awarded apatent (U.S. patent 1,845,947) for this device, which performed a 6 × 6 matrix multiplication modulo 26 using a system of gears and chains.
Unfortunately the gearing arrangements (and thus the key) were fixed for any given machine, so triple encryption was recommended for security: a secret nonlinear step, followed by the wide diffusive step from the machine, followed by a third secret nonlinear step. (The much laterEven–Mansour cipher also uses an unkeyed diffusive middle step). Such a combination was actually very powerful for 1929, and indicates that Hill apparently understood the concepts of ameet-in-the-middle attack as well as confusion and diffusion. Unfortunately, his machine did not sell.[citation needed]
Other practical "pencil-and-paper" polygraphic ciphers include: