TRIPLEDES 填充无效且无法删除。由 C# 解密

TRIPLEDES padding is invalid and cannot be removed. c# decrypt

我在执行提供商以十六进制发送给我的 TRIPLEDES 中执行解密时遇到问题:EF69FF79BBD7E8E4EF69FF79BBD7E8E4 使用以下密钥“0123456789ABCDEFFEDCBA9876543210",应用以下方法:

 public IActionResult GetTokenTemp1()
        {

            TripleDESCryptoServiceProvider tDESalg = new TripleDESCryptoServiceProvider();
            MD5CryptoServiceProvider hashmd5 = new MD5CryptoServiceProvider();
            tDESalg.Key = hashmd5.ComputeHash(UTF8Encoding.UTF8.GetBytes("0123456789ABCDEFFEDCBA9876543210"));
            byte[] cipherBytes = Convert.FromBase64String("EF69FF79BBD7E8E4EF69FF79BBD7E8E4");
            string finalDecrypt = _3desTest.DecryptTextFromMemory(cipherBytes, tDESalg.Key, tDESalg.IV);
            return Ok(finalDecrypt);
        }
  public static string DecryptTextFromMemory(byte[] Data, byte[] Key, byte[] IV)
        {
            try
            {
                // Create a new MemoryStream using the passed
                // array of encrypted data.
                MemoryStream msDecrypt = new MemoryStream(Data);
                TripleDESCryptoServiceProvider de = new TripleDESCryptoServiceProvider();
               
                var descritor = de.CreateDecryptor(Key, IV);
           
                // Create a CryptoStream using the MemoryStream
                // and the passed key and initialization vector (IV).
                CryptoStream csDecrypt = new CryptoStream(msDecrypt,
                   descritor,
                    CryptoStreamMode.Read);
                
                // Create buffer to hold the decrypted data.
                byte[] fromEncrypt = new byte[Data.Length];

                // Read the decrypted data out of the crypto stream
                // and place it into the temporary buffer.
                csDecrypt.Read(fromEncrypt, 0, fromEncrypt.Length);
                string es = new UTF8Encoding().GetString(fromEncrypt);
                //Convert the buffer into a string and return it.
                return new UTF8Encoding().GetString(fromEncrypt);
            }
            catch (CryptographicException e)
            {
                Console.WriteLine("A Cryptographic error occurred: {0}", e.Message);
                return null;
            }
        }

当我将默认填充或任何其他填充保留为零或 none 时,我收到以下错误“添加无效且无法删除。”, 但是当我将填充保留为零或 none tripleDescryptorService.Padding = PaddingMode.None 时,我得到一种格式: padding.none

当我在这个页面上做的时候,我不知道该怎么做: https://neapay.com/online-tools/des-calculator.html?data=EF69FF79BBD7E8E4EF69FF79BBD7E8E4&key=0123456789ABCDEFFEDCBA9876543210&algo=3DES&decr=true

我得到了想要的结果。

我已经绝望了,我不是很擅长加密

非常感谢

该网站既不使用填充也不使用 IV。因此在代码中必须禁用填充并且必须应用 ECB 模式。

此外,该网站需要一个十六进制编码的密钥和密文,returns解密后的数据也是十六进制编码的,因此不能在代码中进行 UTF-8 解码:

public static byte[] DecryptTextFromMemory(byte[] encryptedData, byte[] key)
{
    using (TripleDESCryptoServiceProvider tripleDES = new TripleDESCryptoServiceProvider())
    {
        tripleDES.Key = key;
        tripleDES.Padding = PaddingMode.None;
        tripleDES.Mode = CipherMode.ECB;

        byte[] decryptedData = new byte[encryptedData.Length];
        using (MemoryStream msDecrypt = new MemoryStream(encryptedData))
        {
            ICryptoTransform decryptor = tripleDES.CreateDecryptor(tripleDES.Key, null);
            using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
            {
                csDecrypt.Read(decryptedData, 0, decryptedData.Length);
            }
        }

        return decryptedData;
    }
}

对于十六进制编码和解码,您可以使用任意方法,例如来自 here.

有了这个代码:

byte[] data = HexStringToByteArray("EF69FF79BBD7E8E4EF69FF79BBD7E8E4");
byte[] key = HexStringToByteArray("0123456789ABCDEFFEDCBA9876543210");
Console.WriteLine(ByteArrayToHexString(DecryptTextFromMemory(data, key)));

returns result of the website:

00000000003331720000000000333172

请注意:您最后一次更改没有用,因为它应用了与网站不一致的转换和算法。