-
Notifications
You must be signed in to change notification settings - Fork 3
/
mc_core.py
575 lines (463 loc) · 13.5 KB
/
mc_core.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
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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
# Core Library Utilities
import numpy as np
import pickle
import os.path
import random
import csv
###### Helper Functions #######
def genSMatrix(k):
"""Generates an invertable matrix k * k"""
sMaybe = np.matrix(np.random.randint(0,2,k*k).reshape(k,k).astype(int))
while True:
try:
sMaybe.getI()
return sMaybe
except:
sMaybe = np.matrix(np.random.randint(0,2,k*k).reshape(k,k).astype(int))
def genPMatrix(n,keep=False):
"""Generates a permutation matrix n x n using the given sequence"""
p = np.identity(n, dtype=int)
if keep:
return np.matrix(p).reshape(n,n)
else:
return np.matrix(np.random.permutation(p))
def modTwo(C):
"""Q & D way to Mod 2 all results"""
D = C.copy()
D.fill(2)
return np.remainder(C,D)
def bitFlip(C,n):
"""Flips the bit you tell it, -1 for random bit flip, 0 for no flip"""
if n == 0:
return C
if n == -1:
index = random.randint(1,C.size -1)
else:
index = n
if C[0,index-1] == 1:
C[0,index-1] = 0
else:
C[0,index-1] = 1
return C
def all_zeros(d):
"""Used when looking up a Syndrome, all zeros mean no error"""
zc = 0
for x in d:
if x == 0:
zc += 1
if zc == len(d):
return True
else:
return False
def syndromeLookup(H,d):
"""Looks up where in the syndrome table, if not found must be codeword, in that case returns 0"""
t = H.T.tolist()
s = d.T.tolist()[0]
#print "s s\t",s
if all_zeros(s):
#print "s i\t 0"
return 0
try:
#print "s i\t ",t.index(s) + 1
return t.index(s) + 1
except:
#print "s i\t 0"
return 0
def checkOldGuesses(oG,newGuess):
"""Helper function to see if a guessed matrix has been used before"""
for s in oG:
if np.array_equal(newGuess.A1,s.A1):
return False
else:
return True
def makeString(matrix):
"""Used in DNA file encoding, will make Numpy Matrix into text strings for easier manipulation"""
message = ""
for m in matrix.A1:
if m == "[":
pass
elif m == "]":
pass
elif m == " ":
pass
else:
message += str(m)
return message
#### Hamming 8,4 Encryption Data Structures ######
#### Private Key H84 ####
class privateKeyH84:
"""Datastructure to represent our Private Key"""
def __init__(self,S=None,P=None):
"""Initalizer that will set S & P matricies to random if not given values"""
#Hamming 8,4 in standard
self.G = np.matrix([
[1,0,0,0,0,1,1,1],
[0,1,0,0,1,0,1,1],
[0,0,1,0,1,1,0,1],
[0,0,0,1,1,1,1,0]
], dtype=int)
self.H = np.matrix([
[0,1,1,1,1,0,0,0],
[1,0,1,1,0,1,0,0],
[1,1,0,1,0,0,1,0],
[1,1,1,0,0,0,0,1]
], dtype=int)
#Can create these from known values, otherwise random
if S == None:
self.S = modTwo(genSMatrix(4))
else:
self.S = S
if P == None:
self.P = modTwo(genPMatrix(8))
else:
self.P = P
def printCode(self):
"""Canonical print to screen function"""
print "S: \n" + str(self.S) + "\n"
print "P: \n" + str(self.P) + "\n"
print "GPrime: \n" + str(self.makeGPrime()) + "\n"
def writeKeyToFile(self, keyFile):
"""Saves key to a pickle file"""
try:
pickle.dump(self, open(keyFile,"wb"))
except:
print "Could not save key file to: ",keyFile
exit(1)
def readKeyFromFile(self,keyFile):
"""Reads key from a pickle file"""
try:
newPriv = pickle.load( open(keyFile,"rb"))
self.S = newPriv.S
self.P = newPriv.P
except:
print "Could not load key file from: ",keyFile
exit(1)
def makeGPrime(self):
"""Creates the GPrime encrytion Matrix"""
return modTwo(self.S*self.G*self.P)
def decrypt(self,c):
"""When given cipher text will decode to message"""
cHat = c * modTwo(self.P.I.astype(int))
m = bitFlip(cHat,syndromeLookup(self.H,modTwo(self.H*cHat.T)))
return modTwo(m[0,0:4] * modTwo(self.S.I.astype(int)))
def decryptFile(self,f):
"""Will decrypt whole file"""
cf = open(f,"rb")
cb1 = cf.read(1)
cb2 = cf.read(1)
mf = open(f+".decoded","wb")
while cb1 and cb2:
#First Byte of Cipher Text
c_1 = '{0:08b}'.format(ord(cb1))[0:8]
c1_l = []
m1 = ""
for s in c_1:
c1_l.append(s)
c_1_m = np.matrix(c1_l,dtype=int)
d1 = self.decrypt(c_1_m)
for d in range(0,d1.size):
m1 += str(d1.item(d))
#Second Byte of Cipher Text
c_2 = '{0:08b}'.format(ord(cb2))[0:8]
c2_l = []
m2 = ""
for s in c_2:
c2_l.append(s)
c_2_m = np.matrix(c2_l,dtype=int)
d2 = self.decrypt(c_2_m)
for d in range(0,d2.size):
m2 += str(d2.item(d))
#print m1+m2
mf.write(chr(int(m1+m2,2)))
cb1 = cf.read(1)
cb2 = cf.read(1)
mf.close()
cf.close()
def dnaFileDecrypt(self,f,dlu):
"""Decrypts a file that has been turned into a DNA representation"""
cf = open(f,"r")
c1 = cf.readline().strip("\n")
c2 = cf.readline().strip("\n")
mf = open(f+".decoded","w")
while c1 and c2:
m1 = ""
m2 = ""
mat1 = np.matrix(" ".join(dlu.lookDNADecrypt(c1)),dtype=int)
mat2 = np.matrix(" ".join(dlu.lookDNADecrypt(c2)),dtype=int)
d1 = self.decrypt(mat1)
d2 = self.decrypt(mat2)
for d in range(0,d1.size):
m1 += str(d1.item(d))
for d in range(0,d2.size):
m2 += str(d2.item(d))
mf.write(chr(int(m1+m2,2)))
c1 = cf.readline().strip("\n")
c2 = cf.readline().strip("\n")
mf.close()
cf.close()
#### Public Key H84 ####
class publicKeyH84:
"""Public Key Data Structure"""
def __init__(self,GPrime):
self.GPrime = GPrime
def printCode(self):
"""Canonical print to screen"""
print "GPrime: \n" + str(self.GPrime) + "\n"
def writeKeyToFile(self, keyFile):
"""Saves key to a pickle file"""
try:
pickle.dump(self, open(keyFile,"wb"))
except:
print "Could not save key file to: ",keyFile
exit(1)
def readKeyFromFile(self,keyFile):
"""Reads key from a pickle file"""
try:
newPub = pickle.load( open(keyFile,"rb"))
self.GPrime = newPub.GPrime
except:
print "Could not load key file from: ",keyFile
exit(1)
def encrypt(self,m):
"""When given a message will encode"""
#Error vector will be random
z = random.randint(1,7)
c = bitFlip(modTwo(m*self.GPrime),z)
return c
def encryptFile(self,f):
"""Encrypts a whole file"""
mf = open(f,"rb")
m = mf.read(1)
cf = open(f+".ctxt","wb")
while m:
#First half byte of message text
m_1 = '{0:08b}'.format(ord(m))[0:4]
m1_l = []
c1 = ""
for s in m_1:
m1_l.append(s)
m_1_m = np.matrix(m1_l,dtype=int)
d1 = self.encrypt(m_1_m)
for d in range(0,d1.size):
c1 += str(d1.item(d))
cf.write(chr(int(c1,2)))
#Second half byte of message text
m_2 = '{0:08b}'.format(ord(m))[4:]
m2_l = []
c2 = ""
for s in m_2:
m2_l.append(s)
m_2_m = np.matrix(m2_l,dtype=int)
d2 = self.encrypt(m_2_m)
for d in range(0,d2.size):
c2 += str(d2.item(d))
cf.write(chr(int(c2,2)))
m = mf.read(1)
cf.close()
mf.close()
def dnaFileEncrypt(self,f,dlu):
"""Takes a files, encypts it and represents it as a DNA codeword"""
cipherString = ""
mf = open(f,"rb")
m = mf.read(1)
cf = open(f+".dna.ctxt","wb")
while m:
#First half byte of message text
m_1 = '{0:08b}'.format(ord(m))[0:4]
m1_l = []
c1 = ""
for s in m_1:
m1_l.append(s)
m_1_m = np.matrix(m1_l,dtype=int)
d1 = self.encrypt(m_1_m)
#Second half byte of message text
m_2 = '{0:08b}'.format(ord(m))[4:]
m2_l = []
c2 = ""
for s in m_2:
m2_l.append(s)
m_2_m = np.matrix(m2_l,dtype=int)
d2 = self.encrypt(m_2_m)
message = makeString(m_1_m) + makeString(m_2_m)
cipherMessage = makeString(d1) + makeString(d2)
cf.write(dlu.lookDNAEncrypt(cipherMessage[0:8])+"\n")
cf.write(dlu.lookDNAEncrypt(cipherMessage[8:])+"\n")
m = mf.read(1)
cf.close()
mf.close()
#### Brute Forcer ####
class bruteForcerH84():
"""Data structure that attempts to create Private Key from Given Public Key"""
def __init__(self,GPrime):
self.attempts = 0
self.GPrime = GPrime
self.GPrimeConsider = 0
self.sConsider = 0
self.pConsider = 0
self.STries = list()
self.PTries = list()
self.G = np.matrix([
[1,0,0,0,0,1,1,1],
[0,1,0,0,1,0,1,1],
[0,0,1,0,1,1,0,1],
[0,0,0,1,1,1,1,0]
], dtype=int)
def printCode(self):
"""Canonical Print self function"""
print "Calculated GPrime:\n" + str(self.GPrimeConsider) + "\n"
print "sM:\n" + str(self.sConsider) + "\n"
print "pM:\n" + str(self.pConsider) + "\n"
print "Attempts: " + str(self.attempts) + "\n"
def attemptKey(self):
"""Attempts to reconstitute S,P given GPrime (G is already known)"""
self.attempts = 1
self.sConsider = genSMatrix(4)
self.STries.append(self.sConsider)
self.pConsider = genPMatrix(8)
self.PTries.append(self.pConsider)
self.GPrimeConsider = modTwo(self.sConsider*self.G*self.pConsider)
while not np.array_equal(self.GPrimeConsider.A1,self.GPrime.A1):
self.attempts += 1
#Keep generating new S matrices to test
self.sConsider = genSMatrix(4)
while not checkOldGuesses(self.STries,self.sConsider):
self.sConsider = genSMatrix(4)
self.STries.append(self.sConsider)
#Keep generating new P matrices to test
self.pConsider = genPMatrix(8)
while not checkOldGuesses(self.PTries,self.pConsider):
self.pConsider = genPMatrix(8)
self.PTries.append(self.pConsider)
self.GPrimeConsider = modTwo(self.sConsider*self.G*self.pConsider)
return True
#### Hamming 16,11 Version ######
#### Created to test security of Key Cracking ####
#### Private Key H1611 ####
class privateKeyH1611:
"""Datastructure to represent our Private Key"""
def __init__(self,S=None,P=None):
"""Initalizer that will set S,P, random if not given values"""
#Hamming 16,11 in standard
self.G = np.matrix([
[1,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1],
[0,1,0,0,0,0,0,0,0,0,0,0,1,0,1,1],
[0,0,1,0,0,0,0,0,0,0,0,0,1,1,0,1],
[0,0,0,1,0,0,0,0,0,0,1,0,0,0,1,1],
[0,0,0,0,1,0,0,0,0,0,1,0,0,1,0,1],
[0,0,0,0,0,1,0,0,0,0,1,0,1,0,0,1],
[0,0,0,0,0,0,1,0,0,0,1,0,1,1,1,1],
[0,0,0,0,0,0,0,1,0,0,1,0,1,1,0,0],
[0,0,0,0,0,0,0,0,1,0,1,0,1,0,1,0],
[0,0,0,0,0,0,0,0,0,1,1,0,0,1,1,0],
[0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,0]
], dtype=int)
self.H = np.matrix([
[1,0,0,0,0,1,1,0,0,1,1,1,1,0,0,1],
[0,1,0,0,1,0,1,0,1,0,1,1,0,1,0,1],
[0,0,1,0,1,1,0,0,1,1,0,1,0,0,1,1],
[0,0,0,1,1,1,1,0,0,0,0,1,1,1,1,0],
[0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,0]
], dtype=int)
#Can create these from known values, otherwise random
if S == None:
self.S = modTwo(genSMatrix(11))
else:
self.S = S
if P == None:
self.P = modTwo(genPMatrix(16))
else:
self.P = P
def printCode(self):
"""Canonical print to screen function"""
print "S: \n" + str(self.S) + "\n"
print "P: \n" + str(self.P) + "\n"
print "GPrime: \n" + str(self.makeGPrime()) + "\n"
def makeGPrime(self):
"""Returns a version of GPrime usable for calculations"""
return modTwo(self.S*self.G*self.P)
#### Public Key H1611 ####
class publicKeyH1611:
"""Public Key Data Structure, much simplified then H8,4"""
def __init__(self,GPrime):
self.GPrime = GPrime
def printCode(self):
"""Canonical print to screen"""
print "GPrime: \n" + str(self.GPrime) + "\n"
##### Brute Forcer Based on H16,11 ####
class bruteForcerH1611():
"""Data structure that attempts to create Private Key from Given Public Key"""
def __init__(self,GPrime):
self.attempts = 0
self.GPrime = GPrime
self.GPrimeConsider = 0
self.sConsider = 0
self.pConsider = 0
self.STries = list()
self.PTries = list()
self.G = np.matrix([
[1,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1],
[0,1,0,0,0,0,0,0,0,0,0,0,1,0,1,1],
[0,0,1,0,0,0,0,0,0,0,0,0,1,1,0,1],
[0,0,0,1,0,0,0,0,0,0,1,0,0,0,1,1],
[0,0,0,0,1,0,0,0,0,0,1,0,0,1,0,1],
[0,0,0,0,0,1,0,0,0,0,1,0,1,0,0,1],
[0,0,0,0,0,0,1,0,0,0,1,0,1,1,1,1],
[0,0,0,0,0,0,0,1,0,0,1,0,1,1,0,0],
[0,0,0,0,0,0,0,0,1,0,1,0,1,0,1,0],
[0,0,0,0,0,0,0,0,0,1,1,0,0,1,1,0],
[0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,0]
], dtype=int)
def printCode(self):
"""Canonical Print self function"""
print "Calculated GPrime:\n" + str(self.GPrimeConsider) + "\n"
print "sM:\n" + str(self.sConsider) + "\n"
print "pM:\n" + str(self.pConsider) + "\n"
print "Attempts: " + str(self.attempts) + "\n"
def attemptKey(self):
"""Attempts to reconstitute S,P given GPrime (G is already known)"""
self.attempts = 1
self.sConsider = genSMatrix(11)
self.STries.append(self.sConsider)
self.pConsider = genPMatrix(16)
self.PTries.append(self.pConsider)
self.GPrimeConsider = modTwo(self.sConsider*self.G*self.pConsider)
while not np.array_equal(self.GPrimeConsider.A1,self.GPrime.A1):
self.attempts += 1
self.sConsider = genSMatrix(11)
while not checkOldGuesses(self.STries,self.sConsider):
self.sConsider = genSMatrix(11)
self.STries.append(self.sConsider)
self.pConsider = genPMatrix(16)
while not checkOldGuesses(self.PTries,self.pConsider):
self.pConsider = genPMatrix(16)
self.PTries.append(self.pConsider)
self.GPrimeConsider = modTwo(self.sConsider*self.G*self.pConsider)
return True
##### DNA Lookup Datastructure ###
class lookupDNA():
"""Data structure that keeps a table of DNA mappings"""
def __init__(self,encryptFile,decryptFile):
self.encLU = dict()
self.decLU = dict()
dreader = csv.reader(open(decryptFile,'r'))
for row in dreader:
k,v = row
self.decLU[k] = str(v)
ereader = csv.reader(open(encryptFile,'r'))
for row in ereader:
k,v = row
self.encLU[k] = str(v)
def lookDNAEncrypt(self, bstring):
"""Finds DNA sequence from 8 bits"""
try:
return str(self.encLU[bstring])
except:
return "?"
def lookDNADecrypt(self,bstring):
"""Finds 8 bits from 8 characters of DNA"""
try:
return str(self.decLU[bstring])
except:
return "?"
if __name__ == "__main__":
pass