如何在 C# 和 Java 中生成相同的 MD5 Hashcode? [英] How can you generate the same MD5 Hashcode in C# and Java?

查看:34
本文介绍了如何在 C# 和 Java 中生成相同的 MD5 Hashcode?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个在 C# 中生成 MD5 哈希的函数,如下所示:

I have a function that generates a MD5 hash in C# like this:

MD5 md5 = new MD5CryptoServiceProvider();
byte[] result = md5.ComputeHash(data);
StringBuilder sb = new StringBuilder();
for (int i = 0; i < result.Length; i++)
{
    sb.Append(result[i].ToString("X2"));
}
return sb.ToString();

在java中我的函数是这样的:

In java my function looks like this:

MessageDigest m = MessageDigest.getInstance("MD5");
m.update(bytes,0,bytes.length);

String hashcode = new BigInteger(1,m.digest()).toString(16);
return hashcode;

虽然 C# 代码生成:02945C9171FBFEF0296D22B0607D522D",但 Java 代码生成:5a700e63fa29a8eae77ebe0443d59239".

While the C# code generates: "02945C9171FBFEF0296D22B0607D522D" the java codes generates: "5a700e63fa29a8eae77ebe0443d59239".

有没有办法为同一个字节数组生成相同的 md5 哈希?

Is there a way to generate the same md5 hash for the same bytearray?

按需:

这是java中的测试代码:

This is the testcode in java:

File file = new File(System.getProperty("user.dir") + "/HashCodeTest.flv");
byte[] bytes = null;
try {
    bytes = FileUtils.getBytesFromFile(file);
} catch (IOException e) {
    fail();
}
try {
    generatedHashCode = HashCode.generate(bytes);
} catch (NoSuchAlgorithmException e) {
    fail();
}

这是我的 C# 代码

var blob = GetBlobByHttpPostedFile(httpPostedFile);
var hashCode = Md5Factory.ConvertByteArray(blob);

private static byte[] GetBlobByHttpPostedFile(HttpPostedFile httpPostedFile)
{
    var contentLength = httpPostedFile.ContentLength; 
    var result = new byte[contentLength];
    var inputStream = httpPostedFile.InputStream;
    inputStream.Read(result, 0, contentLength);

    return result;
}

干杯

推荐答案

那应该没问题 - 尽管您可以通过调用使 Java 代码更简单

That should be fine - although you could make the Java code simpler by just calling

byte[] digest = m.digest(bytes);

而不是调用update然后digest.

绝对确定在这两种情况下都获得了相同的数据吗?你能用相同的硬编码数据发布显示这个失败的示例程序吗?

Are you absolutely sure you've got the same data in both cases? Could you post sample programs showing this failing with the same hard-coded data?

这是我想到的那种测试.这两个程序给出了相同的结果:

Here's the sort of test I was thinking of. These two programs give the same result:

C#:

using System;
using System.Security.Cryptography;
using System.Text;

class Test
{
    static void Main()
    {
        byte[] bytes = { 0x35, 0x24, 0x76, 0x12 };
        MD5 md5 = new MD5CryptoServiceProvider();
        byte[] result = md5.ComputeHash(bytes);
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < result.Length; i++)
        {
            sb.Append(result[i].ToString("x2"));
        }
        Console.WriteLine(sb);
    }
}

Java:

import java.math.BigInteger;
import java.security.MessageDigest;

public class Test
{
    public static void main(String[] args) throws Exception
    {
        byte[] bytes = { 0x35, 0x24, 0x76, 0x12 };
        MessageDigest m = MessageDigest.getInstance("MD5");
        byte[] digest = m.digest(bytes);
        String hash = new BigInteger(1, digest).toString(16);
        System.out.println(hash);
    }
}

这篇关于如何在 C# 和 Java 中生成相同的 MD5 Hashcode?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆