3DES - 在 C# 中解密加密文本(通过 JAVA)

发布于 2024-10-16 13:25:29 字数 1980 浏览 1 评论 0原文

情况是这样的:

  1. 加密文本是用JAVA完成的(我们根本没有JAVA背景)
  2. 方法是3DES
  3. 填充的是PKCS#5
  4. Base 64

解密将用C#,代码如下:

    public static string DecryptString(string Message, string Passphrase)
    {
        byte[] Results;
        UTF8Encoding UTF8 = new UTF8Encoding();

        MD5CryptoServiceProvider HashProvider = new MD5CryptoServiceProvider();
        byte[] TDESKey = HashProvider.ComputeHash(UTF8.GetBytes(Passphrase));

        TripleDESCryptoServiceProvider TDESAlgorithm = new TripleDESCryptoServiceProvider();

        TDESAlgorithm.Key = TDESKey;
        TDESAlgorithm.Mode = CipherMode.ECB;
        TDESAlgorithm.Padding = PaddingMode.PKCS7;

        byte[] DataToDecrypt = Convert.FromBase64String(Message);

        try
        {
            ICryptoTransform Decryptor = TDESAlgorithm.CreateDecryptor();
            Results = Decryptor.TransformFinalBlock(DataToDecrypt, 0, DataToDecrypt.Length);
        }
        finally
        {
            TDESAlgorithm.Clear();
            HashProvider.Clear();
        }

        return UTF8.GetString(Results);
    }

但是,当尝试解密,收到错误消息: BAD DATA

我在哪里丢失了?

提前致谢。

添加,加密的工作原理如下:

<cffunction name="getToken" returntype="String" output="false">
    <cfscript>
        plainText = getPlainText();
        rawSecretKey = CreateObject("java","sun.misc.BASE64Decoder").decodeBuffer(variables.encryptionKey);

        secretKeySpec = CreateObject("java","javax.crypto.spec.SecretKeySpec").init(rawSecretKey,"DESEDE");

        cipher = CreateObject("java","javax.crypto.Cipher").getInstance("DESEDE");
        cipher.init(Cipher.ENCRYPT_MODE, secretkeySpec);

        encrypted = cipher.doFinal(plainText.getBytes()); // a byte array (a binary in CF)

        return URLEncodedFormat(ToString(ToBase64(encrypted)));
    </cfscript>
</cffunction>

更新: 这个问题已经解决了。问题是密钥需要从 Base64 转换。

Here is the situation:

  1. The encrypted text is done in JAVA (which we have no JAVA background at all)
  2. The method is 3DES
  3. The padded is PKCS#5
  4. Base 64

The decryption will be in C#, and here is the code:

    public static string DecryptString(string Message, string Passphrase)
    {
        byte[] Results;
        UTF8Encoding UTF8 = new UTF8Encoding();

        MD5CryptoServiceProvider HashProvider = new MD5CryptoServiceProvider();
        byte[] TDESKey = HashProvider.ComputeHash(UTF8.GetBytes(Passphrase));

        TripleDESCryptoServiceProvider TDESAlgorithm = new TripleDESCryptoServiceProvider();

        TDESAlgorithm.Key = TDESKey;
        TDESAlgorithm.Mode = CipherMode.ECB;
        TDESAlgorithm.Padding = PaddingMode.PKCS7;

        byte[] DataToDecrypt = Convert.FromBase64String(Message);

        try
        {
            ICryptoTransform Decryptor = TDESAlgorithm.CreateDecryptor();
            Results = Decryptor.TransformFinalBlock(DataToDecrypt, 0, DataToDecrypt.Length);
        }
        finally
        {
            TDESAlgorithm.Clear();
            HashProvider.Clear();
        }

        return UTF8.GetString(Results);
    }

However, when tried to decrypt, got the error message: BAD DATA

Where am I missing here?

Thanks in advance.

Added, and here's how the encryption works:

<cffunction name="getToken" returntype="String" output="false">
    <cfscript>
        plainText = getPlainText();
        rawSecretKey = CreateObject("java","sun.misc.BASE64Decoder").decodeBuffer(variables.encryptionKey);

        secretKeySpec = CreateObject("java","javax.crypto.spec.SecretKeySpec").init(rawSecretKey,"DESEDE");

        cipher = CreateObject("java","javax.crypto.Cipher").getInstance("DESEDE");
        cipher.init(Cipher.ENCRYPT_MODE, secretkeySpec);

        encrypted = cipher.doFinal(plainText.getBytes()); // a byte array (a binary in CF)

        return URLEncodedFormat(ToString(ToBase64(encrypted)));
    </cfscript>
</cffunction>

Update:
This issue has been resolved. The problem was that the key needed to be converted from Base64.

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

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

发布评论

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

评论(1

攀登最高峰 2024-10-23 13:25:29

答案:

而不是:

byte[] TDESKey = HashProvider.ComputeHash(UTF8.GetBytes(Passphrase));

这样做:

byte[] TDESKey = Convert.FromBase64String(Passphrase);

这解决了这个问题。

The answer:

Instead of:

byte[] TDESKey = HashProvider.ComputeHash(UTF8.GetBytes(Passphrase));

Do this:

byte[] TDESKey = Convert.FromBase64String(Passphrase);

That solves this issue.

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