-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathaesalg.py
231 lines (210 loc) · 6.86 KB
/
aesalg.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
#Advanced Encryption Standard
#necesita operaciones.py
#de momento todo para 128 bits
#para 192: las filas no cambian, pero son 6 columnas
#transformacion del bloque de texto en una matriz 4x4
def block_matrix(block):
matrixk = [[0 for x in xrange(4)] for x in xrange(4)]
k = 0
l = 0
for j in mrange(0,len(block),8):
matrixk[k][l] = block[j:j+8]
k += 1
if k > 3:
k = 0
l += 1
return matrixk
#paso SubBytes
def sub_bytes(byte):
invbyte = inversomodulo(byte,2**8)
bit4rot = invbyte[4:] + invbyte[:4]
bit5rot = invbyte[3:] + invbyte[:3]
bit6rot = invbyte[2:] + invbyte[:2]
bit7rot = invbyte[1:] + invbyte[0]
c = '0' + bin(0x63)[2:]
byteout = xor(xor(xor(xor(xor(invbyte,bit4rot),bit5rot),bit6rot),bit7rot),c)
return byteout
#paso SubBytes para descifrar
def decr_sub_bytes(byte):
bit4rot = byte[4:] + byte[:4]
bit5rot = byte[3:] + byte[:3]
bit6rot = byte[2:] + byte[:2]
bit7rot = byte[1:] + byte[0]
c = '0' + bin(0x63)[2:]
byteout = xor(xor(xor(xor(xor(byte,bit4rot),bit5rot),bit6rot),bit7rot),c)
invbyteout = inversomodulo(byteout,2**8)
return invbyteout
#paso ShiftRows
def shift_row(matrix):
shiftmatrix = [[0 for x in xrange(4)] for x in xrange(4)]
shiftmatrix[0] = matrix[0]
shiftmatrix[1] = matrix[1][1:] + matrix[1][:1]
shiftmatrix[2] = matrix[2][2:] + matrix[2][:2]
shiftmatrix[3] = matrix[3][3:] + matrix[3][:3]
return shiftmatrix
#paso ShiftRows para descifrar
def decr_shift_row(matrix):
shiftmatrix = [[0 for x in xrange(4)] for x in xrange(4)]
shiftmatrix[0] = matrix[0]
shiftmatrix[1] = matrix[1][3:] + matrix[1][:3]
shiftmatrix[2] = matrix[2][2:] + matrix[2][:2]
shiftmatrix[3] = matrix[3][1:] + matrix[3][:1]
return shiftmatrix
#paso MixColumns
def mix_columns(matrix):
one = '00000001'
two = '00000010'
three = '00000011'
replacematrix = [[two, three, one, one],[one, two, three, one],
[one, one, two, three],[three,one,one,two]]
mixmatrix = [[bin(sum(int(a,2)*int(b,2) for a,b in zip(replacematrix_row,matrix_col)) % 2**8) for
matrix_col in zip(*matrix)] for replacematrix_row in replacematrix]
return mixmatrix
#paso MixColumns para descifrar
def decr_mix_columns(matrix):
nine = '00001001'
hexB = '00001011'
hexD = '00001101'
hexE = '00001110'
replacematrix = [[hexE, hexB, hexD, nine],[nine, hexE, hexB, hexD],
[hexD, nine, hexE, hexB],[hexB,hexD,nine,hexE]]
mixmatrix = [[bin(sum(int(a,2)*int(b,2) for a,b in zip(replacematrix_row,matrix_col)) % 2**8) for
matrix_col in zip(*matrix)] for replacematrix_row in replacematrix]
return mixmatrix
#g-function: multiplicar rc por 2 y luego xor
def g_function(word, r, rc): #r is the round
gcirc = word[8:] + word[:8]
bytes4 = []
for i in mrange(0,32,8):
bytes4.append(sub_bytes(gcirc[i:i+8]))
if r == 1:
rc = '00000001'
else:
rc = multimodulo('00000010',rc, 2**8-1)
bytes4[0] = xor(bytes4[0], rc)
newword = ''
for j in xrange(4):
newword += bytes4[j]
return newword, rc
#funcion para generar las subclaves
def key_expansion(key, r, rc): #en realidad la ronda 0 sobra
oldwords = []
newwords = []
newkey = ''
j = 0
for i in mrange(0, 128, 32):
oldwords.append(key[i:i+32])
if r == 0:
newkey += oldwords[j]
j += 1
if r != 0:
bytes4, rc = g_function(oldwords[3], r, rc)
newwords.append(xor(oldwords[0],bytes4))
for j in mrange(0,4,1):
if j != 0:
newwords.append(xor(newwords[j-1],oldwords[j]))
newkey += newwords[j]
return newkey, rc
#funcion para generar las subclaves para descifrar
def decr_key_expansion(key, r, rc): #en realidad la ronda 0 sobra
oldwords = []
newwords = []
newkey = ''
j = 0
for i in xrange(128, 0, -32):
oldwords.append(key[i-32:i])
if r == 0:
newkey += oldwords[j]
j += 1
oldwords = list(reversed(oldwords)) #40-43
if r != 0:
bytes4, rc = g_function(oldwords[3], r, rc)
newwords.append(xor(oldwords[0],bytes4))
for j in mrange(0,4,1):
if j != 0:
newwords.append(xor(newwords[j-1],oldwords[j]))
newkey += newwords[j]
return newkey, rc
#funcion para obtener las subclaves en funcion de si se cifra (encr = 1) o descifra (encr = 0)
def aes_keys(key, encr = 1):
rc = 0
allkeys = []
for r in xrange(11):
if encr == 1:
key, rc = key_expansion(key, r, rc)
elif encr == 0:
key, rc = decr_key_expansion(key, r, rc)
allkeys.append(key)
return allkeys
#algoritmo AES para cifrar
def aes_encr(plaintext, key):
xorplain = xor(plaintext,key)
allkeys = aes_keys(key)
for j in xrange(1,11):
step1 = ''
for i in mrange(0,len(xorplain),8):
step1 += sub_bytes(xorplain[i:i+8])
bmatrix = block_matrix(step1)
step2 = shift_row(bmatrix)
if j < 10:
step3 = mix_columns(step2)
elif j == 10:
step3 = step2
k = 0
l = 0
aux = ''
for m in mrange(0,len(plaintext),8):
auxaux = step3[k][l][2:]
while len(auxaux) < 8:
auxaux = '0' + auxaux
aux += auxaux
k += 1
if k > 3:
k = 0
l += 1
step4 = xor(aux,allkeys[j])
xorplain = step4
return plaintext
#algoritmo AES para descifrar
def aes_decr(plaintext, key):
xorplain = xor(plaintext,key)
allkeys = aes_keys(key,0)
for j in xrange(1,11):
bmatrix = block_matrix(xorplain)
step2 = decr_shift_row(bmatrix)
for row in step2:
for item in row:
item = decr_sub_bytes(item)
k = 0
l = 0
aux = ''
for m in mrange(0,len(plaintext),8):
auxaux = step2[k][l][2:]
while len(auxaux) < 8:
auxaux = '0' + auxaux
aux += auxaux
k += 1
if k > 3:
k = 0
l += 1
step4 = xor(aux,allkeys[j]) # a partir de aqui tambien hay que cambiar
if j < 10:
blockaux = block_matrix(step4)
step3 = decr_mix_columns(blockaux)
aux = ''
k = 0
l = 0
for m in mrange(0,len(plaintext),8):
auxaux = step3[k][l][2:]
while len(auxaux) < 8:
auxaux = '0' + auxaux
aux += auxaux
k += 1
if k > 3:
k = 0
l += 1
step3 = aux
elif j == 10:
step3 = step4
xorplain = step3
return plaintext