MD5 hash differs in Java and PHP

I really need halp getting the Java MD5 and PHP MD5 yield the same result.

In PHP I simply use thins: md5($string);

In Java I use this:
try {
                  hash = snStr+authStr;
                  byte defaultBytes[] = hash.getBytes();
                  MessageDigest algorithm = MessageDigest.getInstance("MD5");
                  byte messageDigest[] = algorithm.digest();

                  StringBuffer hexString = new StringBuffer();
                  for (byte aMessageDigest : messageDigest) {
                    // We need to pad small values with an extra zero:
                    int val = 0xff & aMessageDigest;
                    if (val < 16)
                  hash = hexString.toString();
            } catch (NoSuchAlgorithmException nsae) {

I have also tried withouth the padding.

The even more strange thing is that for short strings the two MD5:s are the same but if the string to be hashed are longer than around 10 chars I will get two different results

Thank you very much for your help!
Who is Participating?

Improve company productivity with a Business Account.Sign Up

CEHJConnect With a Mentor Commented:
String hash = new java.math.BigInteger(1, messageDigest).toString(16);

Open in new window

ariohConnect With a Mentor Commented:
take a look at 2nd comment at
Make sure that the same character encoding is being used in each case. hash.getBytes() will use the encoding of System.getProperty("file.encoding");
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

andtorAuthor Commented:
CEHJ: that yields exactly the same output. When I try System.getProperty("file.encoding"); I get Cp1252 back.

The problem is that the php side is already complete and will not be changed. I can see that MD5(string) has been used and not MD5(string, true). So I really need to make the Java side to create the same output as the php does. Any suggstions on how to do this?

did u try the code i posted?

> byte defaultBytes[] = hash.getBytes();

also try changing that to:

byte defaultBytes[] = hash.getBytes("UTF8");
Can we have some side-by-side examples of input/output on both please
andtorAuthor Commented:
Hello. Have got access to the php code now and can see that the php programmer did not actually follow the specification (did not make all chars to lowercase bfore md5...) Sorry to have bothereed u with this, was extremely painfull to sort out the bug when I could not see the php code.

Please divide the points equally to the once that tried to help me.
>>Please divide the points equally to the once that tried to help me.

You'll have to do that i'm afraid ;-)
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.