如果我没记错的话,对于 AES128,我们生成 16 字节的数据,因此它的代码如下所示。
void KeyExpansion(unsigned char inputKey[16], unsigned char expandedKeys[176])
{
for(int i=0; i<16; i++)
expandedKeys[i] = inputKey[i];
int bytesGenerated = 16;
int rconIteration = 1;
unsigned char temp[4];
while(bytesGenerated <176)
{
for(int i=0; i<4; i++)
temp[i] = expandedKeys[i + bytesGenerated - 4];
if(bytesGenerated % 16 == 0)
{
keyExpansionCore(temp, rconIteration);
rconIteration++;
}
for(unsigned char a=0; a<4; a++)
{
expandedKeys[bytesGenerated] = expandedKeys[bytesGenerated - 16] ^ temp[a];
bytesGenerated++;
}
}
}
但是,我对AES256不太确定。我们是否还生成 16 字节的数据或 32 字节的数据?如果它生成 32 字节的数据,我必须将我的输入键 [16] 更改为输入键 [32]?那么扩展密钥呢?当我看到扩展键[176]和输入键[16]时,我很困惑,它不是变成192字节吗?(但我在搜索 AES128 时看到了这段代码。
不同的是密钥大小,而不是块大小(始终为 16 字节(。AES 中的密钥大小可以是 128、192 或 256 位。更多信息,另请参阅此和此StackExchange答案。