Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/275.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何从128位密钥生成IDEA算法的解密密钥?_C#_Encryption_Cryptography_Key - Fatal编程技术网

C# 如何从128位密钥生成IDEA算法的解密密钥?

C# 如何从128位密钥生成IDEA算法的解密密钥?,c#,encryption,cryptography,key,C#,Encryption,Cryptography,Key,我试图用C#实现IDEA算法,只是想了解它是如何工作的。我获取了一个128位二进制密钥,并使用以下代码生成了52个加密密钥: static ushort[] getKeys(string binaryKey) { ushort[] keys = new ushort[52]; int index = 0; while (true) { int bitPos = 0; keys[inde

我试图用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;
}
静态ushort[]生成加密密钥(ushort[]加密密钥)
{
ushort[]decKeys=新ushort[52];
对于(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);
如果(i==52)中断;
decKeys[i++]=encKeys[52-i];
decKeys[i++]=encKeys[52-i];
}
返回甲板;
}

解密密钥计划以相反的顺序使用相同的52个密钥。

您必须按以下顺序使用密钥:

Encryption: K1       K2  K3     K4      K5  k6  (round 1)
Decryption: k49^-1   -k50 -k51  k49^-1  k47 k48 (round 2)

我假设你已经用输入数据块用
k1
k4
进行了模乘,并对
k2
k3
进行了模加,你说的“现在,我无法理解如何获得这些解密密钥”是什么意思?好吧,我的意思是“到目前为止做得很好”,我无法转发。通过“获取那些解密密钥”,我的意思是“生成解密密钥”。如果要使用相同的加密函数进行解密,并且密钥序列颠倒,则这似乎不起作用。