nb def mul_by_02 num if num 0x80 res num else res num 0x1b return res

 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
nb=4
def mul_by_02(num):
if num < 0x80:
res = (num << 1)
else:
res = (num << 1)^0x1b
return res % 0x100
def mul_by_09(num):
return mul_by_02(mul_by_02(mul_by_02(num)))^num
def mul_by_0b(num):
return mul_by_02(mul_by_02(mul_by_02(num)))^mul_by_02(num)^num
def mul_by_0d(num):
return mul_by_02(mul_by_02(mul_by_02(num)))^mul_by_02(mul_by_02(num))^num
def mul_by_0e(num):
return mul_by_02(mul_by_02(mul_by_02(num)))^mul_by_02(mul_by_02(num))^mul_by_02(num)
def mix_columns(state):
for i in range(nb):
s0 = mul_by_0e(state[0][i])^mul_by_0b(state[1][i])^mul_by_0d(state[2][i])^mul_by_09(state[3][i])
s1 = mul_by_09(state[0][i])^mul_by_0e(state[1][i])^mul_by_0b(state[2][i])^mul_by_0d(state[3][i])
s2 = mul_by_0d(state[0][i])^mul_by_09(state[1][i])^mul_by_0e(state[2][i])^mul_by_0b(state[3][i])
s3 = mul_by_0b(state[0][i])^mul_by_0d(state[1][i])^mul_by_09(state[2][i])^mul_by_0e(state[3][i])
state[0][i] = s0
state[1][i] = s1
state[2][i] = s2
state[3][i] = s3
return state
state = [[0x8E, 0xA5, 0xD6, 0x5A],
[0xB4, 0xAE, 0x68, 0x93],
[0xAB, 0x56, 0x81, 0x0B],
[0xC0, 0x7C, 0xA5, 0x59]]
a = [' '.join([hex(j)[2:].zfill(2) for j in i]) for i in mix_columns(state)]
for i in a:
print i