我正在尝试使用 tripleDes 解密数据。一切看起来都很好,但它的开头有一些无效字符?我究竟做错了什么?对于相同的数据,如果多次调用此函数,前几个字符总是不同的,但其余数据是相同的。
我将 useHashing 传递给 false。
public static byte[] GetTripleDesDecryption(string dataToDecrypt, byte[] key, bool useHashing)
{
byte[] keyArray;
byte[] plainbytes = null;
byte[] cipherbytes;
try
{
if (useHashing)
{
MD5CryptoServiceProvider hashmd5 = new MD5CryptoServiceProvider();
keyArray = hashmd5.ComputeHash(key);
hashmd5.Clear();
}
else
keyArray = key;
using (TripleDESCryptoServiceProvider tdes = new TripleDESCryptoServiceProvider())
{
tdes.Key = keyArray;
tdes.Mode = CipherMode.CBC;
tdes.Padding = PaddingMode.None;
using (ICryptoTransform cTransform = tdes.CreateDecryptor())
{
cipherbytes = Convert.FromBase64String(dataToDecrypt);
plainbytes = cTransform.TransformFinalBlock(cipherbytes, 0, cipherbytes.Length);
}
}
}
catch (Exception e)
{
LogMessage(e.Message + " Attribute Parsing error. DataToDecrypt = " + dataToDecrypt);
throw e;
}
return plainbytes;
}
这是我得到的:
"�{c��]�sertion xmlns:saml=\"urn:oasis:names:tc:SAML:2.0:assertion\"><saml:AttributeStatement><saml:Attribute Name=\"userID\"><saml:AttributeValue>456</saml:AttributeValue></saml:Attribute><saml:Attribute Name=\"financialInstitutionNumber\"><saml:AttributeValue>303986258</saml:AttributeValue></saml:Attribute><saml:Attribute Name=\"password\"><saml:AttributeValue>galaxy</saml:AttributeValue></saml:Attribute></saml:AttributeStatement></saml:Assertion> "
最佳答案
我认为 C# 类在未设置的情况下使用随机 IV。尝试将 IV 设置为 8 个字节的字节数组,值为 00h 字节,并尝试用它解密。如果这不起作用,您将不得不以某种方式检索 IV。
PS 正确的方法当然是从 tdes 实例请求 blocksize 而不是放入文字 8
关于c# - 开头带有一些无效数据的 TripleDes 解密,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9231460/