gpt4 book ai didi

c - AES 混合列错误

转载 作者:太空宇宙 更新时间:2023-11-03 23:33:04 25 4
gpt4 key购买 nike

现在我正在学习密码学。所以,(为了练习和乐趣),我决定实现 AES。我陷入了一点(这里是我的代码混合列):

typedef vector< vector<short> > vvector;

short mixBox[4][4] =
{
{0x02, 0x03, 0x01, 0x01},
{0x01, 0x02, 0x03, 0x01},
{0x01, 0x01, 0x02, 0x03},
{0x03, 0x01, 0x01, 0x02}
};

short gfMultiply(short h1, short h2)
{
//h1 can 0x01, 0x02 or 0x03
}

void mixColumns(vvector & v)
{
vvector res(v.begin(), v.end());
for(int i=0; i<4; i++)
for(int j=0; j<4; j++)
v[i][j] = 0x00;

for(int i=0; i<4; i++)
for(int j=0; j<4; j++)
for(int k=0; k<4; k++)
v[i][j] = v[i][j] ^ gfMultiply(mixBox[i][k], res[k][j]);
}

理论上,我理解乘法 gf(2^8),但是对于实现算法,我有问题。我提到了这个site .但是要么我不明白某些点,要么我做错了什么。在维基百科中我读到了这个:

"The multiplication operation is defined as: multiplication by 1 means no change, multiplication by 2 means shifting to the left, and multiplication by 3 means shifting to the left and then performing xor with the initial unshifted value. After shifting, a conditional xor with 0x1B should be performed if the shifted value is larger than 0xFF."

假设上面我已经实现了这个:

short gfMultiply(short h1, short h2)
{
//h1 can 0x01, 0x02 or 0x03
short r;
if(h1==0x01)
return h2;
if(h1==0x02)
r = (h2<<1);
else
r = (h2<<1)^h2;
if(r>0xFF)
r = r^0x1b;
return r;
}

但是当我测试时,结果不正确。我在这里做错了什么?

最佳答案

抱歉,有错误。我自己修好了,这是正确的:

short gfMultiply(short h1, short h2)
{
//h1 can 0x01, 0x02 or 0x03
short r;
if(h1==0x01)
return h2;
if(h1==0x02)
{
r = (h2<<1);
if(r>0xFF)
r = r^0x11b;
}
else
{
r = (h2<<1);
if(r>0xFF)
r = r^0x11b;
r = r^h2;
}
return r;
}

关于c - AES 混合列错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10766770/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com