MD5哈希是不同的 [英] MD5 hash is different
本文介绍了MD5哈希是不同的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
I don't know how to realize these few lines from php to java..
$varInHex = "\x22\x33\xAd\xB5\x2b\xE6\x22\x33\x12\x36\x22\x31\xCA\x22\x11\x41\x62\x21\x22\x01\x55\x22\x71\x42\x10\x36";<br/><br/>
$result = md5($varInHex);
echo $result;
好的,我试图转换它,但我得到了不同的结果!
Well, I tried to convert it but I'm getting a different result!
byte[] seq20 = new byte[]{(byte)0x22,(byte)...etc...};
String str = seq20.toString();
String result = md5(str);
System.out.println(result);
public static String md5(String source) {
try {
MessageDigest md = MessageDigest.getInstance("MD5");
byte[] bytes = md.digest(source.getBytes("UTF-8"));
return getString(bytes);
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
private static String getString(byte[] bytes) {
StringBuffer sb = new StringBuffer();
for (int i = 0; i < bytes.length; i++) {
byte b = bytes[i];
String hex = Integer.toHexString((int) 0x00FF & b);
if (hex.length() == 1) {
sb.append("0");
}
sb.append(hex);
}
return sb.toString();
}
导致java与php中的结果不一样。
result in java is different from result in php..
你能帮助我吗?
预先感谢您:)
Can you help me please?? Thank you in advance :)
推荐答案
不能直接使用seq20而不将其转换为字符串?
我会这样做:
Can't you use seq20 directly without converting it so string? I would do it this way:
md.update( seq20 );
byte[] md5sum = md.digest();
BigInteger bigInt = new BigInteger(1, md5sum);
output = bigInt.toString(16);
while ( output.length() < 32 ) {
output = "0"+output;
}
这篇关于MD5哈希是不同的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文