【问题标题】:Implementing AES-128 Mix-Column Function in C++在 C++ 中实现 AES-128 混合列函数
【发布时间】:2019-06-02 03:42:08
【问题描述】:

我正在尝试在 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];

}

输入(和预期输出):1101101011101101100010110111011001100110000110110101100010110100100110010001010101010100111001011100111110011011010

使用 mixCoulmns 和 invMixColumns 的输出: 101110101110100100111111100100100111011010101111100011000010000001001001011010010010110011010101111110110001101100101010010101

【问题讨论】:

  • 我不肯定,但*不应该在GF8下吗? ^ 是在 GF8 下添加的,但 * 并非如此
  • 你是绝对正确的@doug 我设法通过硬编码 GF(2^8) 的查找表来让它工作,现在就像一个魅力!感谢您的帮助!
  • 所以基本回答完了?

标签: c++ aes


【解决方案1】:

设法修复它,但忘记更新我的帖子。正如@doug 指出的那样,我没有在 GF(2^8) 下使用乘法,所以我得到了无效的答案。只需硬编码这些字段的查找表即可解决此问题(尽管我不确定这是否是最有效的方法)。

【讨论】:

  • 您问题中的代码是否已更新以使其正常工作?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-01-16
  • 1970-01-01
  • 2019-01-09
  • 2018-01-08
  • 2015-03-10
  • 2016-12-19
  • 1970-01-01
相关资源
最近更新 更多