我正在尝试在C ++中实现“混合列”功能及其逆函数。
我要完成AES-128的作业。我的所有其他功能(以及相反的功能)都可以正常工作。但是,我在努力使mixcolumn函数起作用。在应用函数之前,我先打印纯文本,然后再应用混合列及其倒数并打印出结果。这两个输出不匹配,我不知道为什么会这样。
void mixColumns(array< array<uint8_t, 4>, 4> &state)
{
//Create temp variable to store intermediate results
array< array<uint8_t,4>, 4> temp;
//Perform matrix multiplication under GF
for(int i=0;i<4;i++)
{
temp[0][i] = (0x02 * state[0][i]) ^ (0x03 * state[1][i]) ^ state[2][i] ^ state[3][i];
temp[1][i] = state[0][i] ^ (0x02 * state[1][i]) ^ (0x03 * state[2][i]) ^ state[3][i];
temp[2][i] = state[0][i] ^ state[1][i] ^ (0x02 * state[2][i]) ^ (0x03 * state[3][i]);
temp[3][i] = (0x03 * state[0][i]) ^ state[1][i] ^ state[2][i] ^ (0x02 * state[3][i]);
}
//Fill state with mix column data
for(int i=0;i<4;i++)
for(int j=0;j<4;j++)
state[j][i] = temp[j][i];
}
void invMixColumns(array< array<uint8_t, 4>, 4> &state)
{
//Create temp variable to store intermediate results
array< array<uint8_t,4>, 4> temp;
for(int i=0;i<4;i++)
{
temp[0][i] = (0x0E * state[0][i]) ^ (0x0B * state[1][i]) ^ (0x0D * state[2][i]) ^ (0x09 * state[3][i]);
temp[1][i] = (0x09 * state[0][i]) ^ (0x0E * state[1][i]) ^ (0x0B * state[2][i]) ^ (0x0D * state[3][i]);
temp[2][i] = (0x0D * state[0][i]) ^ (0x09 * state[1][i]) ^ (0x0E * state[2][i]) ^ (0x0B * state[3][i]);
temp[3][i] = (0x0B * state[0][i]) ^ (0x0D * state[1][i]) ^ (0x09 * state[2][i]) ^ (0x0E * state[3][i]);
}
//Fill state with inverse column data
for(int i=0;i<4;i++)
for(int j=0;j<4;j++)
state[j][i] = temp[j][i];
}
输入(和预期输出):1101101011101101101100010110111011001100110000110110101100010110100100110010001010101010100111001100110011001100110110110000110110010
带有mixCoulmns和invMixColumns的输出: 10111010111010010011111110010010011101101010111110001100001000000100100101101001001011001101010111111011000110110011010100110110
答案 0 :(得分:0)
设法解决它,但忘记更新我的帖子。正如@doug指出的那样,我没有在GF(2 ^ 8)下使用乘法,所以得到的答案无效。只需对这些字段进行硬编码查找表即可解决此问题(尽管我不确定这是否是最有效的方法)。