Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java md5, the PHP way

Tags:

java

md5

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. Both the client and the server have to compute the md5 of a string and the server will compare them for authentication (kinda).

On the server, the PHP code is:

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?

Thanks, Dan

like image 383
Dan Avatar asked Nov 15 '10 11:11

Dan


People also ask

How does MD5 work in PHP?

The md5() function uses the RSA Data Security, Inc. MD5 Message-Digest Algorithm. From RFC 1321 - The MD5 Message-Digest Algorithm: "The MD5 message-digest algorithm takes as input a message of arbitrary length and produces as output a 128-bit "fingerprint" or "message digest" of the input.

Is MD5 secure PHP?

It is not recommended to use this function to secure passwords, due to the fast nature of this hashing algorithm. See the Password Hashing FAQ for details and best practices.

Can we decrypt MD5 in PHP?

How to Decrypt MD5 Passwords in PHP? The MD5 cryptographic algorithm is not reversible i.e. We cannot decrypt a hash value created by the MD5 to get the input back to its original value. So there is no way to decrypt an MD5 password.

What is MD5 () function?

What is MD5? MD5 (message-digest algorithm) is a cryptographic protocol used for authenticating messages as well as content verification and digital signatures. MD5 is based on a hash function that verifies that a file you sent matches the file received by the person you sent it to.


5 Answers

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/

like image 109
xil3 Avatar answered Nov 08 '22 14:11

xil3


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/

like image 23
Dan Avatar answered Nov 08 '22 16:11

Dan


You are outputting the raw md5 output, which is just a bunch of bytes. You would get the same result in php if you said md5("some string", true).

You need to convert the bytes to ascii characters instead.

like image 2
Decko Avatar answered Nov 08 '22 15:11

Decko


You need to convert the result into the HEX representation. This is how it is done in Fast MD5 library:

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

like image 1
Sergii Pozharov Avatar answered Nov 08 '22 14:11

Sergii Pozharov


if you use spring security framework , just do :

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

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

The same result as PHP::md5(). I confirm

See more examples

like image 1
Abdennour TOUMI Avatar answered Nov 08 '22 14:11

Abdennour TOUMI