简体   繁体   English

Java md5,PHP方式

[英]Java md5, the PHP way

I have been working on this for hours, but I can't get it to work. 我已经为此工作了几个小时,但我无法使其正常工作。

Basically I am developing a REST client in Java for a REST server in PHP. 基本上,我正在为Java中的REST服务器开发Java REST客户端。 Both the client and the server have to compute the md5 of a string and the server will compare them for authentication (kinda). 客户端和服务器都必须计算字符串的md5,服务器将比较它们的身份验证(kinda)。

On the server, the PHP code is: 在服务器上,PHP代码为:

md5("getTokenapi_keybf8ddfs845jhre980543jhsjfro93fd8capi_ver1tokeniud9ER£jdfff");

that generates: 会产生:

4d7b2e42c3dfd11de3e77b9fe2211b87

Nice! 真好!

Here is the code for the client: 这是客户端的代码:

import java.security.*;
....
String s = "getTokenapi_keybf8ddfs845jhre980543jhsjfro93fd8capi_ver1tokeniud9ER£jdfff";
byte[] bytesOfMessage = s.getBytes("UTF-8");
MessageDigest md = MessageDigest.getInstance("MD5");
byte[] thedigest = md.digest(bytesOfMessage);    

System.out.println("String2: " + thedigest);        
System.out.println("String3: " + new String(thedigest));

That generates: 产生:

String2: [B@42e816
String3: M{.B�����{��!�

How can I get Java to compute the md5 sum the same way PHP does, please? 请问我如何像Java一样用Java来计算md5和?

Thanks, Dan 谢谢,丹

Give this a try: 试试看:

public static String md5(String input) throws NoSuchAlgorithmException {
    String result = input;
    if(input != null) {
        MessageDigest md = MessageDigest.getInstance("MD5"); //or "SHA-1"
        md.update(input.getBytes());
        BigInteger hash = new BigInteger(1, md.digest());
        result = hash.toString(16);
        while(result.length() < 32) { //40 for SHA-1
            result = "0" + result;
        }
    }
    return result;
}

code from http://web.archive.org/web/20140209230440/http://www.sergiy.ca/how-to-make-java-md5-and-sha-1-hashes-compatible-with-php-or-mysql/ 来自http://web.archive.org/web/20140209230440/http://www.sergiy.ca/how-to-make-java-md5-and-sha-1-hashes-compatible-with-php-的代码or-mysql /

Found myself: 发现自己:

import java.math.BigInteger;
..
public static String md5(String input) throws NoSuchAlgorithmException {
        String result = input;
        if(input != null) {
            MessageDigest md = MessageDigest.getInstance("MD5"); //or "SHA-1"
            md.update(input.getBytes());
            BigInteger hash = new BigInteger(1, md.digest());
            result = hash.toString(16);
            if ((result.length() % 2) != 0) {
                result = "0" + result;
            }
        }
        return result;
    }

Source: http://www.sergiy.ca/how-to-make-java-md5-and-sha-1-hashes-compatible-with-php-or-mysql/ 来源: http//www.sergiy.ca/how-to-make-java-md5-and-sha-1-hashes-compatible-with-php-or-mysql/

You are outputting the raw md5 output, which is just a bunch of bytes. 您正在输出原始的md5输出,它只是一堆字节。 You would get the same result in php if you said md5("some string", true). 如果您说md5(“ some string”,true),您将在php中获得相同的结果。

You need to convert the bytes to ascii characters instead. 您需要将字节转换为ASCII字符。

if you use spring security framework , just do : 如果您使用spring安全框架,请执行以下操作:

import org.springframework.security.authentication.encoding.*

new Md5PasswordEncoder().encodePassword("myWord",null)

The same result as PHP::md5() . 结果与PHP::md5() I confirm 我确定

See more examples 查看更多示例

You need to convert the result into the HEX representation. 您需要结果转换为十六进制表示形式。 This is how it is done in Fast MD5 library : 这是在Fast MD5库中完成的操作:

    private static final char[] HEX_CHARS = { '0', '1', '2', '3', '4', '5',
            '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f', };

    /**
     * Turns array of bytes into string representing each byte as unsigned hex
     * number.
     * 
     * @param hash
     *            Array of bytes to convert to hex-string
     * @return Generated hex string
     */
    public static String asHex(byte hash[]) {
        char buf[] = new char[hash.length * 2];
        for (int i = 0, x = 0; i < hash.length; i++) {
            buf[x++] = HEX_CHARS[(hash[i] >>> 4) & 0xf];
            buf[x++] = HEX_CHARS[hash[i] & 0xf];
        }
        return new String(buf);
    }

So you will need to call System.out.println("String3: " + asHex(thedigest)); 因此,您需要调用System.out.println("String3: " + asHex(thedigest));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM