?
Solved

Java MD5 doesn't match php?

Posted on 2003-12-03
7
Medium Priority
?
1,393 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
7 Comments
 
LVL 15

Accepted Solution

by:
jimmack earned 200 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
Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

 

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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Suggested Courses
Course of the Month13 days, 18 hours left to enroll

801 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