```python
import numpy as np
```

# Key


```python
key = np.array([
    [3, 10, 20],
    [20, 9, 17],
    [9, 4, 17]
])
```


```python
key.shape[0] == key.shape[1]
```




    True




```python
np.linalg.det(key) != 0
```




    True




```python
m = "attack is to night"
```


```python
import commons
m = commons.preprocessing(m)
```


```python
m
```




    'attackistonight'




```python
plaintext = commons.lettersOfPlaintext(m)
```


```python
plaintext
```




    ['a', 't', 't', 'a', 'c', 'k', 'i', 's', 't', 'o', 'n', 'i', 'g', 'h', 't']




```python
plaintext_idx = []
for i in plaintext:
    plaintext_idx.append(commons.letterToNumber(i))
```


```python
plaintext_idx
```




    [0, 19, 19, 0, 2, 10, 8, 18, 19, 14, 13, 8, 6, 7, 19]




```python
plaintext_matrix = np.array(plaintext_idx)
```


```python
plaintext_matrix
```




    array([ 0, 19, 19,  0,  2, 10,  8, 18, 19, 14, 13,  8,  6,  7, 19])




```python
plaintext_matrix.resize(5,3)
```


```python
plaintext_matrix
```




    array([[ 0, 19, 19],
           [ 0,  2, 10],
           [ 8, 18, 19],
           [14, 13,  8],
           [ 6,  7, 19]])



# Encryption


```python
encryption = np.matmul(plaintext_matrix, key) % 26
```


```python
encryption
```




    array([[ 5, 13, 22],
           [ 0,  6, 22],
           [ 9,  6,  9],
           [10,  3, 13],
           [17, 17, 16]], dtype=int32)




```python
encryption.resize(1, 15)
```


```python
ciphertext = []
for i in encryption.tolist()[0]:
    ciphertext.append(commons.numberToLetter(i))
```


```python
ciphertext
```




    ['f', 'n', 'w', 'a', 'g', 'w', 'j', 'g', 'j', 'k', 'd', 'n', 'r', 'r', 'q']




```python
plaintext
```




    ['a', 't', 't', 'a', 'c', 'k', 'i', 's', 't', 'o', 'n', 'i', 'g', 'h', 't']



# Decryption


```python
from sympy import Matrix
```


```python
inv_key = Matrix(key).inv_mod(26)
```


```python
inv_key = np.array(inv_key)
```


```python
inv_key
```




    array([[11, 22, 14],
           [7, 9, 21],
           [17, 0, 3]], dtype=object)




```python
inv_key = inv_key.astype(float)
```


```python
np.matmul(key, inv_key)%26
```




    array([[1., 0., 0.],
           [0., 1., 0.],
           [0., 0., 1.]])




```python
encryption.resize(5, 3)
```


```python
decryption = np.matmul(encryption, inv_key) % 26
```


```python
decryption
```




    array([[ 0., 19., 19.],
           [ 0.,  2., 10.],
           [ 8., 18., 19.],
           [14., 13.,  8.],
           [ 6.,  7., 19.]])




```python
decryption.resize(1, 15)
```


```python
restored_text = []
for i in decryption.tolist()[0]:
    restored_text.append(commons.numberToLetter(i))
```


```python
restored_text
```




    ['a', 't', 't', 'a', 'c', 'k', 'i', 's', 't', 'o', 'n', 'i', 'g', 'h', 't']



ee2343f2a0c0946f3e5f0d51a4bd45606c894804
ipynb
Jupyter Notebook
python/classical/Hill.ipynb
nth15/crypto
702f3b6d552a582e5c7cd6b53cfd2757ad7634c7
MIT
2018-02-16T06:24:22.000Z
2022-03-18T06:11:20.000Z
python/classical/Hill.ipynb
nth15/crypto
702f3b6d552a582e5c7cd6b53cfd2757ad7634c7
MIT
2020-04-23T12:37:58.000Z
2020-04-23T12:37:58.000Z
python/classical/Hill.ipynb
nth15/crypto
702f3b6d552a582e5c7cd6b53cfd2757ad7634c7
MIT
2018-02-13T11:12:14.000Z
2022-03-21T19:51:05.000Z
Qwen/Qwen-72B
1. YES
2. YES