MD5哈希值不同

发布于 2024-10-05 12:39:34 字数 1144 浏览 3 评论 0原文

我不知道如何实现从 php 到 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;

好吧,我尝试将它转换,但我得到了不同的结果!

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中的结果不同..

你能帮我吗? 先感谢您 :)

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();
}

result in java is different from result in php..

Can you help me please??
Thank you in advance :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

本王不退位尔等都是臣 2024-10-12 12:39:34

不转换成so字符串就不能直接使用seq20吗?
我会这样做:

md.update( seq20 ); 
byte[] md5sum = md.digest();
BigInteger bigInt = new BigInteger(1, md5sum);
output = bigInt.toString(16);
while ( output.length() < 32 ) {
    output = "0"+output;
}

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;
}
情何以堪。 2024-10-12 12:39:34

其他两个答案都不是肯定错误的,但从优雅的角度来看,请考虑以下

String MD5(String... strings) {
    MessageDigest md = null;
    try {
        md = MessageDigest.getInstance("MD5");
        for(final String s : strings) {
            md.update(s.getBytes());
        }
    } catch (NoSuchAlgorithmException ex) {
        throw new RuntimeException("MD5 Cryptography Not Supported");
    }
    final BigInteger bigInt = new BigInteger(1, md.digest());
    return String.format("%032x", bigInt);
}

编辑: String... varargs 的使用是完全可选的,但它使得该函数更容易一些,因为它避免了调用函数中字符串连接的开销。

Neither of the other two answers are affirmatively wrong, but from an elegance standpoint, consider the following

String MD5(String... strings) {
    MessageDigest md = null;
    try {
        md = MessageDigest.getInstance("MD5");
        for(final String s : strings) {
            md.update(s.getBytes());
        }
    } catch (NoSuchAlgorithmException ex) {
        throw new RuntimeException("MD5 Cryptography Not Supported");
    }
    final BigInteger bigInt = new BigInteger(1, md.digest());
    return String.format("%032x", bigInt);
}

edit: The use of String... varargs is totally optional, but it makes the function a little easier, as it avoids the overhead of string concatenation in the calling function.

彼岸花ソ最美的依靠 2024-10-12 12:39:34

接受的解决方案:

md.update( seq20 ); 
byte[] md5sum = md.digest();
BigInteger bigInt = new BigInteger(1, md5sum);
output = bigInt.toString(16);
if ( output.length() == 31 ) {
    output = "0"+output;
}

包含一个将失败 1/256 次的实现,因为 md5sum 的输出可以有多个前导零。引发此错误的 md5 输入示例为:“15446:68106”(不带引号)。

我建议使用 apache commons DigestUtils.md5Hex 如果您需要与 php 实现匹配的 md5。

The accepted solution:

md.update( seq20 ); 
byte[] md5sum = md.digest();
BigInteger bigInt = new BigInteger(1, md5sum);
output = bigInt.toString(16);
if ( output.length() == 31 ) {
    output = "0"+output;
}

contains an implemention that will fail 1/256 times because the output of md5sum can have more than one leading zero. An example md5 input that will elicit this error is: "15446:68106" (without quotes).

I recommend using the apache commons DigestUtils.md5Hex if you need a md5 that matches the php implementation.

蹲墙角沉默 2024-10-12 12:39:34

我的猜测是 PHP 正在将上面的内容评估为字符串,而不是十六进制。 Java 正在按照您的预期进行操作。

My guess is PHP is evaluating the above as a string, not hex. And Java is doing it as you expect it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文