Solved

Java MD5 doesn't match php?

Posted on 2003-12-03
7
1,391 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 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

707 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