AES-128和AES-256在使用上有什么区别?如何改变StateMatrix或round操作?

What is the difference in the use AES-128 and AES-256? How to change the StateMatrix or round operations?

我做了一个简单的AES-128实现,只用了一轮。我想知道如果我实现 AES-256,将如何更改我的 StateMatrix 或回合操作?我知道我必须使用 256 位密钥,但我的消息仍然有 128 位长度。我知道 ShiftRow 操作会改变。

好的。例如,我有以下必须加密的消息:

 message = "encryptionaes256";
 key = "keyskeyskeyskeys";
 px = x^8 + x^4 + x^3 + x + 1;

现在我想创建我的 StateMatrix(我不知道如何创建 AES-256,这就是我的代码将描述 AES-128 的原因):

stateMatrix = Table[0, {4}, {4}];
partitionByBytes = Partition[ToDigits2Form[message], 8];
counter = 1;
For[j = 0, j < Length[partitionByBytes], ++j,
  If[Mod[j, 4] == 0 && j != 0,
   counter = counter + 1];
  stateMatrix[[Mod[j, 4] + 1, counter]] =  
   FromBitToGalua[partitionByBytes[[j + 1]]];
  ];

ToDigits2Form 将消息转换为二进制向量。 FromBitToGalua表示GF(2^8)中的字节。

下一步是将密钥创建为矩阵形式:

keyMatrix = Table[0, {4}, {4}];
partitionByBytes = Partition[ToDigits2Form[key], 8];
counter = 1;
For[j = 0, j < Length[partitionByBytes], ++j,
  If[Mod[j, 4] == 0 && j != 0,
   counter = counter + 1];
  keyMatrix[[Mod[j, 4] + 1, counter]] =  
   FromBitToGalua[partitionByBytes[[j + 1]]];
  ];

但我必须使用 AES-256,我的密钥必须有 256 位长度。这是否意味着我的矩阵将有 8 行和 4 列?如果是真的,我该如何进行回合操作?代表我的消息的 StateMatrix 将是 4x4,而我的 KeyMatrix 将是 8x4。它将如何运作?

现在我要为 SubBytes 操作创建 TransformationTable:

TransformationTable[Sbox_] := Block[{i, j, table, counter},
  table = Table[{0, 0}, {16}, {16}];
  counter = 1;
  For[i = 1, i <= 16, ++i,
   For[j = 1, j <= 16, ++j,
     table[[i, j]] = IntegerDigits[Sbox[[counter]], 16, 2];
     ++counter;
     ];
   ];
  Return[table];
  ]

table = TransformationTable[Sbox];

我不放SBox矩阵。你可以在互联网上找到它。看起来像 Sbox = {99, 124, 119, 123, 242,.......}

步骤 1. AddRoundKey

现在是第一轮行动。这是 AddRoundKey:

AddRoundKey[matrix1_, matrix2_] := Block[{i, j, result},
      result = Table[0, {4}, {4}];
      For[i = 1, i <= 4, ++i,
       For[j = 1, j <= 4, ++j,
         result[[i, j]] = 
          PolynomialMod[matrix1[[i, j]] + matrix2[[i, j]], {2}]
         ];
       ];
      Return[result];
      ]

stateMatrix = AddRoundKey[stateMatrix, keyMatrix];

我应该得到哪种尺寸的结果?也许这是个愚蠢的问题,但我坚持了下来。

步骤 2. 通过规则集替换矩阵状态的元素 table

现在我想将StateMatrix的元素转换为十六进制:

FromBitToGalua[byte_] := Block[{t1, t2, x},
  t2 = byte;
  t1 = Sum[x^(7 - i)*t2[[i + 1]], {i, 7}];
  Return[t1]
  ]
FromGaluaToBit[byte_] := Block[{t1, t2, t3, x},
  t1 = PadLeft[Reverse@CoefficientList[byte, x], 8];
  Return[t1]
  ]
From16ToGalua[byte_] := Block[{t1, t2},
  t1 = IntegerDigits[
    FromDigits[
     byte /. {a -> 10, b -> 11, c -> 12, d -> 13, e -> 14, f -> 15}, 
     16], 2, 8];
  t2 = FromBitToGalua[t1];
  Return[t2]
  ]
FromGaluaTo16[byte_] := Block[{t1, t2},
  t1 = FromGaluaToBit[byte];
  t2 = IntegerDigits[FromDigits[t1, 2], 16, 2];
  t2 /. {10 -> a, 11 -> b, 12 -> c, 13 -> d, 14 -> e, 15 -> f};
  Return[t2]
  ]

SubBytes 操作:

SubBytes[matrix_, table_] := Block[{i, j, result, pos},
  result = Table[0, {4}, {4}];
  For[i = 1, i <= 4, ++i,
   For[j = 1, j <= 4, ++j,
     pos = matrix[[i, j]];
     result[[i, j]] = table[[pos[[1]] + 1, pos[[2]] + 1]];
     ];
   ];
  Return[result];
  ]

步骤 3. ShiftRows 操作

ShiftRows[matrix_] := Block[{i, j, result, pos},
  result = Table[0, {4}, {4}];
  For[i = 0, i < 4, ++i,
   For[j = 0, j < 4, ++j,
     result[[i + 1, Mod[j - i, 4] + 1]] = matrix[[i + 1, j + 1]];
     ];
   ];
  Return[result];
  ]

我有一个问题。 AES-128 和 AES-256 的移位位置是否相同?我使用 32 位块、128 位消息和 256 位密钥。

步骤 4. 混合列

MixColumns[matrix_, tMatrix_] := Block[{c, i, j, result, col, el},
  result = Table[0, {4}, {4}];
  For[c = 1, c <= 4, ++c,
   col = matrix[[All, c]];
   For[i = 1, i <= 4, ++i,
    el = 0;
    For[j = 1, j <= 4, ++j,
     el += Multiply[tMatrix[[i, j]], col[[j]]];
     ];
    result[[i, c]] = PolynomialMod[el, {2}];
    ];
   ];
  Return[result];
  ]

你能告诉我如果我使用 AES-256 需要改变什么吗?我的消息仍然是 128 位的,而我的块是 32 位的。将如何改变逆运算?

对于所有密钥大小,AES 的块大小均为 16 字节。

使用 不同密钥大小的唯一区别只是使用不同的密钥大小,AES 密钥大小为 128、192 和 256 位。

消息大小不会根据密钥大小而改变,它是由消息大小、加密模式和任何填充决定的。