-
Notifications
You must be signed in to change notification settings - Fork 22
/
hill_decryption.py
187 lines (159 loc) · 5.01 KB
/
hill_decryption.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
import string
import numpy as np
main=string.ascii_lowercase
def generate_key(n,s):
s=s.replace(" ","")
s=s.lower()
key_matrix=['' for i in range(n)]
i=0;j=0
for c in s:
if c in main:
key_matrix[i]+=c
j+=1
if(j>n-1):
i+=1
j=0
print("The key matrix "+"("+str(n)+'x'+str(n)+") is:")
print(key_matrix)
key_num_matrix=[]
for i in key_matrix:
sub_array=[]
for j in range(n):
sub_array.append(ord(i[j])-ord('a'))
key_num_matrix.append(sub_array)
for i in key_num_matrix:
print(i)
return(key_num_matrix)
def modInverse(a, m) :
a = a % m;
for x in range(1, m) :
if ((a * x) % m == 1) :
return x
return 1
def method(a, m) :
if(a>0):
return (a%m)
else:
k=(abs(a)//m)+1
return method(a+k*m,m)
def message_matrix(s,n):
s=s.replace(" ","")
s=s.lower()
final_matrix=[]
if(len(s)%n!=0):
# may be negative also
for i in range(abs(len(s)%n)):
# z is the bogus word
s=s+'z'
print("Converted cipher_text for decryption: ",s)
for k in range(len(s)//n):
message_matrix=[]
for i in range(n):
sub=[]
for j in range(1):
sub.append(ord(s[i+(n*k)])-ord('a'))
message_matrix.append(sub)
final_matrix.append(message_matrix)
print("The column matrices of plain text in numbers are: ")
for i in final_matrix:
print(i)
return(final_matrix)
def multiply_and_convert(key,message):
# multiplying matrices
# resultant must have:
# rows = numbers of rows in message matrix
# columns = number of columns in key matrix
res_num = [[0 for x in range(len(message[0]))] for y in range(len(key))]
for i in range(len(key)):
for j in range(len(message[0])):
for k in range(len(message)):
# resulted number matrix
res_num[i][j]+=key[i][k] * message[k][j]
res_alpha = [['' for x in range(len(message[0]))] for y in range(len(key))]
# getting the alphabets from the numbers
# according to the logic of hill ciipher
for i in range(len(key)):
for j in range(len(message[0])):
# resultant alphabet matrix
res_alpha[i][j]+=chr((res_num[i][j]%26)+97)
return(res_alpha)
n=int(input("What will be the order of square matrix: "))
s=input("Enter the key: ")
key_matrix=generate_key(n,s)
A = np.array(key_matrix)
det=np.linalg.det(A)
adjoint=det*np.linalg.inv(A)
if(det!=0):
convert_det=modInverse(int(det),26)
adjoint=adjoint.tolist()
print("Adjoint Matrix before modulo26 operation: ")
for i in adjoint:
print(i)
print(convert_det)
# applying modulo 26 to all elements in adjoint matrix
for i in range(len(adjoint)):
for j in range(len(adjoint[i])):
adjoint[i][j]=round(adjoint[i][j])
adjoint[i][j]=method(adjoint[i][j],26)
print("Adjoint Matrix after modulo26 operation: ")
for i in adjoint:
print(i)
# modulo is applied to inverse of determinant and
# multiplied to all elements in the adjoint matrix
# to form inverse matrix
adjoint=np.array(adjoint)
inverse=convert_det*adjoint
inverse=inverse.tolist()
for i in range(len(inverse)):
for j in range(len(inverse[i])):
inverse[i][j]=inverse[i][j]%26
print("Inverse matrix after applying modulo26 operation: ")
for i in inverse:
print(i)
cipher_text=input("Enter the cipher text: ")
message=message_matrix(cipher_text,n)
plain_text=''
for i in message:
sub=multiply_and_convert(inverse,i)
for j in sub:
for k in j:
plain_text+=k
print("plain message: ",plain_text)
else:
print("Matrix cannot be inverted")
'''
----------OUTPUT----------
What will be the order of square matrix: 3
Enter the key: BACK UP ABC
The key matrix (3x3) is:
['bac', 'kup', 'abc']
[1, 0, 2]
[10, 20, 15]
[0, 1, 2]
Adjoint Matrix before modulo26 operation:
[25.000000000000014, 2.000000000000001, -40.000000000000014]
[-20.000000000000004, 2.000000000000001, 5.000000000000001]
[10.000000000000002, -1.0000000000000004, 20.000000000000004]
11
Adjoint Matrix after modulo26 operation:
[25, 2, 12]
[6, 2, 5]
[10, 25, 20]
Inverse matrix after applying modulo26 operation:
[15, 22, 2]
[14, 22, 3]
[6, 15, 12]
Enter the cipher text: tvuppmaqilyyocsovpierkhx
Converted cipher_text for decryption: tvuppmaqilyyocsovpierkhx
The column matrices of plain text in numbers are:
[[19], [21], [20]]
[[15], [15], [12]]
[[0], [16], [8]]
[[11], [24], [24]]
[[14], [2], [18]]
[[14], [21], [15]]
[[8], [4], [17]]
[[10], [7], [23]]
plain message: hitheremynameisabhiramzz
>>>
'''