生成 JAVA MD5 哈希以匹配 C# MD5 哈希。

huangapple go评论85阅读模式
英文:

Make JAVA MD5 hash match C# MD5 hash

问题

以下是您要翻译的内容:

我的工作是将一堆 Java 代码重写为 C#。
这是 JAVA 代码:

public static String CreateMD5(String str) {
    try {
        byte[] digest = MessageDigest.getInstance("MD5").digest(str.getBytes("UTF-8"));
        StringBuffer stringBuffer = new StringBuffer();
        for (byte b : digest) {
            // 我无法理解这里的部分
            stringBuffer.append(Integer.toHexString((b & 255) | 256).substring(1, 3));
        }
        return stringBuffer.toString();
    } catch (UnsupportedEncodingException | NoSuchAlgorithmException unused) {
        return null;
    }
}

好的。正如您所看到的,这段代码试图生成 MD5 哈希。但我无法理解的是我展示的那部分。
我尝试在 C# 中重写此 JAVA 代码:

public static string CreateMD5(string input)
{
    // 使用输入字符串计算 MD5 哈希
    using (System.Security.Cryptography.MD5 md5 = System.Security.Cryptography.MD5.Create())
    {
        byte[] inputBytes = System.Text.Encoding.ASCII.GetBytes(input);
        byte[] hashBytes = md5.ComputeHash(inputBytes);

        // 将字节数组转换为十六进制字符串
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < hashBytes.Length; i++)
        {
            sb.Append(hashBytes[i].ToString("X2"));
        }
        return sb.ToString();
    }
}

嗯,这两段代码都生成 MD5 哈希字符串,但结果是不同的。

英文:

My job is to rewrite a bunch of Java codes is C#.
This is the JAVA code:

        public static String CreateMD5(String str) {
    try {
        byte[] digest = MessageDigest.getInstance(&quot;MD5&quot;).digest(str.getBytes(&quot;UTF-8&quot;));
        StringBuffer stringBuffer = new StringBuffer();
        for (byte b : digest) {
    // i can not understand here
            stringBuffer.append(Integer.toHexString((b &amp; 255) | 256).substring(1, 3));
        }
        return stringBuffer.toString();
    } catch (UnsupportedEncodingException | NoSuchAlgorithmException unused) {
        return null;
    }
}

Ok.As you can see this code is trying to make MD5 hash.But the thing i can not understand is the part that i have shown.
I tried this code in C# to rewrite this JAVA code:

    public static string CreateMD5(string input)
    {
// Use input string to calculate MD5 hash
using (System.Security.Cryptography.MD5 md5 = System.Security.Cryptography.MD5.Create())
{
    byte[] inputBytes = System.Text.Encoding.ASCII.GetBytes(input);
    byte[] hashBytes = md5.ComputeHash(inputBytes);

    // Convert the byte array to hexadecimal string
    StringBuilder sb = new StringBuilder();
    for (int i = 0; i &lt; hashBytes.Length; i++)
    {
        sb.Append(hashBytes[i].ToString(&quot;X2&quot;));
    }
    return sb.ToString();
}
    }

Well both codes are making MD5 hash strings but the results are different.

答案1

得分: 1

两段代码片段之间的编码有所不同 - 你展示的Java代码使用了UTF-8,但是你的C#代码使用了ASCII。这会导致不同的MD5哈希计算。

将你的C#代码从以下内容进行修改:

byte[] inputBytes = System.Text.Encoding.ASCII.GetBytes(input);

修改为:

byte[] inputBytes = System.Text.Encoding.UTF8.GetBytes(input);

只要没有其他代码转换错误,这应该&#8482;可以解决你的问题。

英文:

There is a difference in encoding between the two code snippets you've shown - your Java code uses UTF-8, but your C# code uses ASCII. This will result in a different MD5 hash computation.

Change your C# code from:

byte[] inputBytes = System.Text.Encoding.ASCII.GetBytes(input);

to:

byte[] inputBytes = System.Text.Encoding.UTF8.GetBytes(input);

This should&#8482; fix your problem, provided there are no other code conversion errors.

huangapple
  • 本文由 发表于 2020年8月19日 02:42:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/63474790.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定