【发布时间】:2012-05-26 13:44:18
【问题描述】:
现在我正在学习密码学。所以,(为了练习和娱乐),我决定实施 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。但要么我无法理解某些观点,要么我做错了什么。在维基百科中我读过这个:
"乘法运算定义为:乘以1表示 没有变化,乘以 2 意味着向左移动,并且 乘以 3 表示向左移动然后执行异或 具有初始未移位值。移位后,条件异或 如果移位值大于 0x1B,则应执行 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;
}
但我在测试时结果不正确。我在这里做错了什么?
【问题讨论】:
-
如果您实现密码是为了自己的娱乐,那么 RC4 比 AES 更容易编码。
标签: c encryption cryptography aes