-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEncryption.py
More file actions
96 lines (90 loc) · 3.74 KB
/
Encryption.py
File metadata and controls
96 lines (90 loc) · 3.74 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
import secrets
import struct
import generateCMT as gCMT
keyLength = 16
width = 5
numberOfSectorsAcrossX = 10
numberOfSectorsAcrossY = 10
totalChars = 256
def encrypt(character, CMT):
totalCharacters = CMT['config']['totalChars']
FPPB = CMT['config']['FPPB']
integerX = secrets.choice(list(CMT['data'].keys()))
integerY = secrets.choice(list(CMT['data'][0].keys()))
floatingX = 0
floatingY = 0
for i in range(CMT['config']['FPPDigits'] - 1):
floatingX = (10 * floatingX) + secrets.randbelow(9)
floatingY = (10 * floatingY) + secrets.randbelow(9)
xmin = integerX - (integerX % CMT['config']['width'])
ymin = integerY - (integerY % CMT['config']['width'])
key = CMT['data'][xmin][ymin]
# print(character)
asciiChar = ord(str(character))
xoredFloatingXYCord = (((floatingX << FPPB) + floatingY)^key)
beta = xoredFloatingXYCord % totalCharacters
if (beta > asciiChar):
modification = totalCharacters - beta + asciiChar
else:
modification = asciiChar - beta
encryptedFloatingXYCord = xoredFloatingXYCord + modification
newXYCord = encryptedFloatingXYCord ^ key
encryptedFloatingX = newXYCord >> FPPB
encryptedFloatingY = newXYCord & ((2**FPPB)-1)
encryptedFloatingX = float(encryptedFloatingX / CMT['config']['FPPDecimal'])
encryptedFloatingY = float(encryptedFloatingY / CMT['config']['FPPDecimal'])
encryptedX, encryptedY = float(integerX + encryptedFloatingX), float(integerY + encryptedFloatingY)
return(encryptedX, encryptedY)
# CMT = gCMT.generateCodeMapTable(keyLength,width,numberOfSectorsAcrossX,numberOfSectorsAcrossY,totalChars)
# for i in range(10000):
# x,y,key = encrypt('K',CMT)
# print(x,y,key)
# if (decrypt(x,y,CMT)) != 'K':
# print("Decrypt: x, y,", x, y, decrypt(x,y,CMT))
def encryptByte(byte, CMT):
totalCharacters = 256
FPPB = CMT['config']['FPPB']
integerX = secrets.choice(list(CMT['data'].keys()))
integerY = secrets.choice(list(CMT['data'][0].keys()))
floatingX = 0
floatingY = 0
for i in range(CMT['config']['FPPDigits'] - 1):
floatingX = (10 * floatingX) + secrets.randbelow(9)
floatingY = (10 * floatingY) + secrets.randbelow(9)
xmin = integerX - (integerX % CMT['config']['width'])
ymin = integerY - (integerY % CMT['config']['width'])
key = CMT['data'][xmin][ymin]
# print(character)
asciiChar = byte
xoredFloatingXYCord = (((floatingX << FPPB) + floatingY)^key)
beta = xoredFloatingXYCord % totalCharacters
if (beta > asciiChar):
modification = totalCharacters - beta + asciiChar
else:
modification = asciiChar - beta
encryptedFloatingXYCord = xoredFloatingXYCord + modification
newXYCord = encryptedFloatingXYCord ^ key
encryptedFloatingX = newXYCord >> FPPB
encryptedFloatingY = newXYCord & ((2**FPPB)-1)
encryptedFloatingX = float(encryptedFloatingX / CMT['config']['FPPDecimal'])
encryptedFloatingY = float(encryptedFloatingY / CMT['config']['FPPDecimal'])
encryptedX, encryptedY = float(integerX + encryptedFloatingX), float(integerY + encryptedFloatingY)
return(encryptedX, encryptedY)
def encryptString(String, CMT):
encrypted = ""
# print("String: ", String)
for char in String:
# print("char: ", char)
x, y = encrypt(char, CMT)
encrypted += str(x) + " " + str(y) + " "
# encrypted.append(x)
# encrypted.append(y)
return encrypted
def encryptByteArray(Bytes,CMT):
encrypted =""
for i in range(len(Bytes)):
print(i)
byte=Bytes[i]
x,y = encryptByte(byte,CMT)
encrypted += str(x) + " " + str(y) + " "
return encrypted