forked from DCRasPi/RasPi-Encryption
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMainMenu.py
More file actions
344 lines (254 loc) · 10.1 KB
/
MainMenu.py
File metadata and controls
344 lines (254 loc) · 10.1 KB
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
#This is the menu. Here are the various options
#There are four options on our menu
#1. Encrypt
#2. Decrypt
#3. Marry
import pickle
import Encrypt
import ast
import KeyGenerator
import Decryption
import Hasher
def MainMenu(OptionList):
x = (len(OptionList))
i = 0
while i < x:
print (str(i + 1) + "). " + OptionList[i])
i = i + 1
choice = raw_input("Choose your option: ")
truechoice = CheckIfInteger(choice)
return truechoice
def ConvertKeyToString(KeyDict):
String = KeyDict[0] + KeyDict[1] + KeyDict[2] + KeyDict[3]
return String
def ReadThisFile(Filename):
HashCipherList = []
HashCipherList.append("")
HashCipherList.append("")
Filehandle = open (Filename, 'r')
tempstring = Filehandle.readline()
while tempstring != "\n":
tempstring = tempstring.replace(" ", "")
tempstring = tempstring[0:10]
HashCipherList[0] = HashCipherList[0] + tempstring
tempstring = Filehandle.readline()
HashCipherList[1] = Filehandle.readline()
Filehandle.close
return HashCipherList
def CheckIfInteger(choice):
string = choice
while True:
try:
truechoice = int(string)
break
except ValueError:
string = raw_input("Please enter a number: ")
return truechoice
def FindKey(Mount):
Filename = "/media/" + Mount + "/Key/Key.txt"
Filehandle = open ( Filename, 'r')
KeyDict = Filehandle.read()
KeyDict = ast.literal_eval(KeyDict)
Filehandle.close
return KeyDict
def NumericCipherList(Cipherstring):
cipherlist = []
i = 0
while not ((i+2) > len(Cipherstring)):
cipherlist.append(int(Cipherstring[i:(i+2)]))
i = i + 2
return cipherlist
def Continue():
print ("Would you like to continue using this program?, 1 for yes, 2 for no.")
tchoice = CheckIfInteger(raw_input())
if tchoice == 1:
loop = 0
else:
loop = 1
return loop
def GetKey(Mount):
print "Would you like to enter a key, or find one from the USB"
print "1 for enter, 2 for search"
tchoice = CheckIfInteger(raw_input())
if tchoice == 1:
Key = raw_input()
else:
Key = FindKey(Mount)
return Key
def WriteNewKey(Mount, Key):
Filename = "/media/" + Mount + "/Key/Key.txt"
Filehandle = open ( Filename, 'w' )
Filehandle.write("{0: '" + Key[0:4] + "', 1: '" + Key[4:8] + "', 2: '" + Key[8:12] + "', 3: '" + Key[12:16] + "'}")
Filehandle.close
return 1
def WriteHash(HashW, FileW):
Filehandle = open (FileW, 'r+')
Filehandle.seek(0, 2)
Filehandle.write('\n')
Filehandle.write(str(HashW))
Filehandle.close
return 1
def Marry():
print "Are you Pi 1 or Pi 2 (Pi 1 will generate the key and give it to Pi 2)"
print "Enter 1 or 2"
user_input = int(raw_input())
if user_input == 1:
NewKey = Encrypt.GenerateKey()
print "Enter this key into Pi2, word for word"
print NewKey
else:
print "Please enter the key, matching exactly the key that is used"
NewKey = raw_input()
NewKey = ast.literal_eval(NewKey)
return NewKey
def PrintDecryptedText(DecryptedText, filename):
i = 0
Filehandle = open (filename, 'w')
uBound = len(DecryptedText)
while not (i + 10) > uBound:
if (i+20) > uBound:
tempstring = DecryptedText[i:(i+10)] + '\n'
Filehandle.write(tempstring)
x = uBound
tempstring = DecryptedText[(i+10):x]
Filehandle.write(tempstring)
i = i + 20
else:
tempstring = DecryptedText[i:(i+10)] + '\n'
Filehandle.write(tempstring)
i = i + 10
Filehandle.close
return 1
def ConvertToDictionary(KeyString):
KeyDict = {}
KeyDict[0] = KeyString[0:4]
KeyDict[1] = KeyString[4:8]
KeyDict[2] = KeyString[8:12]
KeyDict[3] = KeyString[12:16]
return KeyDict
def readfile(name):
#name= "text.txt"
filehandle = open ( name, 'r' )
str1 = filehandle.read()
filehandle.close
stringlist = list(str1)
count = 0
for y in stringlist:
x = ord(y)
if (x < 48 or x > 57) and (x < 65 or x > 90) and (x < 97 or x > 122):
stringlist[count] = ""
else:
if (x > 96 and x < 123):
stringlist[count] = stringlist[count].upper()
count = count + 1
str1 = "".join(stringlist)
return str1
print "Enter the drive that the USB Key is mounted on"
Mount = raw_input()
OptionList = ['Encrypt', 'Decrypt', 'Marry', 'Quit']
print "This is the Dulwich College Raspberry Pi Encryption software."
loop = 0
pquit = 0
while pquit == 0:
while loop == 0:
Choice = MainMenu(OptionList)
if Choice == 1:
print("You chose to encrypt")
print("Press 1 to confirm your choice")
tempchoice = raw_input()
tchoice = CheckIfInteger(tempchoice)
if tchoice == 1:
print ("Beginning encryption process")
print (" ")
print ("Before continuing, make sure you have placed the file in the same folder as the program")
print ("")
Filename = raw_input("Enter the name of the file, including the .txt extension: ")
Plaintext = readfile(Filename)
Key = GetKey(Mount)
NewKey = Encrypt.GenerateKey()
NewKey = ConvertKeyToString(NewKey)
StringToBeEncrypted = Plaintext + NewKey
Ciphertext = Encrypt.ProcessString(StringToBeEncrypted, Key)
FileWrite = "c" + Filename
FileWrite = "EncryptedFiles/" + FileWrite
EncryptionSuccess = Encrypt.PrintEncryption(Ciphertext, FileWrite)
FileWriteSuccesful = WriteNewKey(Mount, (str(NewKey)))
HashPS = Hasher.GetString(Ciphertext)
HashP = Hasher.HashString(HashPS)
HashWriteSuccessful = WriteHash(HashP, FileWrite)
print "Encryption Successful!"
loop = Continue()
else:
print(" ")
loop = 0
elif Choice == 2:
print("You chose to get decrypt")
print("Press 1 to confirm your choice")
tempchoice = raw_input()
tchoice = CheckIfInteger(tempchoice)
if tchoice == 1:
print ("Beginning decryption process")
print (" ")
print ("Before continuing, make sure you have placed the file in the 'EncryptedFiles' folder")
print ("")
CipherFileName = raw_input("Enter the name of the file, including the .txt extension: ")
CipherTempFile = CipherFileName
CipherFileName = "EncryptedFiles/" + CipherFileName
CipherHashList = ReadThisFile(CipherFileName)
CipherText = NumericCipherList(CipherHashList[0])
HashCS = Hasher.GetString(CipherText)
HashC = Hasher.HashString(HashCS)
if HashC == (CipherHashList[1]):
Key = GetKey(Mount)
Plaintext = Decryption.Decipher(CipherText, Key)
CipherLength = len(CipherText)
StringLength = (CipherLength/2)
if Plaintext[-1] == "Z":
NewKeyD = Plaintext[(StringLength-17):(StringLength-1)]
DecryptedText = Plaintext[0:(StringLength-17)]
else:
NewKeyD = Plaintext[(StringLength-16):StringLength]
DecryptedText = Plaintext[0:(StringLength-16)]
NewKeyD = NewKeyD.lower()
NewKeyDict = ConvertToDictionary(NewKeyD)
NewKeyS = ConvertKeyToString(NewKeyDict)
FileWriteDSuccesful = WriteNewKey(Mount, NewKeyS)
FileLocation = "EncryptedFiles/DECRYPTED" + CipherTempFile
PrintDecryption = PrintDecryptedText(DecryptedText, FileLocation)
print "Decryption Successful!"
loop = Continue()
else:
print "This messsage has been modified in some way!"
print "The message will be rejected"
loop = 1
else:
print(" ")
loop = 0
elif Choice == 3:
print("You chose to pair programs")
print("Press 1 to confirm your choice")
tempchoice = raw_input()
tchoice = CheckIfInteger(tempchoice)
if tchoice == 1:
print ("Beginning marrying process")
Key = Marry()
Key = ConvertKeyToString(Key)
FileWriteMSuccessful = WriteNewKey(Mount, Key)
loop = Continue()
else:
print(" ")
loop = 0
elif Choice == 4:
print("You chose to quit")
print("Press 1 to confirm your choice")
tempchoice = raw_input()
tchoice = CheckIfInteger(tempchoice)
if tchoice == 1:
print ("Quitting")
loop = 1
else:
print(" ")
loop = 0
else:
print("That option is not available, remember to pick the number not the word")
print(" ")