Encryption AES加密:[1][1]和[1][2]在第1轮中,[1][1]在第2轮中,其余的是正确的
我正在尝试根据此规范实现AES密码: 根据示例(第20页及以后),我对第1轮和第2轮混合后状态列的结果为铁: 混合后的第1轮Encryption AES加密:[1][1]和[1][2]在第1轮中,[1][1]在第2轮中,其余的是正确的,encryption,aes,Encryption,Aes,我正在尝试根据此规范实现AES密码: 根据示例(第20页及以后),我对第1轮和第2轮混合后状态列的结果为铁: 混合后的第1轮 example state my state 04 e0 48 28 04 e0 48 28 66 cb f8 06 66 60 f8 06< 81 19 d3 26 81 28 d3 26< e5 9a 7a 4c e5 9a 7a 4c ^^ 如果我再次更正[1][1]并在混合后
example state my state
04 e0 48 28 04 e0 48 28
66 cb f8 06 66 60 f8 06<
81 19 d3 26 81 28 d3 26<
e5 9a 7a 4c e5 9a 7a 4c
^^
如果我再次更正[1][1]
并在混合后继续计算我的状态,则第3轮的列与示例相同
以下是第1轮中[1][1]
[1][2]
的混合列值:
FFmul(2, t[1] = b4) = d8
FFmul(3, t[(1 + 1) % 4] = t[2] = 52) = f6
t[(1 + 2) % 4] = t[3] = ae
t[(1 + 3) % 4] = t[0] = e0
=> 60
FFmul(2, t[2] = 52) = 95
FFmul(3, t[(2 + 1) % 4] = t[3] = ae) = e9
t[(2 + 2) % 4] = t[0] = e0
t[(2 + 3) % 4] = t[1] = b4
=> 28
这里是第三轮中[1][1]
的混合列值:
FFmul(2, t[1] = 39) = 2e
FFmul(3, t[(1 + 1) % 4] = t[2] = 53) = f5
t[(1 + 2) % 4] = t[3] = 89
t[(1 + 3) % 4] = t[0] = 45
我可能没有看到明显的原因,这是源代码(键扩展尚未实现,因此圆键是硬编码的):
公共类测试器
{
公共静态最终字节[][]日志表=新字节[][]{
{(字节)0x00,(字节)0x00,(字节)0x19,(字节)0x01,(字节)0x32,(字节)0x02,(字节)0x1a,(字节)0xc6,
(字节)0x4b,(字节)0xc7,(字节)0x1b,(字节)0x68,(字节)0x33,(字节)0xee,(字节)0xdf,(字节)0x03},
{(字节)0x64,(字节)0x04,(字节)0xe0,(字节)0x0e,(字节)0x34,(字节)0x8d,(字节)0x81,(字节)0xef,
(字节)0x4c,(字节)0x71,(字节)0x08,(字节)0xc8,(字节)0xf8,(字节)0x69,(字节)0x1c,(字节)0xc1},
{(字节)0x7d,(字节)0xc2,(字节)0x1d,(字节)0xb5,(字节)0xf9,(字节)0xb9,(字节)0x27,(字节)0x6a,
(字节)0x4d,(字节)0xe4,(字节)0xa6,(字节)0x72,(字节)0x9a,(字节)0xc9,(字节)0x09,(字节)0x78},
{(字节)0x65,(字节)0x2f,(字节)0x8a,(字节)0x05,(字节)0x21,(字节)0x0f,(字节)0xe1,(字节)0x24,
(字节)0x12,(字节)0xf0,(字节)0x82,(字节)0x45,(字节)0x35,(字节)0x93,(字节)0xda,(字节)0x8e},
{(字节)0x96,(字节)0x8f,(字节)0xdb,(字节)0xbd,(字节)0x36,(字节)0xd0,(字节)0xce,(字节)0x94,
(字节)0x13,(字节)0x5c,(字节)0xd2,(字节)0xf1,(字节)0x40,(字节)0x46,(字节)0x83,(字节)0x38},
{(字节)0x66,(字节)0xdd,(字节)0xfd,(字节)0x30,(字节)0xbf,(字节)0x06,(字节)0x8b,(字节)0x62,
(字节)0xb3,(字节)0x25,(字节)0xe2,(字节)0x98,(字节)0x22,(字节)0x88,(字节)0x91,(字节)0x10},
{(字节)0x7e,(字节)0x6e,(字节)0x48,(字节)0xc3,(字节)0xa3,(字节)0xb6,(字节)0x1e,(字节)0x42,
(字节)0x3a,(字节)0x6b,(字节)0x28,(字节)0x54,(字节)0xfa,(字节)0x85,(字节)0x3d,(字节)0xba},
{(字节)0x2b,(字节)0x79,(字节)0x0a,(字节)0x15,(字节)0x9b,(字节)0x9f,(字节)0x5e,(字节)0xca,
(字节)0x4e,(字节)0xd4,(字节)0xac,(字节)0xe5,(字节)0xf3,(字节)0x73,(字节)0xa7,(字节)0x57},
{(字节)0xaf,(字节)0x58,(字节)0xa8,(字节)0x50,(字节)0xf4,(字节)0xea,(字节)0xd6,(字节)0x74,
(字节)0x4f,(字节)0xae,(字节)0xe9,(字节)0xd5,(字节)0xe7,(字节)0xe6,(字节)0xad,(字节)0xe8},
{(字节)0x2c,(字节)0xd7,(字节)0x75,(字节)0x7a,(字节)0xeb,(字节)0x16,(字节)0x0b,(字节)0xf5,
(字节)0x59,(字节)0xcb,(字节)0x5f,(字节)0xb0,(字节)0x9c,(字节)0xa9,(字节)0x51,(字节)0xa0},
{(字节)0x7f,(字节)0x0c,(字节)0xf6,(字节)0x6f,(字节)0x17,(字节)0xc4,(字节)0x49,(字节)0xec,
(字节)0xd8,(字节)0x43,(字节)0x1f,(字节)0x2d,(字节)0xa4,(字节)0x76,(字节)0x7b,(字节)0xb7},
{(字节)0xcc,(字节)0xbb,(字节)0x3e,(字节)0x5a,(字节)0xfb,(字节)0x60,(字节)0xb1,(字节)0x86,
(字节)0x3b,(字节)0x52,(字节)0xa1,(字节)0x6c,(字节)0xaa,(字节)0x55,(字节)0x29,(字节)0x9d},
{(字节)0x97,(字节)0xb2,(字节)0x87,(字节)0x90,(字节)0x61,(字节)0xbe,(字节)0xdc,(字节)0xfc,
(字节)0xbc,(字节)0x95,(字节)0xcf,(字节)0xcd,(字节)0x37,(字节)0x3f,(字节)0x5b,(字节)0xd1},
{(字节)0x53,(字节)0x39,(字节)0x84,(字节)0x3c,(字节)0x41,(字节)0xa2,(字节)0x6d,(字节)0x47,
(字节)0x14,(字节)0x2a,(字节)0x9e,(字节)0x5d,(字节)0x56,(字节)0xf2,(字节)0xd3,(字节)0xab},
{(字节)0x44,(字节)0x11,(字节)0x92,(字节)0xd9,(字节)0x23,(字节)0x20,(字节)0x2e,(字节)0x89,
(字节)0xb4,(字节)0x7c,(字节)0xb8,(字节)0x26,(字节)0x77,(字节)0x99,(字节)0xe3,(字节)0xa5},
{(字节)0x67,(字节)0x4a,(字节)0xed,(字节)0xde,(字节)0xc5,(字节)0x31,(字节)0xfe,(字节)0x18,
(字节)0x0d,(字节)0x63,(字节)0x8c,(字节)0x80,(字节)0xc0,(字节)0xf7,(字节)0x70,(字节)0x07}
};
公共静态最终字节[][]powTable=新字节[][]{
{(字节)0x01,(字节)0x03,(字节)0x05,(字节)0x0f,(字节)0x11,(字节)0x33,(字节)0x55,(字节)0xff,
(字节)0x1a,(字节)0x2e,(字节)0x72,(字节)0x96,(字节)0xa1,(字节)0xf8,(字节)0x13,(字节)0x35},
{(字节)0x5f,(字节)0xe1,(字节)0x38,(字节)0x48,(字节)0xd8,(字节)0x73,(字节)0x95,(字节)0xa4,
(字节)0xf7,(字节)0x02,(字节)0x06,(字节)0x0a,(字节)0x1e,(字节)0x22,(字节)0x66,(字节)0xaa},
{(字节)0xe5,(字节)0x34,(字节)0x5c,(字节)0xe4,(字节)0x37,(字节)0x59,(字节)0xeb,(字节)0x26,
(字节)0x6a,(字节)0xbe,(字节)0xd9,(字节)0x70,(字节)0x90,(字节)0xab,(字节)0xe6,(字节)0x31},
{(字节)0x53,(字节)0xf5,(字节)0x04,(字节)0x0c,(字节)0x14,(字节)0x3c,(字节)0x44,(字节)0xcc,
(字节)0x4f,(字节)0xd1,(字节)0x68,(字节)0xb8,(字节)0xd3,(字节)0x6e,(字节)0xb2,(字节)0xcd},
{(字节)0x4c,(字节)0xd4,(字节)0x67,(字节)0xa9,(字节)0xe0,(字节)0x3b,(字节)0x4d,(字节)0xd7,
(字节)0x62,(字节)0xa6,(字节)0xf1,(字节)0x08,(字节)0x18,(字节)0x28,(字节)0x78,(字节)0x88},
{(字节)0x83,(字节)0x9e,(字节)0xb9,(字节)0xd0,(字节)0x6b,(字节)0xbd,(字节)0xdc,(字节)0x7f,
(字节)0x81,(字节)0x98,(字节)0xb3,(字节)0xce,(字节)0x49,(字节)0xdb,(字节)0x76,(字节)0x9a},
{(字节)0xb5,(字节)0xc4,(字节)0x57,(按
FFmul(2, t[1] = 39) = 2e
FFmul(3, t[(1 + 1) % 4] = t[2] = 53) = f5
t[(1 + 2) % 4] = t[3] = 89
t[(1 + 3) % 4] = t[0] = 45
public class Tester
{
public static final byte[][] logTable = new byte[][] {
{(byte) 0x00, (byte) 0x00, (byte) 0x19, (byte) 0x01, (byte) 0x32, (byte) 0x02, (byte) 0x1a, (byte) 0xc6,
(byte) 0x4b, (byte) 0xc7, (byte) 0x1b, (byte) 0x68, (byte) 0x33, (byte) 0xee, (byte) 0xdf, (byte) 0x03},
{(byte) 0x64, (byte) 0x04, (byte) 0xe0, (byte) 0x0e, (byte) 0x34, (byte) 0x8d, (byte) 0x81, (byte) 0xef,
(byte) 0x4c, (byte) 0x71, (byte) 0x08, (byte) 0xc8, (byte) 0xf8, (byte) 0x69, (byte) 0x1c, (byte) 0xc1},
{(byte) 0x7d, (byte) 0xc2, (byte) 0x1d, (byte) 0xb5, (byte) 0xf9, (byte) 0xb9, (byte) 0x27, (byte) 0x6a,
(byte) 0x4d, (byte) 0xe4, (byte) 0xa6, (byte) 0x72, (byte) 0x9a, (byte) 0xc9, (byte) 0x09, (byte) 0x78},
{(byte) 0x65, (byte) 0x2f, (byte) 0x8a, (byte) 0x05, (byte) 0x21, (byte) 0x0f, (byte) 0xe1, (byte) 0x24,
(byte) 0x12, (byte) 0xf0, (byte) 0x82, (byte) 0x45, (byte) 0x35, (byte) 0x93, (byte) 0xda, (byte) 0x8e},
{(byte) 0x96, (byte) 0x8f, (byte) 0xdb, (byte) 0xbd, (byte) 0x36, (byte) 0xd0, (byte) 0xce, (byte) 0x94,
(byte) 0x13, (byte) 0x5c, (byte) 0xd2, (byte) 0xf1, (byte) 0x40, (byte) 0x46, (byte) 0x83, (byte) 0x38},
{(byte) 0x66, (byte) 0xdd, (byte) 0xfd, (byte) 0x30, (byte) 0xbf, (byte) 0x06, (byte) 0x8b, (byte) 0x62,
(byte) 0xb3, (byte) 0x25, (byte) 0xe2, (byte) 0x98, (byte) 0x22, (byte) 0x88, (byte) 0x91, (byte) 0x10},
{(byte) 0x7e, (byte) 0x6e, (byte) 0x48, (byte) 0xc3, (byte) 0xa3, (byte) 0xb6, (byte) 0x1e, (byte) 0x42,
(byte) 0x3a, (byte) 0x6b, (byte) 0x28, (byte) 0x54, (byte) 0xfa, (byte) 0x85, (byte) 0x3d, (byte) 0xba},
{(byte) 0x2b, (byte) 0x79, (byte) 0x0a, (byte) 0x15, (byte) 0x9b, (byte) 0x9f, (byte) 0x5e, (byte) 0xca,
(byte) 0x4e, (byte) 0xd4, (byte) 0xac, (byte) 0xe5, (byte) 0xf3, (byte) 0x73, (byte) 0xa7, (byte) 0x57},
{(byte) 0xaf, (byte) 0x58, (byte) 0xa8, (byte) 0x50, (byte) 0xf4, (byte) 0xea, (byte) 0xd6, (byte) 0x74,
(byte) 0x4f, (byte) 0xae, (byte) 0xe9, (byte) 0xd5, (byte) 0xe7, (byte) 0xe6, (byte) 0xad, (byte) 0xe8},
{(byte) 0x2c, (byte) 0xd7, (byte) 0x75, (byte) 0x7a, (byte) 0xeb, (byte) 0x16, (byte) 0x0b, (byte) 0xf5,
(byte) 0x59, (byte) 0xcb, (byte) 0x5f, (byte) 0xb0, (byte) 0x9c, (byte) 0xa9, (byte) 0x51, (byte) 0xa0},
{(byte) 0x7f, (byte) 0x0c, (byte) 0xf6, (byte) 0x6f, (byte) 0x17, (byte) 0xc4, (byte) 0x49, (byte) 0xec,
(byte) 0xd8, (byte) 0x43, (byte) 0x1f, (byte) 0x2d, (byte) 0xa4, (byte) 0x76, (byte) 0x7b, (byte) 0xb7},
{(byte) 0xcc, (byte) 0xbb, (byte) 0x3e, (byte) 0x5a, (byte) 0xfb, (byte) 0x60, (byte) 0xb1, (byte) 0x86,
(byte) 0x3b, (byte) 0x52, (byte) 0xa1, (byte) 0x6c, (byte) 0xaa, (byte) 0x55, (byte) 0x29, (byte) 0x9d},
{(byte) 0x97, (byte) 0xb2, (byte) 0x87, (byte) 0x90, (byte) 0x61, (byte) 0xbe, (byte) 0xdc, (byte) 0xfc,
(byte) 0xbc, (byte) 0x95, (byte) 0xcf, (byte) 0xcd, (byte) 0x37, (byte) 0x3f, (byte) 0x5b, (byte) 0xd1},
{(byte) 0x53, (byte) 0x39, (byte) 0x84, (byte) 0x3c, (byte) 0x41, (byte) 0xa2, (byte) 0x6d, (byte) 0x47,
(byte) 0x14, (byte) 0x2a, (byte) 0x9e, (byte) 0x5d, (byte) 0x56, (byte) 0xf2, (byte) 0xd3, (byte) 0xab},
{(byte) 0x44, (byte) 0x11, (byte) 0x92, (byte) 0xd9, (byte) 0x23, (byte) 0x20, (byte) 0x2e, (byte) 0x89,
(byte) 0xb4, (byte) 0x7c, (byte) 0xb8, (byte) 0x26, (byte) 0x77, (byte) 0x99, (byte) 0xe3, (byte) 0xa5},
{(byte) 0x67, (byte) 0x4a, (byte) 0xed, (byte) 0xde, (byte) 0xc5, (byte) 0x31, (byte) 0xfe, (byte) 0x18,
(byte) 0x0d, (byte) 0x63, (byte) 0x8c, (byte) 0x80, (byte) 0xc0, (byte) 0xf7, (byte) 0x70, (byte) 0x07}
};
public static final byte[][] powTable = new byte[][] {
{(byte) 0x01, (byte) 0x03, (byte) 0x05, (byte) 0x0f, (byte) 0x11, (byte) 0x33, (byte) 0x55, (byte) 0xff,
(byte) 0x1a, (byte) 0x2e, (byte) 0x72, (byte) 0x96, (byte) 0xa1, (byte) 0xf8, (byte) 0x13, (byte) 0x35},
{(byte) 0x5f, (byte) 0xe1, (byte) 0x38, (byte) 0x48, (byte) 0xd8, (byte) 0x73, (byte) 0x95, (byte) 0xa4,
(byte) 0xf7, (byte) 0x02, (byte) 0x06, (byte) 0x0a, (byte) 0x1e, (byte) 0x22, (byte) 0x66, (byte) 0xaa},
{(byte) 0xe5, (byte) 0x34, (byte) 0x5c, (byte) 0xe4, (byte) 0x37, (byte) 0x59, (byte) 0xeb, (byte) 0x26,
(byte) 0x6a, (byte) 0xbe, (byte) 0xd9, (byte) 0x70, (byte) 0x90, (byte) 0xab, (byte) 0xe6, (byte) 0x31},
{(byte) 0x53, (byte) 0xf5, (byte) 0x04, (byte) 0x0c, (byte) 0x14, (byte) 0x3c, (byte) 0x44, (byte) 0xcc,
(byte) 0x4f, (byte) 0xd1, (byte) 0x68, (byte) 0xb8, (byte) 0xd3, (byte) 0x6e, (byte) 0xb2, (byte) 0xcd},
{(byte) 0x4c, (byte) 0xd4, (byte) 0x67, (byte) 0xa9, (byte) 0xe0, (byte) 0x3b, (byte) 0x4d, (byte) 0xd7,
(byte) 0x62, (byte) 0xa6, (byte) 0xf1, (byte) 0x08, (byte) 0x18, (byte) 0x28, (byte) 0x78, (byte) 0x88},
{(byte) 0x83, (byte) 0x9e, (byte) 0xb9, (byte) 0xd0, (byte) 0x6b, (byte) 0xbd, (byte) 0xdc, (byte) 0x7f,
(byte) 0x81, (byte) 0x98, (byte) 0xb3, (byte) 0xce, (byte) 0x49, (byte) 0xdb, (byte) 0x76, (byte) 0x9a},
{(byte) 0xb5, (byte) 0xc4, (byte) 0x57, (byte) 0xf9, (byte) 0x10, (byte) 0x30, (byte) 0x50, (byte) 0xf0,
(byte) 0x0b, (byte) 0x1d, (byte) 0x27, (byte) 0x69, (byte) 0xbb, (byte) 0xd6, (byte) 0x61, (byte) 0xa3},
{(byte) 0xfe, (byte) 0x19, (byte) 0x2b, (byte) 0x7d, (byte) 0x87, (byte) 0x92, (byte) 0xad, (byte) 0xec,
(byte) 0x2f, (byte) 0x71, (byte) 0x93, (byte) 0xae, (byte) 0xe9, (byte) 0x20, (byte) 0x60, (byte) 0xa0},
{(byte) 0xfb, (byte) 0x16, (byte) 0x3a, (byte) 0x4e, (byte) 0xd2, (byte) 0x6d, (byte) 0xb7, (byte) 0xc2,
(byte) 0x5d, (byte) 0xe7, (byte) 0x32, (byte) 0x56, (byte) 0xfa, (byte) 0x15, (byte) 0x3f, (byte) 0x41},
{(byte) 0xc3, (byte) 0x5e, (byte) 0xe2, (byte) 0x3d, (byte) 0x47, (byte) 0xc9, (byte) 0x40, (byte) 0xc0,
(byte) 0x5b, (byte) 0xed, (byte) 0x2c, (byte) 0x74, (byte) 0x9c, (byte) 0xbf, (byte) 0xda, (byte) 0x75},
{(byte) 0x9f, (byte) 0xba, (byte) 0xd5, (byte) 0x64, (byte) 0xac, (byte) 0xef, (byte) 0x2a, (byte) 0x7e,
(byte) 0x82, (byte) 0x9d, (byte) 0xbc, (byte) 0xdf, (byte) 0x7a, (byte) 0x8e, (byte) 0x89, (byte) 0x80},
{(byte) 0x9b, (byte) 0xb6, (byte) 0xc1, (byte) 0x58, (byte) 0xe8, (byte) 0x23, (byte) 0x65, (byte) 0xaf,
(byte) 0xea, (byte) 0x25, (byte) 0x6f, (byte) 0xb1, (byte) 0xc8, (byte) 0x43, (byte) 0xc5, (byte) 0x54},
{(byte) 0xfc, (byte) 0x1f, (byte) 0x21, (byte) 0x63, (byte) 0xa5, (byte) 0xf4, (byte) 0x07, (byte) 0x09,
(byte) 0x1b, (byte) 0x2d, (byte) 0x77, (byte) 0x99, (byte) 0xb0, (byte) 0xcb, (byte) 0x46, (byte) 0xca},
{(byte) 0x45, (byte) 0xcf, (byte) 0x4a, (byte) 0xde, (byte) 0x79, (byte) 0x8b, (byte) 0x86, (byte) 0x91,
(byte) 0xa8, (byte) 0xe3, (byte) 0x3e, (byte) 0x42, (byte) 0xc6, (byte) 0x51, (byte) 0xf3, (byte) 0x0e},
{(byte) 0x12, (byte) 0x36, (byte) 0x5a, (byte) 0xee, (byte) 0x29, (byte) 0x7b, (byte) 0x8d, (byte) 0x8c,
(byte) 0x8f, (byte) 0x8a, (byte) 0x85, (byte) 0x94, (byte) 0xa7, (byte) 0xf2, (byte) 0x0d, (byte) 0x17},
{(byte) 0x39, (byte) 0x4b, (byte) 0xdd, (byte) 0x7c, (byte) 0x84, (byte) 0x97, (byte) 0xa2, (byte) 0xfd,
(byte) 0x1c, (byte) 0x24, (byte) 0x6c, (byte) 0xb4, (byte) 0xc7, (byte) 0x52, (byte) 0xf6, (byte) 0x01}
};
public static final byte[][] substitutionTable = new byte[][] {
{(byte) 0x63, (byte) 0x7c, (byte) 0x77, (byte) 0x7b, (byte) 0xf2, (byte) 0x6b, (byte) 0x6f, (byte) 0xc5,
(byte) 0x30, (byte) 0x01, (byte) 0x67, (byte) 0x2b, (byte) 0xfe, (byte) 0xd7, (byte) 0xab, (byte) 0x76},
{(byte) 0xca, (byte) 0x82, (byte) 0xc9, (byte) 0x7d, (byte) 0xfa, (byte) 0x59, (byte) 0x47, (byte) 0xf0,
(byte) 0xad, (byte) 0xd4, (byte) 0xa2, (byte) 0xaf, (byte) 0x9c, (byte) 0xa4, (byte) 0x72, (byte) 0xc0},
{(byte) 0xb7, (byte) 0xfd, (byte) 0x93, (byte) 0x26, (byte) 0x36, (byte) 0x3f, (byte) 0xf7, (byte) 0xcc,
(byte) 0x34, (byte) 0xa5, (byte) 0xe5, (byte) 0xf1, (byte) 0x71, (byte) 0xd8, (byte) 0x31, (byte) 0x15},
{(byte) 0x04, (byte) 0xc7, (byte) 0x23, (byte) 0xc3, (byte) 0x18, (byte) 0x96, (byte) 0x05, (byte) 0x9a,
(byte) 0x07, (byte) 0x12, (byte) 0x80, (byte) 0xe2, (byte) 0xeb, (byte) 0x27, (byte) 0xb2, (byte) 0x75},
{(byte) 0x09, (byte) 0x83, (byte) 0x2c, (byte) 0x1a, (byte) 0x1b, (byte) 0x6e, (byte) 0x5a, (byte) 0xa0,
(byte) 0x52, (byte) 0x3b, (byte) 0xd6, (byte) 0xb3, (byte) 0x29, (byte) 0xe3, (byte) 0x2f, (byte) 0x84},
{(byte) 0x53, (byte) 0xd1, (byte) 0x00, (byte) 0xed, (byte) 0x20, (byte) 0xfc, (byte) 0xb1, (byte) 0x5b,
(byte) 0x6a, (byte) 0xcb, (byte) 0xbe, (byte) 0x39, (byte) 0x4a, (byte) 0x4c, (byte) 0x58, (byte) 0xcf},
{(byte) 0xd0, (byte) 0xef, (byte) 0xaa, (byte) 0xfb, (byte) 0x43, (byte) 0x4d, (byte) 0x33, (byte) 0x85,
(byte) 0x45, (byte) 0xf9, (byte) 0x02, (byte) 0x7f, (byte) 0x50, (byte) 0x3c, (byte) 0x9f, (byte) 0xa8},
{(byte) 0x51, (byte) 0xa3, (byte) 0x40, (byte) 0x8f, (byte) 0x92, (byte) 0x9d, (byte) 0x38, (byte) 0xf5,
(byte) 0xbc, (byte) 0xb6, (byte) 0xda, (byte) 0x21, (byte) 0x10, (byte) 0xff, (byte) 0xf3, (byte) 0xd2},
{(byte) 0xcd, (byte) 0x0c, (byte) 0x13, (byte) 0xec, (byte) 0x5f, (byte) 0x97, (byte) 0x44, (byte) 0x17,
(byte) 0xc4, (byte) 0xa7, (byte) 0x7e, (byte) 0x3d, (byte) 0x64, (byte) 0x5d, (byte) 0x19, (byte) 0x73},
{(byte) 0x60, (byte) 0x81, (byte) 0x4f, (byte) 0xdc, (byte) 0x22, (byte) 0x2a, (byte) 0x90, (byte) 0x88,
(byte) 0x46, (byte) 0xee, (byte) 0xb8, (byte) 0x14, (byte) 0xde, (byte) 0x5e, (byte) 0x0b, (byte) 0xdb},
{(byte) 0xe0, (byte) 0x32, (byte) 0x3a, (byte) 0x0a, (byte) 0x49, (byte) 0x06, (byte) 0x24, (byte) 0x5c,
(byte) 0xc2, (byte) 0xd3, (byte) 0xac, (byte) 0x62, (byte) 0x91, (byte) 0x95, (byte) 0xe4, (byte) 0x79},
{(byte) 0xe7, (byte) 0xc8, (byte) 0x37, (byte) 0x6d, (byte) 0x8d, (byte) 0xd5, (byte) 0x4e, (byte) 0xa9,
(byte) 0x6c, (byte) 0x56, (byte) 0xf4, (byte) 0xea, (byte) 0x65, (byte) 0x7a, (byte) 0xae, (byte) 0x08},
{(byte) 0xba, (byte) 0x78, (byte) 0x25, (byte) 0x2e, (byte) 0x1c, (byte) 0xa6, (byte) 0xb4, (byte) 0xc6,
(byte) 0xe8, (byte) 0xdd, (byte) 0x74, (byte) 0x1f, (byte) 0x4b, (byte) 0xbd, (byte) 0x8b, (byte) 0x8a},
{(byte) 0x70, (byte) 0x3e, (byte) 0xb5, (byte) 0x66, (byte) 0x48, (byte) 0x03, (byte) 0xf6, (byte) 0x0e,
(byte) 0x61, (byte) 0x35, (byte) 0x57, (byte) 0xb9, (byte) 0x86, (byte) 0xc1, (byte) 0x1d, (byte) 0x9e},
{(byte) 0xe1, (byte) 0xf8, (byte) 0x98, (byte) 0x11, (byte) 0x69, (byte) 0xd9, (byte) 0x8e, (byte) 0x94,
(byte) 0x9b, (byte) 0x1e, (byte) 0x87, (byte) 0xe9, (byte) 0xce, (byte) 0x55, (byte) 0x28, (byte) 0xdf},
{(byte) 0x8c, (byte) 0xa1, (byte) 0x89, (byte) 0x0d, (byte) 0xbf, (byte) 0xe6, (byte) 0x42, (byte) 0x68,
(byte) 0x41, (byte) 0x99, (byte) 0x2d, (byte) 0x0f, (byte) 0xb0, (byte) 0x54, (byte) 0xbb, (byte) 0x16}
};
public static void main(String... args) throws Exception
{
byte[] input = new byte[] {
(byte) 0x32, (byte) 0x43, (byte) 0xf6, (byte) 0xa8, (byte) 0x88, (byte) 0x5a, (byte) 0x30, (byte) 0x8d,
(byte) 0x31, (byte) 0x31, (byte) 0x98, (byte) 0xa2, (byte) 0xe0, (byte) 0x37, (byte) 0x07, (byte) 0x34
};
byte[] key = new byte[] {
(byte) 0x2b, (byte) 0x7e, (byte) 0x15, (byte) 0x16, (byte) 0x28, (byte) 0xae, (byte) 0xd2, (byte) 0xa6,
(byte) 0xab, (byte) 0xf7, (byte) 0x15, (byte) 0x88, (byte) 0x09, (byte) 0xcf, (byte) 0x4f, (byte) 0x3c
};
byte[] roundKey1 = new byte[] {
(byte) 0xa0, (byte) 0xfa, (byte) 0xfe, (byte) 0x17, (byte) 0x88, (byte) 0x54, (byte) 0x2c, (byte) 0xb1,
(byte) 0x23, (byte) 0xa3, (byte) 0x39, (byte) 0x39, (byte) 0x2a, (byte) 0x6c, (byte) 0x76, (byte) 0x05
};
byte[] roundKey2 = new byte[] {
(byte) 0xf2, (byte) 0xc2, (byte) 0x95, (byte) 0xf2, (byte) 0x7a, (byte) 0x96, (byte) 0xb9, (byte) 0x43,
(byte) 0x59, (byte) 0x35, (byte) 0x80, (byte) 0x7a, (byte) 0x73, (byte) 0x59, (byte) 0xf6, (byte) 0x7f
};
// [row][col]
byte[][] state = new byte[4][4];
// [round][row][col]
byte[][][] roundKeys = new byte[3][4][4];
// [row]
byte[] shiftRowsOffsets = new byte[] {0, 1, 2, 3};
// fill the arrays
fillTable(state, input);
fillTable(roundKeys[0], key);
fillTable(roundKeys[1], roundKey1);
fillTable(roundKeys[2], roundKey2);
// printTable(state);
// printTable(roundKeys[0]);
tableXOR(state, roundKeys[0]);
//printTable(state);
substituteBytes(state);
// printTable(state);
shiftRows(state, shiftRowsOffsets);
// printTable(state);
mixColumns(state);
printTable(state);
state[1][1] = (byte) 0xcb;
state[2][1] = (byte) 0x19;
tableXOR(state, roundKeys[1]);
substituteBytes(state);
shiftRows(state, shiftRowsOffsets);
mixColumns(state);
printTable(state);
state[1][1] = (byte) 0x4b;
tableXOR(state, roundKeys[2]);
substituteBytes(state);
shiftRows(state, shiftRowsOffsets);
mixColumns(state);
printTable(state);
}
public static void printTable(byte[][] table)
{
synchronized(System.out)
{
System.out.print(" ");
for (byte col = 0;col < table[0].length;col++)
{
System.out.print(formatHex(col, ' '));
System.out.print(' ');
}
System.out.println();
for (byte row = 0;row < table.length;row++)
{
System.out.print(formatHex(row, ' '));
System.out.print(' ');
for (byte col = 0;col < table[row].length;col++)
{
System.out.print(formatHex(table[row][col], '0'));
System.out.print(' ');
}
System.out.println();
}
}
}
public static String formatHex(byte value, char pad)
{
int val = value & 0xFF;
String str = Integer.toString(val, 16);
if (str.length() == 2)
{
return str;
}
else
{
return pad + str;
}
}
public static void substituteBytes(byte[][] target)
{
for (int row = 0;row < 4;row++)
{
for (int col = 0;col < target[row].length;col++)
{
target[row][col] = lookupTable(substitutionTable, target[row][col]);
}
}
}
public static void tableXOR(byte[][] dataTarget, byte[][] xorSource)
{
for (int row = 0;row < dataTarget.length;row++)
{
for (int col = 0;col < dataTarget[row].length;col++)
{
dataTarget[row][col] = (byte) (dataTarget[row][col] ^ xorSource[row][col]);
}
}
}
public static void fillTable(byte[][] table, byte[] data)
{
int pointer = 0;
for (int col = 0;col < table[0].length;col++)
{
for (int row = 0;row < table.length;row++)
{
table[row][col] = data[pointer++];
}
}
}
private static void shiftRows(byte[][] target, byte[] offsets)
{
int nCols = target[0].length;
for (int row = 0;row < 4;row++)
{
byte[] rowBuff = new byte[offsets[row]];
byte offset = offsets[row];
System.arraycopy(target[row], 0, rowBuff, 0, offset);
System.arraycopy(target[row], offset, target[row], 0, nCols - offset);
System.arraycopy(rowBuff, 0, target[row], nCols - offset, offset);
}
}
private static void mixColumns(byte[][] target)
{
byte[] t = new byte[4];
int nCols = target[0].length;
for (int col = 0;col < nCols;col++)
{
for (int row = 0;row < 4;row++)
{
t[row] = target[row][col];
}
for (int row = 0;row < 4;row++)
{
/*
state[r,c] = FFmul(0x02, t[r]) xor
FFmul(0x03, t[(r + 1) mod 4]) xor
t[(r + 2) mod 4] xor t[(r + 3) mod 4]
*/
byte a = fieldMultiplication((byte) 2, t[row]);
byte b = fieldMultiplication((byte) 3, t[(row + 1) % 4]);
byte c = t[(row + 2) % 4];
byte d = t[(row + 3) % 4];
target[row][col] = (byte) (
a ^ b ^ c ^ d);
if (col == 1 && (row == 1 || row == 2))
{
int id = row;
System.out.println("FFmul(2, t[" + id + "] = "
+ formatHex(t[id], '0') + ") = "
+ formatHex(a, '0'));
id = (row + 1) % 4;
System.out.println("FFmul(3, t[(" + row + " + 1) % 4] = "
+ "t[" + id + "] = "
+ formatHex(t[id], '0') + ") = "
+ formatHex(b, '0'));
id = (row + 2) % 4;
System.out.println("t[(" + row + " + 2) % 4] = "
+ "t[" + id + "] = "
+ formatHex(c, '0'));
id = (row + 3) % 4;
System.out.println("t[(" + row + " + 3) % 4] = "
+ "t[" + id + "] = "
+ formatHex(d, '0'));
System.out.println("=> " + formatHex((byte) (a ^ b ^ c ^ d), '0'));
System.out.println();
}
}
}
}
private static byte lookupTable(byte[][] table, byte position)
{
int x = (position & 0xF0) >> 4;
int y = position & 0x0F;
return table[x][y];
}
private static byte fieldMultiplication(byte a, byte b)
{
if (a != 0 && b != 0)
{
int t = lookupTable(logTable, a) + lookupTable(logTable, b);
return lookupTable(powTable, (byte) (t % 0xFF));
}
else
{
return 0;
}
}
}
int t = lookupTable(logTable, a) + lookupTable(logTable, b);
int t = (lookupTable(logTable, a) & 0xFF) + (lookupTable(logTable, b) & 0xFF);