-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathEncoder.py
More file actions
145 lines (81 loc) · 2.64 KB
/
Encoder.py
File metadata and controls
145 lines (81 loc) · 2.64 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
#This file contains all the functions needed to encrypt a string
#It does not contain key generation functions
#Import this file to use in the main one
def flip(x):
if x == 0:
return 1
if x == 1:
return 0
def FlipBits(binary,key):
msgBinary = binary
BitKey = key
msgBinaryList = list(msgBinary)
keyList = list(BitKey) #Get a list of the key
numRange = range(0,16)
CipherBlockChain = 0
for x in numRange:
keyList[x] = int(keyList[x],16)
for x in keyList:
a = msgBinaryList[int(x)]
b = flip(int(a))
msgBinaryList[int(x)] = str(b)
return msgBinaryList
def TableBits(binarylist):
TabledList = ['0', '0', '0', '0']
TabledList[0] = binarylist[0:4]
TabledList[1] = binarylist[4:8]
TabledList[2] = binarylist[8:12]
TabledList[3] = binarylist[12:16]
return TabledList
def OrderKey(keyDict, orderedKeyStr):
OrderedKeyHash = {}
numbers = range(0, 4)
for x in numbers:
OrderedKeyHash[x] = 0
i = 0
while not i == 4:
n = 0
while not n == 4:
if orderedKeyStr[i] == keyDict[n]:
OrderedKeyHash[i] = n
n = n + 1
i = i + 1
return OrderedKeyHash
def OrderByKeyList(Key):
i = 0
templist = []
orderedhexlist = []
while not i == 4:
templist.append(KeyValue(Key, i))
i = i + 1
templist = sorted(templist, reverse=True)
for value in templist:
orderedhexlist.append(hex(value)[2:])
return orderedhexlist
def KeyValue(KeyList, component):
x = int(KeyList[component], 16)
return x
def ReArrangeBit(BinaryKeyTable, OrderedKeyHash):
BinaryString = ['0', '0', '0', '0']
i = 0
n = 0
while not n > 3:
i = 0
while not i > 3:
if OrderedKeyHash[i] == n:
BinaryString[n] = BinaryKeyTable[i]
i = i + 1
n = n + 1
return BinaryString
def ReturnNumerics(FinalString):
tempstring = ''
i = 0
while not i > 3:
tempstring = tempstring + ("".join(FinalString[i]))
i = i + 1
numberlist = []
numberlist.append(int(tempstring[0:4], 2))
numberlist.append(int(tempstring[4:8], 2))
numberlist.append(int(tempstring[8:12], 2))
numberlist.append(int(tempstring[12:16], 2))
return numberlist