Solved

Java MD5 doesn't match php?

Posted on 2003-12-03
7
1,378 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
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 

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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
copyEndy  challenge 15 55
mapShare challenge 13 68
Image decoding from Camera 3 45
GUI builder for Eclipse? 8 10
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
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…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

706 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

17 Experts available now in Live!

Get 1:1 Help Now