Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/360.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/335.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
Python System.Security.Cryptography.CryptographyException:&x27;坏数据';-DES加密_Python_C#_Encryption_Cryptography_Des - Fatal编程技术网

Python System.Security.Cryptography.CryptographyException:&x27;坏数据';-DES加密

Python System.Security.Cryptography.CryptographyException:&x27;坏数据';-DES加密,python,c#,encryption,cryptography,des,Python,C#,Encryption,Cryptography,Des,我正在将一些代码从Python转换为C#(.NETFramework4.8,VS2019,Win10)。首先,我使用DES CBC对数据进行加密,然后使用DES ECB对其进行解密。在中间,我得到了坏数据的例外。 我看到它可能连接到另一个键,但我不排除相同的结果,所以我不知道是什么导致它 我无法比较结果,因为每次数据变量都是随机的 键(cmacKey)是15522d4fca86042d,数据是93450567@ïïïpreactivi0000 private byte[] CBCMAC3DES(

我正在将一些代码从Python转换为C#(.NETFramework4.8,VS2019,Win10)。首先,我使用DES CBC对数据进行加密,然后使用DES ECB对其进行解密。在中间,我得到了坏数据的例外。 我看到它可能连接到另一个键,但我不排除相同的结果,所以我不知道是什么导致它

我无法比较结果,因为每次数据变量都是随机的

键(cmacKey)是
15522d4fca86042d
,数据是
93450567@ïïïpreactivi0000

private byte[] CBCMAC3DES(string cmacKey, string data){
        // pad by zeroes to multiple of eight
        string workDataStr = data;
        while ((workDataStr.Length / 2) % 8 != 0)
            workDataStr += "00";

        byte[] clearBytes = Str2byteArr(workDataStr);

        DESCryptoServiceProvider des = new DESCryptoServiceProvider();
        des.KeySize = 64;
        des.Mode = CipherMode.CBC;
        // taking only 8 chars from the key, convert to ascii and to byte array
        des.Key = Str2byteArr(HexStringToAsciString(cmacKey).Substring(0, 8));
        des.IV = new byte[8] { 0, 0, 0, 0, 0, 0, 0, 0 };

        ICryptoTransform ct = des.CreateEncryptor();
        byte[] resultArray = ct.TransformFinalBlock(clearBytes, 0, clearBytes.Length);
        byte[] lastBlock = new byte[8];
        for (int i = 0; i < 8; i++)
            lastBlock[i] = resultArray[resultArray.Length - (8 - i)];

        // Decryption
        des.Mode = CipherMode.ECB;
        string s = cmacKey.Substring(8, 8);
        byte[] keyTmpArr = Str2byteArr(s);
        des.Key = keyTmpArr;
        ct = des.CreateDecryptor();
        lastBlock = ct.TransformFinalBlock(lastBlock, 0, lastBlock.Length);    // THE EXCEPTION

        des.Key = Str2byteArr(cmacKey.Substring(0, 8));
        ct = des.CreateEncryptor();
        lastBlock = ct.TransformFinalBlock(lastBlock, 0, lastBlock.Length);
        return lastBlock;

    }

提前感谢您在不同系统之间工作,常见的问题是系统之间的默认值不匹配。不要使用默认值,指定所有内容,以便它们在两侧都匹配。在这种情况下,它可能意味着确保C#代码与Python默认值匹配。如果事情不完全匹配,加密就会失败。阅读Python文档和代码,了解如何设置C端。
    public static byte[] Str2byteArr(string str)
    {
        byte[] ba = new byte[str.Length];
        for (int i = 0; i < str.Length; i++) ba[i] = Convert.ToByte(str[i]);
        return ba;
    }
    public string HexStringToAsciString(string hexString)
    {
        string ascii = string.Empty;
        for (int i = 0; i < hexString.Length; i += 2)
        {
            String hs = string.Empty;

            hs = hexString.Substring(i, 2);
            uint decval = System.Convert.ToUInt32(hs, 16);
            char character = System.Convert.ToChar(decval);
            ascii += character;

        }
        return ascii;
    }
def retail_mac(macKey, data, iv='\0'*8):
    workDataStr = pad_by_zeroes_to_multiple_of_eight(data)
    des = DES.new(str(macKey[0:8]), DES.MODE_CBC, IV = iv)
    res = des.encrypt(workDataStr)
    lastBlock = res[-8:]
    des = DES.new(str(macKey[8:16]), DES.MODE_ECB)
    lastBlock = des.decrypt(lastBlock)
    des = DES.new(str(macKey[0:8]), DES.MODE_ECB)
    return des.encrypt(lastBlock)

def pad_by_zeroes_to_multiple_of_eight(data):
    paddingSize = (8 - len(data)) % 8
    workData = bytearray(data)
    workData.extend('\0'*paddingSize)
    workDataStr = str(workData)
    return workDataStr