Skip to content

Commit ac59df6

Browse files
authored
Update hill_cipher.py
1 parent ea99904 commit ac59df6

1 file changed

Lines changed: 38 additions & 0 deletions

File tree

ciphers/hill_cipher.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,41 @@
1+
"""
2+
3+
Hill Cipher:
4+
The 'HillCipher' class below implements the Hill Cipher algorithm which uses
5+
modern linear algebra techniques to encode and decode text using an encryption
6+
key matrix.
7+
8+
Algorithm:
9+
Let the order of the encryption key be N (as it is a square matrix).
10+
Your text is divided into batches of length N and converted to numerical vectors
11+
by a simple mapping starting with A=0 and so on.
12+
13+
The key is then multiplied with the newly created batch vector to obtain the
14+
encoded vector. After each multiplication modular 36 calculations are performed
15+
on the vectors so as to bring the numbers between 0 and 36 and then mapped with
16+
their corresponding alphanumerics.
17+
18+
While decrypting, the decrypting key is found which is the inverse of the
19+
encrypting key modular 36. The same process is repeated for decrypting to get
20+
the original message back.
21+
22+
Constraints:
23+
The determinant of the encryption key matrix must be relatively prime w.r.t 36.
24+
25+
Note:
26+
This implementation only considers alphanumerics in the text. If the length of
27+
the text to be encrypted is not a multiple of the break key(the length of one
28+
batch of letters), the last character of the text is added to the text until the
29+
length of the text reaches a multiple of the break_key. So the text after
30+
decrypting might be a little different than the original text.
31+
32+
References:
33+
https://apprendre-en-ligne.net/crypto/hill/Hillciph.pdf
34+
https://www.youtube.com/watch?v=kfmNeskzs2o
35+
https://www.youtube.com/watch?v=4RhLNDqcjpA
36+
37+
"""
38+
139
import string
240

341
import numpy as np

0 commit comments

Comments
 (0)