TripleDes 解密,开头有一些无效数据

发布于 2025-01-04 14:13:07 字数 1991 浏览 0 评论 0原文

我正在尝试使用 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>   "

I am trying to decrypt data using tripleDes. Everything looks fine but it has some invalid characters at the beginning? What am I doing wrong? For same data if call this function again and again these first few characters are always different but the rest of the data is same.

I am passing useHashing to 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;
    }

This is what I get:

"�{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>   "

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

黯然 2025-01-11 14:13:07

我认为如果未设置,C# 类将使用随机 IV。尝试将 IV 设置为 8 字节、值为 00h 字节的字节数组,并尝试用它进行解密。如果这不起作用,您将不得不以某种方式取回静脉注射。

PS,正确的方法当然是从 tdes 实例请求块大小,而不是输入文字 8

I think that the C# classes use a random IV if it is not set. Try to set the IV to a byte array of 8 bytes valued 00h bytes and try to decrypt with that. If that does not work, you will have to retrieve the IV somehow.

PS the right way is of course to request the blocksize from the tdes instance instead of putting in the literal 8

寻梦旅人 2025-01-11 14:13:07

我回顾了您的帖子,发现您正在尝试解密 SAML 断言。 SAML 2 包含在 WIF 中,因此我会考虑下载并使用它。抽象比重新发明轮子更容易使用。

至于我给出的第一个答案和其他人的评论:@Henk Holterman 关注的事实是加密机制同时使用密钥(转换为字节)和初始化向量(IV)。如果使用“标准”以外的 IV(即已指定),则必须匹配它。

由于这是 SAML,因此请重点解密 SAML,以便您可以应用断言。您可以使用自定义代码来执行此操作,但您的搜索应重点关注 SAML。

I was looking back over your post and saw you are trying to decrypt a SAML assertion. SAML 2 is included in WIF, so I would consider downloading and using it. The abstraction is much easier to use than trying to reinvent the wheel.

As for the first answer I gave and comments from others: @Henk Holterman is focusing on the fact that an encryption mechanism uses botha key (converted to bytes) and an Initialization Vector (IV). If an IV other than "standard" is used (ie, it was specified), you have to match it.

As this is SAML, focus on decrypting SAML so you can apply the assertion. You can do this with custom code, but your searching should focus on SAML.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文