-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcipher.py
59 lines (50 loc) · 2.08 KB
/
cipher.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
import math
class CodeRing:
def __init__(self):
self.encoded = []
self.password = []
pass
def encodeChr(self, character):
return ord(character)
def decodeChr(self, integer):
return chr(integer)
def encodeLine(self, line):
encMSG = []
for index, char in enumerate(line):
intEncoded = self.encodeChr(char)
mutator = self.password[index % len(self.password)]
intEncoded = (
(intEncoded * intEncoded) + (mutator * mutator)
) / 89 # 89 is prime, makes the number smaller, can mutate some chars due to rounding though. Not an issue in most cases though...
encMSG.append(
intEncoded - 97
) # removing prime number to lower the integer values below 256
return [int(math.floor(flt)) for flt in encMSG]
def decodeLine(self, tupleEnc):
if type(tupleEnc) == list:
print("found list type")
tupleEnc = ",".join(str(x) for x in tupleEnc)
buff = []
if self.password == []:
return "could not decode wihout password"
for index, char in enumerate(tupleEnc.split(",")):
intEncoded = int(char) + 97 # adding back
intEncoded = intEncoded * 89
mutator = self.password[index % len(self.password)]
mutator = mutator * mutator
intDecoded = intEncoded - mutator
decoded = math.sqrt(intDecoded)
buff.append(
chr(int(decoded) + 1)
) # undoing the mutations seems to round everything down. Adding one takes care of that.
return "".join(buff)
def setPass(self, password):
self.password = [self.encodeChr(x) for x in password]
pass
if __name__ == "__main__":
cr = CodeRing()
cr.setPass("HWre0IXm0Ju ln6vial4!ik4xpqKdV7tOOd.ymXw7BcohwxWFXABogT,c8loMbPfWAs")
enctuple = cr.encodeLine("Alice, walk to the store and get me some milk, cheese, and anchovies.")
decstring = cr.decodeLine(enctuple)
print(enctuple)
print(decstring)