Solved

Java MD5 doesn't match php?

Posted on 2003-12-03
7
1,381 Views
Last Modified: 2008-02-26
Java:
------------------------------------------------------
StringBuffer sb = new StringBuffer();

        try {
            String data = "apple";
            MessageDigest messageDigest = MessageDigest.getInstance("MD5");
            messageDigest.update(data.getBytes());
            byte[] digestBytes = messageDigest.digest();

            /* convert to hexstring */
            String hex = null;

            for (int i = 0; i < digestBytes.length; i++) {
                hex = Integer.toHexString(0xFF & digestBytes[i]);

                if (hex.length() < 2) {
                    sb.append("0");
                    sb.append(hex);
                }
                sb.append(hex);
            }
        }
        catch (Exception ex) {
            System.out.println(ex.getMessage());
        }

        System.out.println(sb.toString());

PHP:
------------------------------------------------------
echo md5("apple");
or http://tw.php.net/md5

--
I got a
1f3870be274f6c49b3e31a0cc6728957f (from Java)
1f3870be274f6c49b3e31a0c6728957f  (from PHP)

always differ one bit.

Thanks in advance:)
0
Comment
Question by:YuRen
  • 3
  • 2
  • 2
7 Comments
 
LVL 15

Accepted Solution

by:
jimmack earned 50 total points
ID: 9873072
               if (hex.length() < 2) {
                    sb.append("0");
                    sb.append(hex);
                }
                sb.append(hex);

should be:

                if (hex.length() < 2) {
                    sb.append("0");
                }
                sb.append(hex);

You were appending "hex" twice if you found a value less than 0x10 ;-)
0
 

Author Comment

by:YuRen
ID: 9873190
You save my life, jimmack
:)

A few days later, I would implement the security scheme in my web site.
http://www.uwan.com.tw/   Enjoy it!
0
 
LVL 15

Expert Comment

by:jimmack
ID: 9874566
;-)
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Expert Comment

by:kelvinlaw
ID: 10359519
may i ask :

hex = Integer.toHexString(0xFF & digestBytes[i]);

why 0xFF is needed?
0xFF should be 11111111

0xFF & digestBytes[i] same as  digestBytes[i] ??

thanks
0
 

Author Comment

by:YuRen
ID: 10363555
0xff shoud be 00000000 00000000 00000000 11111111

--
        byte[] digestBytes = { (byte) 0x7f, (byte) 0x8f, (byte) 0xff };

        for (int i = 0; i < digestBytes.length; i++) {
            System.out.print(Integer.toHexString(digestBytes[i]) + "\t");
            System.out.print(Integer.toHexString(0xff & digestBytes[i]) + "\t");
        }
--
result:
7f      7f      ffffff8f      8f      ffffffff      ff      
0
 

Expert Comment

by:kelvinlaw
ID: 10364544
1 byte is 8 bits, right?
but
System.out.print(Integer.toBinaryString(digestBytes[i])

if give me a 32 bits answer if digestBytes[i] >= 0x80....


WHY????
0
 

Author Comment

by:YuRen
ID: 10368345
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
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.
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

910 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

24 Experts available now in Live!

Get 1:1 Help Now