那应该很好-尽管您可以通过调用来简化Java代码
byte[] digest = m.digest(bytes);
而不是调用
update然后
digest。
您是否 绝对确定 在两种情况下都具有相同的数据?您能否发布示例程序,以显示相同的硬编码数据导致此操作失败?
编辑:这是我在想的那种测试。这两个程序给出相同的结果:
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); }}


