# Hill cipher concepts

I have some questions about the Hill cipher.

1. There is a rule for key K: Determinant of matrix and number of characters of the alphabet must be coprime. What does this rule say? Why it must be like this? So If I had determinant 5 and characters 30, it would not work?

2. Let's assume I have key K which should be 3x3. I would get the key: ENCRYPT. I would create trigrams like ENC RYP T?? - now the last trigraph is missing two characters. Should this input key be REFUSED by application for example or should I fill it with some values like #?(# alphabet must contain this char). Or how should the application behave when this happens?

3. Let's assume I have plain text T which is: textabcd - again the same question (as 2.). I have TEX TAB CD? - (should I again insert something in that empty space (?) (for instance #) ?

4. Should HILL cipher accept MATRICES (input as matrices) or as alphabet key like ABCD?

• 1, The encryption must be invertible, 2. padding... 3. padding... 4. Text encoding. Nov 2 '20 at 12:28
• @kelalaka would you please add a little bit more information? 2 and 3 - padding: So how exactly should be empty space replaced? with some agreed character? 4 - means that I should convert text key to numbers in matrix?
– Arth
Nov 2 '20 at 14:02

The Hill Cipher as one of the classical cipher is invented by Lester S. Hill in 1929. It is the first polygraphic cipher that can operate more than one letter at a time.

1. There is a rule for key K: Determinant of matrix and number of characters of the alphabet must be coprime. What does this rule say? Why it must be like this? So If I had determinant 5 and characters 30, it would not work?

In the Hill cipher, there is an invertible matrix for the key $$K$$ of size $$n\times n$$, and the plaintexts $$m_i$$'s are row vectors of size $$n$$.

The encryption of a block $$m$$ is performed by $$c = m K$$ and the encryption of the message can be considered mathematically take $$c = m K$$. For decryption, multiply both sides with $$K^{-1}$$ and get $$cK^{-1} = m K K^{-1}$$ and $$cK^{-1} = m I$$. That is the inverse of a matrix and it is a well-known property that to be invertible a matrix must have of square size and have a non-zero determinant.

If the determinant has common factors with the alphabet size then it will not have inverse since invertible matrixes must have invertible determinants. This is due to fact that we are in the composite modulus. In this case, choose a new uniform random key or choose a prime alphabet.

1. Let's assume I have key K which should be 3x3. I would get the key: ENCRYPT. I would create trigrams like ENC RYP T?? - now the last trigraph is missing two characters. Should this input key be REFUSED by application for example or should I fill it with some values like #?(# alphabet must contain this char). Or how should the application behave when this happens?
2. Let's assume I have plain text T which is: textabcd - again the same question (as 2.). I have TEX TAB CD? - (should I again insert something in that empty space (?) (for instance #) ?

In modern Cryptography, we call it padding and the classical era has padding also. I'm not aware that Hill proposed any padding.

Fixed padding can cause serious problems with the Hill cipher since it hasn't got known-plaintext attack security. Putting X or any fixed character can cause information leak about the key.

Wikipedia states it very well. The messages are starting end ending with predictable ways, like my dear ambassador, Weather report, Sincerely your From .. To in the military, etc. Context can change according to the target. Therefore we need randomized padding in the beginning and at the end. This can cause confusion, too. The better one may use nonsense letters for this aim.

1. Should HILL cipher accept MATRICES (input as matrices) or as alphabet key like ABCD?

If I've understood this part correctly; first, the messages are encoded into row vectors of numbers by using the alphabet then multiplied with the key matrix $$K$$. The key matrix is also a number matrix, not a character. We operate on the numbers then decode the information back to character.

Little proof of the claim that the determinant must have relatively prime to $$n$$. When $$n$$ is a prime it is clear that we are in a field and the determinant must be non-zero by the Invertible Matrix Theorem

The theorem also gives the general case for this as;

In general, a square matrix over a commutative ring is invertible if and only if its determinant is a unit in that ring.

The units are invertible elements. For a given $$u$$, if $$\gcd(u,n)=1$$ then it has an inverse, therefore it is a unit. The converse is also true that for a given unit the $$\gcd(u,n)=1$$ must hold. We can see it form that $$u x \equiv 1 \pmod n$$ then we can form the bezout's identity $$ux+bn=1$$ then we can conclude that they are relatively prime.

Or directly; if $$ax\equiv 1 \pmod n$$, then there is a $$k$$ such that $$ax=bk+1$$, equivalently $$ax−bz=1.$$ If $$d=\gcd(a,b)$$, then $$d|a$$ and $$d|b$$, so $$d|ax−bz=1$$. We can conclude that $$d=1$$.

• thanks. Which padding should I use with Hill cipher then? Would you please give me example, how would be padding added for plain text like: NORM (for key=3x3)? I havent found any concrete padding techniques for Hill cipher...
– Arth
Nov 2 '20 at 22:29
• Add garbage characters to the beginning and to the end. Make it different for each message. That is up to you. Hill ciphers has no usage in modern era! Nov 2 '20 at 22:31
• "it is a well-known property that to be invertible a matrix must have of square size and have a non-zero determinant" does not explain why "Determinant of matrix and number of characters of the alphabet must be coprime". In the case of 26, or more generally the product of distinct primes, it's easy to give a rigorous explanation using the CRT. In the general case, well...
– fgrieu
Dec 2 '20 at 17:31
• @fgrieu Invertible determinants? Dec 2 '20 at 17:50
• Well, a square matrix with coefficients in the ring $\mathbb Z_m$ with $m$ the product of distinct primes $p_i$ is invertible iff it's discriminant is not divisible by any the of $p_i$, that is the discriminant is coprime with $m$. Proof follows from the CRT, and that property when $m$ is prime (in which case $\mathbb Z_m$ is a bona fide field just like good old $\mathbb R$, only finite). I don't see/remember if that holds in the general case. I learned the invertible matrix theorem in a field.
– fgrieu
Dec 2 '20 at 18:12