-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhill_cipher.py
43 lines (35 loc) · 991 Bytes
/
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
def hill(code):
decryptionKey = [[2,0,3],
[23,5,11],
[7,6,25]]
code = code.lower()
output = [[0],[0],[0]]
counter = 0
for character in code:
number = ord(character) - 97
output[counter][0] = number
counter += 1
result = [[0],
[0],
[0]]
for i in range(len(decryptionKey)):
for j in range(len(output[0])):
for k in range(len(output)):
result[i][0] += decryptionKey[i][k] * output[k][j]
unCiphered = ""
for r in result:
numeric_letter = r[0] % 26
val = chr(numeric_letter + 97)
unCiphered = unCiphered + val
return unCiphered
def main():
code = raw_input("Enter ciphertext: ")
print
plaintext = ""
while(code):
ciphertext = code[:3]
code = code[3:]
plaintext = plaintext + hill(ciphertext)
print plaintext
print
main()