我的MixColumns实现正确吗?

时间:2020-04-18 18:32:55

标签: java-me aes

我正在尝试在J2ME中实现AES,我已经基于Wikipedia上的C#示例实现了我的:https://en.wikipedia.org/wiki/Rijndael_MixColumns#Implementation_example 我的AES实现给出的结果不正确,这是我最不信任的代码,因此我想确保它是正确的:

state是一个字节数组,以列主顺序存储状态矩阵。

  public void mix_columns() {
    byte[] new_state = new byte[16];

    for (int i = 0; i < 4; i++) {
      new_state[0|(i<<2)] = (byte)(
        galois_field_multiply((byte)0x02, state[0|(i<<2)]) ^
        galois_field_multiply((byte)0x03, state[1|(i<<2)]) ^
        state[2|(i<<2)] ^
        state[3|(i<<2)]
      );
      new_state[1|(i<<2)] = (byte)(
        state[0|(i<<2)] ^
        galois_field_multiply((byte)0x02, state[1|(i<<2)]) ^
        galois_field_multiply((byte)0x03, state[2|(i<<2)]) ^
        state[3|(i<<2)]
      );
      new_state[2|(i<<2)] = (byte)(
        state[0|(i<<2)] ^
        state[1|(i<<2)] ^
        galois_field_multiply((byte)0x02, state[2|(i<<2)]) ^
        galois_field_multiply((byte)0x03, state[3|(i<<2)])
      );
      new_state[3|(i<<2)] = (byte)(
        galois_field_multiply((byte)0x03, state[0|(i<<2)]) ^
        state[1|(i<<2)] ^
        state[2|(i<<2)] ^
        galois_field_multiply((byte)0x02, state[3|(i<<2)])
      );
    }

    state = new_state;
  }

1 个答案:

答案 0 :(得分:0)

实现是正确的,在页面上看有一个带有一些测试向量的部分。可以使用这些测试向量之一分别测试输入的每一列,这些向量可以提供正确的结果。例如,您可以提供以下状态:

        (byte)0xDB, (byte)0x13, (byte)0x53, (byte)0x45,
        (byte)0xDB, (byte)0x13, (byte)0x53, (byte)0x45,
        (byte)0xDB, (byte)0x13, (byte)0x53, (byte)0x45,
        (byte)0xDB, (byte)0x13, (byte)0x53, (byte)0x45,

,它将提供值:

        (byte)0x8E, (byte)0x4D, (byte)0xA1, (byte)0xBC,
        (byte)0x8E, (byte)0x4D, (byte)0xA1, (byte)0xBC,
        (byte)0x8E, (byte)0x4D, (byte)0xA1, (byte)0xBC,
        (byte)0x8E, (byte)0x4D, (byte)0xA1, (byte)0xBC,