-
Notifications
You must be signed in to change notification settings - Fork 0
/
hill_cipher.py
55 lines (47 loc) · 1.65 KB
/
hill_cipher.py
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import numpy as np
import string
from sympy import Matrix
def hc_encrypt(msg, key):
dimension = 3
msg = msg.replace(" ", "")
alphabets = string.ascii_lowercase
encrypted_message = ""
for index, i in enumerate(msg):
values = []
if index % dimension == 0:
for j in range(0, dimension):
if index + j < len(msg):
values.append([alphabets.index(msg[index + j])])
else:
values.append([25])
vector = np.matrix(values)
vector = key * vector
vector = vector % 26
for j in range(0, dimension):
encrypted_message = encrypted_message + alphabets[vector.item(j)]
return encrypted_message
def hc_decrypt(msg, key):
dimension = 3
alphabet = string.ascii_lowercase
decrypted_message = ""
key = Matrix(key)
key = key.inv_mod(26)
key = key.tolist()
for index, i in enumerate(msg):
values = []
if index % dimension == 0:
for j in range(0, dimension):
values.append([alphabet.index(msg[index + j])])
vector = np.matrix(values)
vector = key * vector
vector = vector % 26
for j in range(0, dimension):
decrypted_message = decrypted_message + alphabet[vector.item(j)]
return decrypted_message
message = input("Enter String: ").lower()
print("Original Message: ", message)
# key = hillciphr
key_matrix = np.matrix([[7, 8, 11], [11, 2, 8], [15, 7, 17]])
enc = hc_encrypt(message, key_matrix)
print("Encrypted Message: ", enc)
print("Decrypted Message: ", hc_decrypt(enc, key_matrix))