[英]Why does C# and Java application calculate different MD5 values for the same file?
I have a C# project to verify a file's MD5.我有一个 C# 项目来验证文件的 MD5。 I use
System.Security.Cryptography.MD5
to calculate the MD5 in C#.我使用
System.Security.Cryptography.MD5
来计算 C# 中的 MD5。
But it is different from the MD5 in Java.但与Java中的MD5不同。
EDIT: I have found the c# code is correct one.编辑:我发现 c# 代码是正确的。 Thanks to Andy.
感谢安迪。 May I know how to correct the Java code?
我可以知道如何更正 Java 代码吗?
C# code: C# 代码:
public static String ComputeMD5(String fileName)
{
String hashMD5 = String.Empty;
if (System.IO.File.Exists(fileName))
{
using (System.IO.FileStream fs = new System.IO.FileStream(fileName, System.IO.FileMode.Open, System.IO.FileAccess.Read))
{
System.Security.Cryptography.MD5 calculator = System.Security.Cryptography.MD5.Create();
Byte[] buffer = calculator.ComputeHash(fs);
calculator.Clear();
StringBuilder stringBuilder = new StringBuilder();
for (int i = 0; i < buffer.Length; i++){
stringBuilder.Append(buffer[i].ToString("x2"));
}
hashMD5 = stringBuilder.ToString();
}
}
return hashMD5;
}
Java Code: Java 代码:
public static String ComputeMD5(File file) {
if (!file.isFile()) {
return null;
}
MessageDigest digest = null;
FileInputStream in = null;
byte buffer[] = new byte[1024];
int len;
try {
digest = MessageDigest.getInstance("MD5");
in = new FileInputStream(file);
while ((len = in.read(buffer, 0, 1024)) != -1) {
digest.update(buffer, 0, len);
}
in.close();
} catch (Exception e) {
e.printStackTrace();
return null;
}
return bytesToHexString(digest.digest());
}
Your bytesToHexString
function is wrong.您的
bytesToHexString
function 是错误的。 After using the function from here , in this complete example, I get the same result as the Linux md5sum
command and`onlinemd5.com as Andy suggested.从这里使用 function 后,在这个完整的示例中,我得到与 Linux
md5sum
命令和`onlinemd5.Z4D236D9A2D102C5FE6AD1C50DA4BEC5Z 建议的结果相同的结果。 The best way to handle this is to use a library, such as Apache Commons, that has a function to convert from bytes to a hex string.处理这个问题的最好方法是使用一个库,例如 Apache Commons,它有一个 function 从字节转换为十六进制字符串。 That way, you offload the work to get it right to a reputable library.
这样,您就可以将工作卸载到有信誉的图书馆。
import java.io.File;
import java.io.FileInputStream;
import java.security.MessageDigest;
public class Md5{
public static String computeMd5(File file) {
if (!file.isFile()) {
return null;
}
MessageDigest digest = null;
FileInputStream in = null;
byte buffer[] = new byte[1024];
int len;
try {
digest = MessageDigest.getInstance("MD5");
in = new FileInputStream(file);
while ((len = in.read(buffer, 0, 1024)) != -1) {
digest.update(buffer, 0, len);
}
in.close();
} catch (Exception e) {
e.printStackTrace();
return null;
}
return byteArrayToHex(digest.digest());
}
public static String byteArrayToHex(byte[] a) {
StringBuilder sb = new StringBuilder(a.length * 2);
for(byte b: a)
sb.append(String.format("%02x", b));
return sb.toString();
}
public static void main(String[] args){
System.out.println(computeMd5(new File("./text.txt")));
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.