-
Notifications
You must be signed in to change notification settings - Fork 0
/
crypter.py
780 lines (752 loc) · 29.9 KB
/
crypter.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
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
## D&D Puzzle, Rewilding Campaign
## Initial draft: 2013.10.13, Philip Kahn
## https://github.com/tigerhawkvok/DnD-LLNS-CryptPuzzle
import math,re
class Message:
# Consider including numbers in map, newline.
mapping = {
'A':'1',
'B':'2',
'C':'3',
'D':'4',
'E':'5',
'F':'6',
'G':'7',
'H':'8',
'I':'9',
'J':'10',
'K':'11',
'L':'12',
'M':'13',
'N':'14',
'O':'15',
'P':'16',
'Q':'17',
'R':'18',
'S':'19',
'T':'20',
'U':'21',
'V':'22',
'W':'23',
'X':'24',
'Y':'25',
'Z':'26',
' ':'27',
'.':'28',
'?':'29',
'-':'30',
',':'31',
'0':'32',
'1':'33',
'2':'34',
'3':'35',
'4':'36',
'5':'37',
'6':'38',
'7':'39',
'8':'40',
'9':'41'
}
maplen=len(mapping)
regex_pattern="^[A-Za-z0-9 .?\-,]*$" # regular expression for valid character set
regex_hex="^[A-Fa-f0-9]*$"
def __init__(self,val=None):
# Initialization of class
# Do clipboard stuff
try:
from pyperclip import pyperclip
pyperclip.copy('Crytpo Puzzle')
p=pyperclip.paste()
except Exception as inst:
# If we're in Linux, we may need xclip installed
import os
try:
if os.uname()[0] == 'Linux':
import yn # From https://gist.github.com/tigerhawkvok/9542594
if not yn.yn("For clipboard functions, xclip is needed. Do you want to install now?"):
raise Exception
print('Attempting to install the package xclip ...')
os.system('sudo apt-get install xclip')
try:
from pyperclip import pyperclip
pyperclip.copy('Crytpo Puzzle')
p=pyperclip.paste()
except:
print('Could not automatically install xclip. Clipboard functions may not work correctly.')
else:
print('ERROR: Could not enable clipboard functions - ',inst)
except:
print('Clipboard functions may not work correctly on your setup')
try:
if os.uname()[0] == 'Linux':
print('Please make sure the package "xclip" is installed')
except: pass
self.key=None
try:
if val is None:
self.cipher=None
self.message=None
else:
# Message is intialized with any value
if not re.match(self.regex_hex,val):
self.cipher=None
if re.match(self.regex_pattern, val):
# Just valid characters
self.message=val.upper()
else:
# Bad input
print("Invalid message or ciphertext. Initializing empty object.")
self.message=None
else:
# Check padding/encoding
if len(val)%3 is 0 or len(val)%5 is 0:
self.cipher=val
self.message=None
else:
# All ciphertext characters are legitimate message characters.
# Probably a mistake, but make it clear to the user and don't assume.
print("Invalid ciphertext length. Initializing as a message.")
self.cipher=None
self.message=val
print("Ready.")
except Exception as inst:
print("UNEXPECTED ERROR: Could not initialize object -",inst)
def encode(self,key=None,message=None):
import re
try:
if key is None and self.key is None:
raise Exception("No encryption key provided.")
elif key is None and self.key is not None:
key=self.key
if not re.match(self.regex_pattern, key) and key is not None:
raise Exception("Bad encryption key")
else:
if key is not None:
self.key=key.upper()
if message is not None:
if re.match(self.regex_pattern, message):
# Just valid characters
self.message=message.upper()
else:
# Bad input
print("Invalid message or ciphertext, attempting to use stored value")
if self.message is not None:
factor=self.getFactor()
rm=self.rot()
mc=list(rm)
q=''
for c in mc:
num=self.mutateLetter(c,factor)
q+=num
# Deal with the resultant encode
self.cipher=q # Store in object
print(q) # Display
try:
from pyperclip import pyperclip
pyperclip.copy(q) # Copy to clipboard
except Exception:
print("The value could not be copied to the clipboard.")
else:
raise Exception("No valid message to encrypt")
except Exception as inst:
print("ERROR:", inst)
return None
def decode(self,key=None,cipher=None):
import re
try:
if key is None and self.key is None:
raise Exception("No decryption key provided.")
elif key is None and self.key is not None:
key=self.key
if not re.match(self.regex_pattern, key) and key is not None:
raise Exception("Bad decryption key")
else:
if key is not None:
self.key=key.upper()
if cipher is not None and not re.match(self.regex_pattern,key):
if len(cipher)%5 is 0:
self.cipher=cipher.upper()
else:
print("Invalid ciphertext length. Attempting to use stored value")
elif cipher is not None and not cipher.isdigit():
print("Invalid ciphertext. Attempting to use stored value")
if self.cipher is not None:
# Decrypt
factor=self.getFactor()
# Bucket the characters in 5
cl=list(self.cipher)
cb=list()
i=1
t=''
for l in cl:
t+=str(l)
if i%5 is 0:
cb.append(t)
t=''
i+=1
q=''
for c in cb:
q+=self.mutateLetter(c,factor,False) # rotated inside here
self.message=q
print(q)
try:
from pyperclip import pyperclip
pyperclip.copy(q) # Copy to clipboard
except Exception:
print("The value could not be copied to the clipboard.")
else:
raise Exception("No valid ciphertext to decrypt")
except Exception as inst:
print("ERROR:", inst)
return None
def getFactor(self):
# Work with the key
try:
if self.key is None:
raise Exception("No encryption key has been set. Run encode(), decode(), or setKey() with an encryption key first")
import math
chars=list(self.key)
nums=list()
n=range(len(chars)) # The iterator
for i,letter in enumerate(chars):
letterVal = self.mapping[letter]
letterVal = int(letterVal) * (len(chars)-i+1)^2
nums.append(letterVal)
# Do the math
numerator=0 # summation holder
k=0 # second summation holder
for i in n:
l=i+1
# Difficulty iteration - l+=(l^nums[i]) to shift it by bitwise XOR
# Difficulty iteration - if maplen-l is positive, multiply by it ( weights in other direction)
numerator+=int(nums[i])*l
# k+=int(nums[i]) # Difficulty iteration - fold the sum in based on even/odd?
factor=numerator
return factor
except Exception as inst:
print("ERROR:",inst)
return None
def rot(self,m=None,n=None):
# Rotate a message 'm' by 'n'
try:
if n is None or str(abs(int(n))).isdigit() is False:
if m is not None:
try:
if str(abs(int(m))).isdigit() is True:
n=m
except TypeError:
# abs or something didn't work
pass
else:
try:
n=len(self.key)
except TypeError:
# the key hasn't been defined
raise Exception("No key has been defined yet, and no rotation size has been provided.")
else: n=int(n) # just in case
except Exception as inst:
print("ERROR: Invalid Rotation Length - ",inst);
return None
try:
if m is not None:
import re
if re.match(self.regex_pattern, m):
# Just valid characters
m=m.upper()
else:
raise Exception("Invalid message text")
else:
m=self.message
chars=list(m)
rotated=''
toSym=dict([reversed(i) for i in self.mapping.items()])
#self.mapping.update(toSym)
for letter in chars:
new_num=(int(self.mapping[letter])+n)%self.maplen
if new_num is 0: new_num=self.maplen
new_letter=toSym[str(new_num)]
rotated+=new_letter
return rotated
except Exception as inst:
print("UNEXPECTED ROTATION ERROR:",inst)
return None
def mutateLetter(self,letter,factor=None,forward=True):
# Take a letter, and mutate it to the 5-character version
## Difficulty iteration -- have the rotation be based on character position in string (defeat frequency)
## Difficulty iteration -- round to 1 dec, if %1!=0, double and mod maplen (close matches aren't)
try:
if factor is None:
factor=self.getFactor()
if factor is None:
raise Exception('No factor could be obtained.')
if forward:
# Take a letter, get its map, multiply by factor, divide by prime, pad with significand in hex
# find the biggest prime less than the factor
letter=letter.upper()
try:
numeric_rep=int(self.mapping[letter])
except KeyError:
raise Exception("Invalid message character '"+letter+"'")
if str(factor).isdigit() is False:
raise Exception("Bad factor")
hp=self.getHighestPrime(factor)
composite=numeric_rep*factor
import math
significand=math.floor(composite/hp)
cipher=composite%hp
#print('encoding',letter,significand,cipher)
hsig=hex(significand).split('x')[1]
hcip=hex(cipher).split('x')[1]
spad=2-len(hsig)
cpad=3-len(hcip)
if cpad is 1: hcip='0'+hcip
elif cpad is 2: hcip='00'+hcip
if spad is 1: hsig='0'+hsig
mutated=hsig+hcip
return mutated.upper()
else:
# undo it!
import re
if not re.match(self.regex_hex,letter) or len(letter)%5 is not 0:
raise Exception('Invalid cipher character')
# checked the input, contiue
significand=int(letter[:2],16)
cipher=int(letter[2:],16)
hp=self.getHighestPrime(factor)
resid=cipher/hp
frac=significand+resid
composite=frac*hp
origmap=str(round(composite/factor)) # rounding errors
# Now take this digit and remap it to the base characters
toSym=dict([reversed(i) for i in self.mapping.items()])
try:
q=toSym[origmap]
dc=self.rot(q,'-'+str(len(self.key)))
except KeyError:
dc=''
#print('got',dc,q,'with',factor,hp,letter,frac,composite/factor,'from',origmap)
return dc
except Exception as inst:
print("UNEXPECTED MUTATION ERROR:",inst)
return None
def getHighestPrime(self,s=None):
try:
if s is None:
if self.getFactor() is None:
raise Exception("No argument provided and no key is set")
else:
s=self.getFactor()
c1=list()
ref=list()
r=range(2,s) # start at 2, lowest prime
for n in r:
if n%2 is not 0:
c1.append(n)
c1.reverse()
ref=c1
hprime=s
for n in ref:
isPrime=True # default to true, break out if not
for n2 in c1:
if n2 < n:
# only check smaller values
if (n/n2)%1 == 0:
isPrime=False
break
if isPrime:
hprime=n
break
return hprime
except Exception as inst:
print("UNEXPECTED PRIME FINDING ERROR:",inst)
return None
def setKey(self,key):
# Manually set the encryption key
import re
try:
if key is None:
raise Exception("No encryption key provided.")
elif not re.match(self.regex_pattern, key) and key is not None:
raise Exception("Bad encryption key")
else:
self.key=key.upper()
except Exception as inst:
print("ERROR:",inst)
return None
def setMessage(self,message):
# Manually set the message
try:
if message is not None:
import re
if re.match(self.regex_pattern, message):
# Just valid characters
self.message=message.upper()
else:
raise Exception("Invalid message text")
else:
raise Exception("Message is empty")
except Exception as inst:
print("ERROR:",inst)
return None
def setCipher(self,cipher):
# Manually set the cipher
try:
if cipher is not None:
#check if it's a hex
import re
if cipher.isdigit():
# This has to be first, hex is a subset
if len(cipher)%3 is 0:
self.cipher=cipher.upper()
else:
raise Exception("Invalid ciphertext length (dec).")
elif re.match(self.regex_hex, cipher):
if len(cipher)%5 is 0:
self.cipher=cipher.upper()
else:
raise Exception("Invalid ciphertext length (hex).")
else:
raise Exception("Invalid ciphertext. Ciphertext can only have hex numbers (0-9, A-F).")
else:
raise Exception("Cipher is empty")
except Exception as inst:
print("ERROR:",inst)
return None
def export(self):
# Export the ciphertext to /raw
try:
if self.cipher is None:
if self.message is None:
raise Exception("No message or ciphertext")
elif self.key is None:
raise Exception("No key set to encode message")
else:
self.encode()
try:
string=raw_input("Please enter the exported Note ID")
except NameError:
string=input("Please enter the exported Note ID")
if string == "":
raise Exception("No Note ID provided")
try:
float(string)
except ValueError:
raise Exception("Invalid Note ID")
fname = "raw/"+string
f = open(fname,'w')
f.write(self.cipher)
f.close()
print("Ciphertext written to",fname)
except Exception as inst:
print("EXPORT ERROR: Could not export ciphertext - ",inst)
def generate(self):
# Generate HTML based on the images from the various puzzles to make an in-universe example
# Hex will be 0-6, then the first ten phonetic characters to represent base 16
try:
if self.cipher is None:
if self.message is None:
raise Exception("No message or ciphertext")
elif self.key is None:
raise Exception("No key set to encode message")
else:
self.encode()
else:
print("Using stored ciphertext - if this isn't intentional, run encode() then run this again.")
# Do a map of character to files
import time
fname = "generated/generated_output-"+str(int(time.time()))+".xht"
f = open(fname,'w')
output = "<?xml version='1.0' encoding='utf-8' ?>\n<!DOCTYPE html>\n<html xmlns='http://www.w3.org/1999/xhtml' xml:lang='en'>\n\t<head>\n\t\t<title></title>\n\t\t<style type='text/css'>\n\t\t\timg { display:inline-block; max-width:1.5em;}\n\t\t</style>\n\t</head>\n\t<body>\n\t\t<!--Begin Ciphertext-->"
for char in self.cipher:
# Take each char, and map it onto an img resource
x = int(char,16)
html = "\n\t\t<img src='assets/"+str(x)+".png'/>"
output+= html
output += "\n\t\t<!--End Ciphertext--><!-- DM Solution: \""+self.message+"\" with key \""+self.key+"\"-->\n\t</body>\n</html>"
f.write(output)
f.close()
print("Output written to",fname)
self.export()
except Exception as inst:
print("GENERATE ERROR:", inst)
return None
## Alternate definitions
def setcipher(self,*args):
self.setCipher(*args)
def setmessage(self,*args):
self.setMessage(*args)
def setText(self,*args):
self.setMessage(*args)
def settext(self,*args):
self.setMessage(*args)
def setkey(self,*args):
self.setKey(*args)
##################################
# Old Versions for continued use #
##################################
mappingV1 = {
'A':'1',
'B':'2',
'C':'3',
'D':'4',
'E':'5',
'F':'6',
'G':'7',
'H':'8',
'I':'9',
'J':'10',
'K':'11',
'L':'12',
'M':'13',
'N':'14',
'O':'15',
'P':'16',
'Q':'17',
'R':'18',
'S':'19',
'T':'20',
'U':'21',
'V':'22',
'W':'23',
'X':'24',
'Y':'25',
'Z':'26',
' ':'27',
'.':'28',
'?':'29'
}
maplenV1=len(mappingV1)
def getFactorV1(self):
# Work with the key
try:
if self.key is None:
raise Exception("No encryption key has been set. Run encode(), decode(), or setKey() with an encryption key first")
import math
chars=list(self.key)
nums=list()
n=range(len(chars)) # The iterator
for letter in chars:
nums.append(self.mappingV1[letter])
# Do the math
numerator=0 # summation holder
k=0 # second summation holder
for i in n:
l=i+1
numerator+=int(nums[i])*l
k+=int(nums[i])
exp=math.floor(math.log(k*len(chars),10))
denominator=math.pow(10,exp)
factor=numerator/denominator
return factor
except Exception as inst:
print("ERROR:",inst)
return None
def getFactorV2(self):
# Work with the key
try:
if self.key is None:
raise Exception("No encryption key has been set. Run encode(), decode(), or setKey() with an encryption key first")
import math
chars=list(self.key)
nums=list()
n=range(len(chars)) # The iterator
for i,letter in enumerate(chars):
letterVal = self.mapping[letter]
nums.append(letterVal)
# Do the math
numerator=0 # summation holder
k=0 # second summation holder
for i in n:
l=i+1
numerator+=int(nums[i])*l
factor=numerator
return factor
except Exception as inst:
print("ERROR:",inst)
return None
def decodeV1(self,key=None,cipher=None):
import re
try:
if key is None and self.key is None:
raise Exception("No decryption key provided.")
elif key is None and self.key is not None:
key=self.key
if not re.match(self.regex_pattern, key) and key is not None:
raise Exception("Bad decryption key")
else:
if key is not None:
self.key=key.upper()
if cipher is not None and cipher.isdigit():
if len(cipher)%3 is 0:
self.cipher=cipher.upper()
else:
print("Invalid ciphertext length. Attempting to use stored value")
elif cipher is not None and not cipher.isdigit():
print("Invalid ciphertext. Attempting to use stored value")
if self.cipher is not None:
# Decrypt
factor=self.getFactorV1()
# Bucket the characters in 3
cl=list(self.cipher)
cb=list()
i=1
t=''
for l in cl:
t+=str(l)
if i%3 is 0:
cb.append(t)
t=''
i+=1
q=''
toSym=dict([reversed(i) for i in self.mappingV1.items()])
self.mappingV1.update(toSym)
for c in cb:
try:
letter=self.mappingV1[str(int(math.ceil(int(c)/factor)))]
except KeyError:
# Replace a 'bad' map from a bad key with a blank
letter=''
q+=letter
uq=self.rotV1(q,'-'+str(len(self.key)))
print(uq)
self.message=uq
else:
raise Exception("No valid ciphertext to decrypt")
except Exception as inst:
print("ERROR:", inst)
return None
def decodeV2(self,key=None,cipher=None):
import re
try:
if key is None and self.key is None:
raise Exception("No decryption key provided.")
elif key is None and self.key is not None:
key=self.key
if not re.match(self.regex_pattern, key) and key is not None:
raise Exception("Bad decryption key")
else:
if key is not None:
self.key=key.upper()
if cipher is not None and cipher.isdigit():
if len(cipher)%3 is 0:
self.cipher=cipher.upper()
else:
print("Invalid ciphertext length. Attempting to use stored value")
elif cipher is not None and not cipher.isdigit():
print("Invalid ciphertext. Attempting to use stored value")
if self.cipher is not None:
# Decrypt
factor=self.getFactorV2()
# Bucket the characters in 3
cl=list(self.cipher)
cb=list()
i=1
t=''
for l in cl:
t+=str(l)
if i%3 is 0:
cb.append(t)
t=''
i+=1
q=''
toSym=dict([reversed(i) for i in self.mappingV1.items()])
self.mappingV1.update(toSym)
for c in cb:
try:
letter=self.mappingV1[str(int(math.ceil(int(c)/factor)))]
except KeyError:
# Replace a 'bad' map from a bad key with a blank
letter=''
q+=letter
uq=self.rotV1(q,'-'+str(len(self.key)))
print(uq)
self.message=uq
else:
raise Exception("No valid ciphertext to decrypt")
except Exception as inst:
print("ERROR:", inst)
return None
def rotV1(self,m=None,n=None):
# Rotate a message 'm' by 'n'
try:
if n is None or str(abs(int(n))).isdigit() is False:
if m is not None:
try:
if str(abs(int(m))).isdigit() is True:
n=m
except TypeError:
# abs or something didn't work
pass
else:
try:
n=len(self.key)
except TypeError:
# the key hasn't been defined
raise Exception("No key has been defined yet, and no rotation size has been provided.")
else: n=int(n) # just in case
except Exception as inst:
print("ERROR: Invalid Rotation Length - ",inst);
return None
try:
if m is not None:
import re
if re.match(self.regex_pattern, m):
# Just valid characters
m=m.upper()
else:
raise Exception("Invalid message text")
else:
m=self.message
chars=list(m)
rotated=''
toSym=dict([reversed(i) for i in self.mappingV1.items()])
self.mappingV1.update(toSym)
for letter in chars:
new_num=(int(self.mappingV1[letter])+n)%self.maplenV1
if new_num is 0: new_num=self.maplenV1
new_letter=self.mappingV1[str(new_num)]
rotated+=new_letter
return rotated
except Exception as inst:
print("UNEXPECTED ERROR:",inst)
return None
def encodeV1(self,key=None,message=None):
import re
try:
if key is None and self.key is None:
raise Exception("No encryption key provided.")
elif key is None and self.key is not None:
key=self.key
if not re.match("^[A-Za-z .?]*$", key) and key is not None:
raise Exception("Bad encryption key")
else:
if key is not None:
self.key=key.upper()
if message is not None:
if re.match(self.regex_pattern, message):
# Just valid characters
self.message=message.upper()
else:
# Bad input
print("Invalid message or ciphertext, attempting to use stored value")
if self.message is not None:
factor=self.getFactorV1()
rm=self.rotV1()
mc=list(rm) # self.message)
q=''
for c in mc:
cmap=int(self.mappingV1[c]) # Consider offseting by either message length or key length
num=str(int(math.floor(cmap*factor)))
if len(num) is not 3:
if len(num)==1:
num='00'+num
else:
num='0'+num
q+=num
self.cipher=q
return q
else:
raise Exception("No valid message to encrypt")
except Exception as inst:
print("ERROR:", inst)
return None