Solved

MD5 hash differs in Java and PHP

Posted on 2007-12-06
9
5,295 Views
Last Modified: 2008-03-19
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 {
                  
                  //get
                  hash = snStr+authStr;
                  byte defaultBytes[] = hash.getBytes();
                  MessageDigest algorithm = MessageDigest.getInstance("MD5");
                  algorithm.reset();
                  algorithm.update(defaultBytes);
                  byte messageDigest[] = algorithm.digest();

                  StringBuffer hexString = new StringBuffer();
                  for (byte aMessageDigest : messageDigest) {
                    // We need to pad small values with an extra zero: http://forum.de.selfhtml.org/archiv/2005/10/t117779/
                    int val = 0xff & aMessageDigest;
                    if (val < 16)
                      hexString.append("0");
                    hexString.append(Integer.toHexString(val));
                  }
                  hash = hexString.toString();
            } catch (NoSuchAlgorithmException nsae) {
                  nsae.printStackTrace();
            }


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!
0
Comment
Question by:andtor
  • 4
  • 2
  • 2
  • +1
9 Comments
 
LVL 86

Accepted Solution

by:
CEHJ earned 84 total points
Comment Utility
Try
String hash = new java.math.BigInteger(1, messageDigest).toString(16);

Open in new window

0
 
LVL 9

Assisted Solution

by:arioh
arioh earned 83 total points
Comment Utility
take a look at 2nd comment at http://ru2.php.net/manual/en/function.md5.php
0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
Make sure that the same character encoding is being used in each case. hash.getBytes() will use the encoding of System.getProperty("file.encoding");
0
 
LVL 92

Assisted Solution

by:objects
objects earned 83 total points
Comment Utility
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Author Comment

by:andtor
Comment Utility
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?


0
 
LVL 92

Expert Comment

by:objects
Comment Utility
did u try the code i posted?

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

also try changing that to:

byte defaultBytes[] = hash.getBytes("UTF8");
0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
Can we have some side-by-side examples of input/output on both please
0
 

Author Comment

by:andtor
Comment Utility
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.
0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
>>Please divide the points equally to the once that tried to help me.

You'll have to do that i'm afraid ;-)
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Cybersecurity has become the buzzword of recent years and years to come. The inventions of cloud infrastructure and the Internet of Things has made us question our online safety. Let us explore how cloud- enabled cybersecurity can help us with our b…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now