首页 > 解决方案 > 我的 MixColumns 实现是否正确?

问题描述

我正在尝试在 J2ME 中实现 AES,我已经根据维基百科上的 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;
  }

标签: java-meaes

解决方案


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

        (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,

推荐阅读