我正在尝试在 C# 中实现 IDEA 算法,只是为了了解它是如何工作的。我采用了 128 位二进制密钥并使用以下代码生成了 52 个加密密钥:
static ushort[] getKeys(string binaryKey)
{
ushort[] keys = new ushort[52];
int index = 0;
while (true)
{
int bitPos = 0;
keys[index++] = Convert.ToUInt16(binaryKey.Substring(bitPos, 16), 2);
bitPos += 16;
keys[index++] = Convert.ToUInt16(binaryKey.Substring(bitPos, 16), 2);
bitPos += 16;
keys[index++] = Convert.ToUInt16(binaryKey.Substring(bitPos, 16), 2);
bitPos += 16;
keys[index++] = Convert.ToUInt16(binaryKey.Substring(bitPos, 16), 2);
bitPos += 16;
if (index == 52)
break;
keys[index++] = Convert.ToUInt16(binaryKey.Substring(bitPos, 16), 2);
bitPos += 16;
keys[index++] = Convert.ToUInt16(binaryKey.Substring(bitPos, 16), 2);
bitPos += 16;
keys[index++] = Convert.ToUInt16(binaryKey.Substring(bitPos, 16), 2);
bitPos += 16;
keys[index++] = Convert.ToUInt16(binaryKey.Substring(bitPos, 16), 2);
bitPos += 16;
binaryKey = binaryKey.Substring(25) + binaryKey.Substring(0, 25);
}
return keys;
}
我相信这个函数会返回正确的值(我无法测试它们,但它们在界限内)。现在,我无法理解如何获得这些解密密钥。我也找不到足够的关于此事的文字。
编辑:这是我用来生成解密密钥的方法 -
static ushort[] generateDecryptionKeys(ushort[] encKeys)
{
ushort[] decKeys = new ushort[52];
for (int i = 0; i < 52; )
{
decKeys[i++] = (ushort)GetModMulInv(encKeys[52 - i], 65537);
decKeys[i++] = (ushort)AdditiveInv(encKeys[52 - i]);
decKeys[i++] = (ushort)AdditiveInv(encKeys[52 - i]);
decKeys[i++] = (ushort)GetModMulInv(encKeys[52 - i], 65537);
if (i == 52) break;
decKeys[i++] = encKeys[52 - i];
decKeys[i++] = encKeys[52 - i];
}
return decKeys;
}