/
miniAES.py
196 lines (170 loc) · 5.36 KB
/
miniAES.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
from operator import xor
from math import sqrt
__author__ = 'Michal Rzezniczek'
SboxE = {(0, 0, 0, 0): [1, 1, 1, 0],
(0, 0, 0, 1): [0, 1, 0, 0],
(0, 0, 1, 0): [1, 1, 0, 1],
(0, 0, 1, 1): [0, 0, 0, 1],
(0, 1, 0, 0): [0, 0, 1, 0],
(0, 1, 0, 1): [1, 1, 1, 1],
(0, 1, 1, 0): [1, 0, 1, 1],
(0, 1, 1, 1): [1, 0, 0, 0],
(1, 0, 0, 0): [0, 0, 1, 1],
(1, 0, 0, 1): [1, 0, 1, 0],
(1, 0, 1, 0): [0, 1, 1, 0],
(1, 0, 1, 1): [1, 1, 0, 0],
(1, 1, 0, 0): [0, 1, 0, 1],
(1, 1, 0, 1): [1, 0, 0, 1],
(1, 1, 1, 0): [0, 0, 0, 0],
(1, 1, 1, 1): [0, 1, 1, 1]}
SboxD = {(0, 0, 0, 0): [1, 1, 1, 0],
(0, 0, 0, 1): [0, 0, 1, 1],
(0, 0, 1, 0): [0, 1, 0, 0],
(0, 0, 1, 1): [1, 0, 0, 0],
(0, 1, 0, 0): [0, 0, 0, 1],
(0, 1, 0, 1): [1, 1, 0, 0],
(0, 1, 1, 0): [1, 0, 1, 0],
(0, 1, 1, 1): [1, 1, 1, 1],
(1, 0, 0, 0): [0, 1, 1, 1],
(1, 0, 0, 1): [1, 1, 0, 1],
(1, 0, 1, 0): [1, 0, 0, 1],
(1, 0, 1, 1): [0, 1, 1, 0],
(1, 1, 0, 0): [1, 0, 1, 1],
(1, 1, 0, 1): [0, 0, 1, 0],
(1, 1, 1, 0): [0, 0, 0, 0],
(1, 1, 1, 1): [0, 1, 0, 1]}
def toMatrix(text, howManyBits):
i = 0
j = 0
stop = len(text) / howManyBits
output = [[]]
for letter in text:
if j == stop:
j = 0
i += 1
output.append([])
output[i].append(letter)
j += 1
return output
def add(matrix0, matrix1):
output = []
for i in range(len(matrix0)):
k = 0
output.append([])
for j in matrix0[i]:
output[i].append(xor(j, matrix1[i][k]))
k += 1
return output
def multiplyMatrixes(matrix0, matrix1):
output = [[], [], [], []]
k = 0
for eee in range(int((sqrt(len(matrix0))))):
j = 0
sqrtOfLength = int(sqrt(len(matrix0)))
for ee in range(sqrtOfLength):
i = sqrtOfLength * eee
sum = []
for e in range(sqrtOfLength):
# multiply(matrix0[i], matrix1[j])
sum.append(multiply(matrix0[i], matrix1[j]))
i += 1
j += sqrtOfLength
for e in range(len(sum[0])):
output[k].append((sum[0][e] + sum[1][e]) % 2)
k += 1
j -= len(matrix0) - 1
return output
def multiply(bits0, bits1):
polynomialOfReduction = [1, 0, 0, 1, 1, 0, 0]
output = []
tmp = []
for i in range(4):
tmp.append([0, 0, 0, 0, 0, 0, 0])
for j, bit in enumerate(bits0):
if (bit != 0) and (bits1[i] != 0):
tmp[i][j+i] = bit * bits1[i]
for i in range(7):
sum = 0
for j in range(4):
sum += tmp[j][i]
output.append(sum % 2)
for i in range(3):
if output[i] != 0:
for k, bit in enumerate(output):
tmp2 = polynomialOfReduction[k - i]
output[k] = (bit + tmp2) % 2
del output[0:3]
return output
def ZK(matrix):
tmp = matrix.pop(2)
matrix.append(tmp)
return matrix
def MM(matrix):
m = [[0, 0, 1, 1], [0, 0, 1, 0], [0, 0, 1, 0], [0, 0, 1, 1]]
output = multiplyMatrixes(m, matrix)
return output
#flag E - encrypt; D - decrypt
def Fsbox(matrix, flag):
output = []
if flag == 'E':
for element in matrix:
output.append(SboxE[tuple(element)])
elif flag == 'D':
for element in matrix:
output.append(SboxD[tuple(element)])
else:
return -1
return output
def keysGenerator(key0):
keys = []
key0InMatrix = toMatrix(key0, 4)
keys.append(key0InMatrix)
key1 = [[], [], [], []]
key2 = [[], [], [], []]
poly = [0, 0, 0, 1]
for i in range(4):
tmp = xor(poly[i], key0InMatrix[0][i])
tmp = xor(tmp, (SboxE[tuple(key0InMatrix[3])])[i])
key1[0].append(tmp)
for i in range(4):
key1[2].append(xor(key0InMatrix[2][i], key1[0][i]))
for i in range(4):
key1[1].append(xor(key0InMatrix[1][i], key1[2][i]))
for i in range(4):
key1[3].append(xor(key0InMatrix[3][i], key1[1][i]))
keys.append(key1)
poly = [0, 0, 1, 0]
for i in range(4):
tmp = xor(poly[i], key1[0][i])
tmp = xor(tmp, (SboxE[tuple(key1[3])])[i])
key2[0].append(tmp)
for i in range(4):
key2[2].append(xor(key1[2][i], key2[0][i]))
for i in range(4):
key2[1].append(xor(key1[1][i], key2[2][i]))
for i in range(4):
key2[3].append(xor(key1[3][i], key2[1][i]))
keys.append(key2)
return keys
def encrypt(key0, text):
keys = keysGenerator(key0)
encrypted = add(keys[0], toMatrix(text, 4))
encrypted = Fsbox(encrypted, 'E')
encrypted = ZK(encrypted)
encrypted = MM(encrypted)
encrypted = add(keys[1], encrypted)
encrypted = Fsbox(encrypted, 'E')
encrypted = ZK(encrypted)
encrypted = add(keys[2], encrypted)
return encrypted
def decrypt(key0, encryptedText):
keys = keysGenerator(key0)
decrypted = add(keys[2], toMatrix(encryptedText, 4))
decrypted = ZK(decrypted)
decrypted = Fsbox(decrypted, 'D')
decrypted = add(keys[1], decrypted)
decrypted = MM(decrypted)
decrypted = ZK(decrypted)
decrypted = Fsbox(decrypted, 'D')
decrypted = add(keys[0], decrypted)
return decrypted