【发布时间】:2016-11-17 16:39:40
【问题描述】:
为了构建 AES 的实现,我正在尝试在 Galois 域 256 中实现按位乘法的方法。我目前的乘法方法如下:
public static int multiplyPolynomials(int n, int m)
{
int result = 0x00000000;
String ns = toBitString(n);
String ms = toBitString(m);
for (int i = 0; i < ns.length(); i++)
{
if (ns.charAt(i) == '1')
{
/*
* If there's a 1 at place i, add the value of m left-shifted i places to the result.
*/
int temp = m;
for (int j = 0; j < i; j++) { temp = temp << 1; }
result += temp;
}
}
return result;
}
toBitString(int n) 方法纯粹是Integer.toBinaryString(int n) 的快捷方式。
给定输入(0x0000ef00, 2),这个函数的输出是494(应该是478)。打印对toBitString(0x0000ef00) 的直接调用确认该函数的输出符合预期(在本例中为 1110111100000000)。如果第一个输入向右移动一个字节(0x000000ef),输出仍然是 494。
使用上述输入,ns 的值为1110111100000000,result 的位串等价物为111101110。 ns 因此是正确的。
上面的方法有什么错误?
【问题讨论】:
-
所以据我了解,您遇到的问题是 ms,但我看不到它的任何用法!另外请您解释一下计算结果背后的机制是什么?
-
ms 实际上此时并未使用。据我所知,问题一定出在最里面的
for循环中,但我不知道它是什么。 (虽然我刚刚意识到使用<=是一个问题,因为它转换的次数太多了。)
标签: java bit-manipulation