-
Notifications
You must be signed in to change notification settings - Fork 0
/
encoder_decoder_program_hill_cypher.py
227 lines (116 loc) · 4.82 KB
/
encoder_decoder_program_hill_cypher.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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
import pickle
import numpy as np
def inputObtainer(): #obtains input and converts it into a useable format where number of characters in string is multiple of 2
inputStr=input("Enter your input with lowercase characters only ->")
while inputStr.islower()==False:
inputStr=input("Please enter valid input")
if len(inputStr)%2==1:
inputStr=inputStr+'a'
return inputStr
def matrixMaker(string): #converts the input of a string into its matrix format
randomMatrix=[[2],[2]]
for i in range (2):
for j in range(1):
randomMatrix[i][j]=ord(string[i])%96
return randomMatrix
def encrypt(matrix): #multiplies the message matrix with the key matrix to obtain the encrypted matrix
placeholderMatrix=[[0],[0]]
for i in range(2):
for j in range(1):
for x in range(2):
placeholderMatrix[i][j] += (keyMatrix[i][x] * matrix[x][j])
placeholderMatrix[i][j] = placeholderMatrix[i][j] % 26
return placeholderMatrix
def matrixToText(matrix): #converts matrix to text
text=''
for i in range(2):
for j in range(1):
text+=chr(matrix[i][j]+96)
return text
def inputProcessorForEncryption(inputToEncrypt): #splits the text into blocks of 2 and then encrypts each block
placeholder1=''
k=int(len(inputToEncrypt)/2)
for i in range(1,k+1):
placeholder2=''
for j in range((i-1)*2,i*2):
placeholder2+=inputToEncrypt[j]
messageMatrix=matrixMaker(placeholder2)
placeholder1+=matrixToText(encrypt(messageMatrix))
return placeholder1
def decrypt(matrix):
placeholder=[[0],[0]]
for i in range(2):
for j in range(1):
for x in range(2):
placeholder[i][j]+=modularInvertedMatrix[i][x]*matrix[x][j]
placeholder[i][j]=placeholder[i][j]%26
return placeholder
def inputProcessorForDecryption(inputToDecrypt):
placeholder1=''
k=int(len(inputToDecrypt)/2)
for i in range(1,k+1):
placeholder2=''
for j in range((i-1)*2,i*2):
placeholder2+=inputToDecrypt[j]
encryptedMatrix=matrixMaker(placeholder2)
placeholder1+=matrixToText(decrypt(encryptedMatrix))
return placeholder1
'''
def adjointer(matrix): #converts matrix to adjoint through normal algebraic method
adjoint=[[0,0],[0,0]]
inverse=np.linalg.inv(matrix)
for i in range(2):
for j in range(1):
adjoint[i][j]=inverse[i][j]*(np.linalg.det(matrix))
return adjoint
def modularInverseMatrix(matrix):#used to calculate the modular inverse, useable for decryption
adjointMatrix=[[0,0],[0,0]]
adjointMatrix=adjointer(matrix)
determinant=np.linalg.det(matrix)
invertedMatrix=[[0,0],[0,0]]
for i in range(3):
for j in range(3):
invertedMatrix[i][j]=cofactorMatrix[i][j]*pow(cofactorMatrix[i][j],-1,determinant)
return invertedMatrix
def obtainingKey():
key=input('Please enter a 9 letter lowercase word without any other characters.')
key=key.strip()
while (key.isalpha() and key.islower() and len(key)==9)==False:
key=input('Please enter a valid key')
counter=0
while np.linalg.det(keyMatrix)!=0:
for i in range(3):
for j in range(3):
keyMatrix[i][j]=ord(key[counter])%96
counter+=1
if np.linalg.det(keyMatrix)==0:
key=input('This key forms a singular matrix, please try entering something else')
continue
else:
break
'''
with open("info.dat",'rb') as datafile:
variableTuple=pickle.load(datafile)
encryptedInformation=variableTuple[0]
decryptedIformation=variableTuple[1]
keyMatrix=variableTuple[2]
messageMatrix=variableTuple[3]
encryptedMatrix=variableTuple[4]
modularInvertedMatrix=variableTuple[5]
while True:
var1=input('Would you like to encrypt or decrypt? (Type exit to quit)')
if var1.lower()=='encrypt':
inputToEncrypt=inputObtainer()
encryptedInformation=inputProcessorForEncryption(inputToEncrypt)
print(encryptedInformation)
continue
elif var1.lower()=='decrypt':
inputToDecrypt=input('Enter encrypted information ->')
decryptedInformation=inputProcessorForDecryption(inputToDecrypt)
print(decryptedInformation)
continue
elif var1.lower()=='exit':
break
else:
var1=input('Please try to enter again, encrypt or decrypt?')
continue