-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathhill_cipher.py
199 lines (167 loc) · 5.18 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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
# *********
# -*- Made by VoxelPixel
# -*- For YouTube Tutorial
# -*- https://github.com/VoxelPixel
# -*- Support me on Patreon: https://www.patreon.com/voxelpixel
# *********
import sys
import numpy as np
def cipher_encryption():
msg = input("Enter message: ").upper()
msg = msg.replace(" ", "")
# if message length is odd number, append 0 at the end
len_chk = 0
if len(msg) % 2 != 0:
msg += "0"
len_chk = 1
# msg to matrices
row = 2
col = int(len(msg)/2)
msg2d = np.zeros((row, col), dtype=int)
itr1 = 0
itr2 = 0
for i in range(len(msg)):
if i % 2 == 0:
msg2d[0][itr1] = int(ord(msg[i])-65)
itr1 += 1
else:
msg2d[1][itr2] = int(ord(msg[i])-65)
itr2 += 1
# for
key = input("Enter 4 letter Key String: ").upper()
key = key.replace(" ", "")
# key to 2x2
key2d = np.zeros((2, 2), dtype=int)
itr3 = 0
for i in range(2):
for j in range(2):
key2d[i][j] = ord(key[itr3])-65
itr3 += 1
# checking validity of the key
# finding determinant
deter = key2d[0][0] * key2d[1][1] - key2d[0][1] * key2d[1][0]
deter = deter % 26
# finding multiplicative inverse
mul_inv = -1
for i in range(26):
temp_inv = deter * i
if temp_inv % 26 == 1:
mul_inv = i
break
else:
continue
# for
if mul_inv == -1:
print("Invalid key")
sys.exit()
# if
encryp_text = ""
itr_count = int(len(msg)/2)
if len_chk == 0:
for i in range(itr_count):
temp1 = msg2d[0][i] * key2d[0][0] + msg2d[1][i] * key2d[0][1]
encryp_text += chr((temp1 % 26) + 65)
temp2 = msg2d[0][i] * key2d[1][0] + msg2d[1][i] * key2d[1][1]
encryp_text += chr((temp2 % 26) + 65)
# for
else:
for i in range(itr_count-1):
temp1 = msg2d[0][i] * key2d[0][0] + msg2d[1][i] * key2d[0][1]
encryp_text += chr((temp1 % 26) + 65)
temp2 = msg2d[0][i] * key2d[1][0] + msg2d[1][i] * key2d[1][1]
encryp_text += chr((temp2 % 26) + 65)
# for
# if else
print("Encrypted Text: {}".format(encryp_text))
def cipher_decryption():
msg = input("Enter message: ").upper()
msg = msg.replace(" ", "")
# if message length is odd number, append 0 at the end
len_chk = 0
if len(msg) % 2 != 0:
msg += "0"
len_chk = 1
# msg to matrices
row = 2
col = int(len(msg) / 2)
msg2d = np.zeros((row, col), dtype=int)
itr1 = 0
itr2 = 0
for i in range(len(msg)):
if i % 2 == 0:
msg2d[0][itr1] = int(ord(msg[i]) - 65)
itr1 += 1
else:
msg2d[1][itr2] = int(ord(msg[i]) - 65)
itr2 += 1
# for
key = input("Enter 4 letter Key String: ").upper()
key = key.replace(" ", "")
# key to 2x2
key2d = np.zeros((2, 2), dtype=int)
itr3 = 0
for i in range(2):
for j in range(2):
key2d[i][j] = ord(key[itr3]) - 65
itr3 += 1
# for
# finding determinant
deter = key2d[0][0] * key2d[1][1] - key2d[0][1] * key2d[1][0]
deter = deter % 26
# finding multiplicative inverse
mul_inv = -1
for i in range(26):
temp_inv = deter * i
if temp_inv % 26 == 1:
mul_inv = i
break
else:
continue
# for
# adjugate matrix
# swapping
key2d[0][0], key2d[1][1] = key2d[1][1], key2d[0][0]
# changing signs
key2d[0][1] *= -1
key2d[1][0] *= -1
key2d[0][1] = key2d[0][1] % 26
key2d[1][0] = key2d[1][0] % 26
# multiplying multiplicative inverse with adjugate matrix
for i in range(2):
for j in range(2):
key2d[i][j] *= mul_inv
# modulo
for i in range(2):
for j in range(2):
key2d[i][j] = key2d[i][j] % 26
# cipher to plain
decryp_text = ""
itr_count = int(len(msg) / 2)
if len_chk == 0:
for i in range(itr_count):
temp1 = msg2d[0][i] * key2d[0][0] + msg2d[1][i] * key2d[0][1]
decryp_text += chr((temp1 % 26) + 65)
temp2 = msg2d[0][i] * key2d[1][0] + msg2d[1][i] * key2d[1][1]
decryp_text += chr((temp2 % 26) + 65)
# for
else:
for i in range(itr_count - 1):
temp1 = msg2d[0][i] * key2d[0][0] + msg2d[1][i] * key2d[0][1]
decryp_text += chr((temp1 % 26) + 65)
temp2 = msg2d[0][i] * key2d[1][0] + msg2d[1][i] * key2d[1][1]
decryp_text += chr((temp2 % 26) + 65)
# for
# if else
print("Decrypted Text: {}".format(decryp_text))
def main():
choice = int(input("1. Encryption\n2. Decryption\nChoose(1,2): "))
if choice == 1:
print("---Encryption---")
cipher_encryption()
elif choice == 2:
print("---Decryption---")
cipher_decryption()
else:
print("Invalid Choice")
if __name__ == "__main__":
main()