首页 > 解决方案 > TRIPLEDES 填充无效且无法删除。c# 解密

问题描述

我在 TRIPLEDES 中执行解密时遇到问题,提供商以十六进制格式发送给我:EF69FF79BBD7E8E4EF69FF79BBD7E8E 4 使用以下密钥“ 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;
            }
        }

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

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

我得到了想要的结果。

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

太感谢了

标签: c#.netasp.net-core-3.1tripledes

解决方案


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

此外,该网站需要一个十六进制编码的密钥和密文,并返回同样是十六进制编码的解密数据,因此不得在代码中进行 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;
    }
}

对于十六进制编码和解码,您可以使用任意方法,例如从这里

有了这个代码:

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

返回网站的结果

00000000003331720000000000333172

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


推荐阅读