Python 的 AES 混合列
AES MixColumns with Python
我正在尝试在 python 中创建 aes 混合列函数。但结果并不如预期。我做错了什么?
这里我在第一列测试
[2, 3, 1, 1, 1, 2, 3, 1, 1, 1, 2, 3, 3, 1, 1, 2]
[0, 17, 34, 51, 68, 85, 102, 119, 136, 153, 170, 187, 204, 221, 238, 255]
print((0*2)^(17*3)^(34*1)^(51*1))
print((0*1)^(17*2)^(34*3)^(51*1))
print((0*1)^(17*1)^(34*2)^(51*3))
print((0*3)^(17*1)^(34*1)^(51*2))
结果如下:
34
119
204
85
我认为的预期结果是:
34
119
0
85
是预期结果错误还是我做错了?
看来你做错了:(0*1)^(17*1)^(34*2)^(51*3)
的答案是 204
。
(0*1) = 00000000
(17*1) = 00010001
(34*2) = 01000100
(51*3) = 10011001
然后在这些行上应用 XOR 运算符会导致:11001100
.
XOR 运算符如果两个位中只有一个为 1,则将每个位设置为 1。这意味着对于每个位,如果有奇数个 1,则结果位将设置为 1。
如果此答案不能帮助您理解为什么答案是 204
,请解释您是如何计算 0 的。
在发布的代码中,乘法实现错误。乘法是伽罗瓦中的模不可约多项式 x8 + x4 + x3 + x + 1字段 GF(28)。这在维基百科文章 Rijndael MixColumns. Here you can also find various implementations. Another, easier to understand description can be found on SO in the accepted answer to the post How to solve MixColumns?.
中有详细描述
在以下实现中,乘法(与 1、2 和 3)在函数 gmul()
:
中实现
def mixColumns(a, b, c, d):
printHex(gmul(a, 2) ^ gmul(b, 3) ^ gmul(c, 1) ^ gmul(d, 1))
printHex(gmul(a, 1) ^ gmul(b, 2) ^ gmul(c, 3) ^ gmul(d, 1))
printHex(gmul(a, 1) ^ gmul(b, 1) ^ gmul(c, 2) ^ gmul(d, 3))
printHex(gmul(a, 3) ^ gmul(b, 1) ^ gmul(c, 1) ^ gmul(d, 2))
print()
def gmul(a, b):
if b == 1:
return a
tmp = (a << 1) & 0xff
if b == 2:
return tmp if a < 128 else tmp ^ 0x1b
if b == 3:
return gmul(a, 2) ^ a
def printHex(val):
return print('{:02x}'.format(val), end=' ')
# test vectors from https://en.wikipedia.org/wiki/Rijndael_MixColumns#Test_vectors_for_MixColumn()
mixColumns(0xdb, 0x13, 0x53, 0x45) # 0x8e 0x4d 0xa1 0xbc
mixColumns(0xf2, 0x0a, 0x22, 0x5c) # 0x9f 0xdc 0x58 0x9d
mixColumns(0x01, 0x01, 0x01, 0x01) # 0x01 0x01 0x01 0x01
mixColumns(0xc6, 0xc6, 0xc6, 0xc6) # 0xc6 0xc6 0xc6 0xc6
mixColumns(0xd4, 0xd4, 0xd4, 0xd5) # 0xd5 0xd5 0xd7 0xd6
mixColumns(0x2d, 0x26, 0x31, 0x4c) # 0x4d 0x7e 0xbd 0xf8
# example from question
mixColumns(0, 17, 34, 51) # 0x22 0x77 0x00 0x55 = 34 119 0 85
输出:
8e 4d a1 bc
9f dc 58 9d
01 01 01 01
c6 c6 c6 c6
d5 d5 d7 d6
4d 7e bd f8
22 77 00 55
前 6 行是维基百科文章 test vectors 的结果。最后一行对应于已发布问题的示例:0x22、0x77、0x00、0x55 或 34、119、0、85 十进制。如您所见,第三个值是 0 而不是 204。
我正在尝试在 python 中创建 aes 混合列函数。但结果并不如预期。我做错了什么?
这里我在第一列测试
[2, 3, 1, 1, 1, 2, 3, 1, 1, 1, 2, 3, 3, 1, 1, 2]
[0, 17, 34, 51, 68, 85, 102, 119, 136, 153, 170, 187, 204, 221, 238, 255]
print((0*2)^(17*3)^(34*1)^(51*1))
print((0*1)^(17*2)^(34*3)^(51*1))
print((0*1)^(17*1)^(34*2)^(51*3))
print((0*3)^(17*1)^(34*1)^(51*2))
结果如下:
34
119
204
85
我认为的预期结果是:
34
119
0
85
是预期结果错误还是我做错了?
看来你做错了:(0*1)^(17*1)^(34*2)^(51*3)
的答案是 204
。
(0*1) = 00000000
(17*1) = 00010001
(34*2) = 01000100
(51*3) = 10011001
然后在这些行上应用 XOR 运算符会导致:11001100
.
XOR 运算符如果两个位中只有一个为 1,则将每个位设置为 1。这意味着对于每个位,如果有奇数个 1,则结果位将设置为 1。
如果此答案不能帮助您理解为什么答案是 204
,请解释您是如何计算 0 的。
在发布的代码中,乘法实现错误。乘法是伽罗瓦中的模不可约多项式 x8 + x4 + x3 + x + 1字段 GF(28)。这在维基百科文章 Rijndael MixColumns. Here you can also find various implementations. Another, easier to understand description can be found on SO in the accepted answer to the post How to solve MixColumns?.
中有详细描述在以下实现中,乘法(与 1、2 和 3)在函数 gmul()
:
def mixColumns(a, b, c, d):
printHex(gmul(a, 2) ^ gmul(b, 3) ^ gmul(c, 1) ^ gmul(d, 1))
printHex(gmul(a, 1) ^ gmul(b, 2) ^ gmul(c, 3) ^ gmul(d, 1))
printHex(gmul(a, 1) ^ gmul(b, 1) ^ gmul(c, 2) ^ gmul(d, 3))
printHex(gmul(a, 3) ^ gmul(b, 1) ^ gmul(c, 1) ^ gmul(d, 2))
print()
def gmul(a, b):
if b == 1:
return a
tmp = (a << 1) & 0xff
if b == 2:
return tmp if a < 128 else tmp ^ 0x1b
if b == 3:
return gmul(a, 2) ^ a
def printHex(val):
return print('{:02x}'.format(val), end=' ')
# test vectors from https://en.wikipedia.org/wiki/Rijndael_MixColumns#Test_vectors_for_MixColumn()
mixColumns(0xdb, 0x13, 0x53, 0x45) # 0x8e 0x4d 0xa1 0xbc
mixColumns(0xf2, 0x0a, 0x22, 0x5c) # 0x9f 0xdc 0x58 0x9d
mixColumns(0x01, 0x01, 0x01, 0x01) # 0x01 0x01 0x01 0x01
mixColumns(0xc6, 0xc6, 0xc6, 0xc6) # 0xc6 0xc6 0xc6 0xc6
mixColumns(0xd4, 0xd4, 0xd4, 0xd5) # 0xd5 0xd5 0xd7 0xd6
mixColumns(0x2d, 0x26, 0x31, 0x4c) # 0x4d 0x7e 0xbd 0xf8
# example from question
mixColumns(0, 17, 34, 51) # 0x22 0x77 0x00 0x55 = 34 119 0 85
输出:
8e 4d a1 bc
9f dc 58 9d
01 01 01 01
c6 c6 c6 c6
d5 d5 d7 d6
4d 7e bd f8
22 77 00 55
前 6 行是维基百科文章 test vectors 的结果。最后一行对应于已发布问题的示例:0x22、0x77、0x00、0x55 或 34、119、0、85 十进制。如您所见,第三个值是 0 而不是 204。