0

I have a perl script which uses Digest::MD5 md5($data) to obtain the 16 byte digest(which is in non readable form - binary) and this digest is used to encrypt the data. Now i have to reverse the above procedure in java i.e first i need to obtain 16 byte digest using MessageDigest.getInstance("MD5").digest($data) and decrypt the message.

Now i am not sure that output digest of perl Digest::MD5 md5($data) and java digest MessageDigest.getInstance("MD5").digest($data) are same or not how do i validate this scenario.

1 Answers1

1

1) Convert the Perl md5 from byte to hex

2) Convert the Java md5 from byte to hex (examples here)

3) Compare the outputs

This is the Java code for the MD5 and convertion in Hex:

import java.security.MessageDigest;


public class HelloWorld
{
  public static void main(String[] args)
  {

    System.out.println("Start");

    String res=MD5("35799510369");

    System.out.print("res:"+res);

  }

 public static String MD5( String source ) {
        try {
            MessageDigest md = MessageDigest.getInstance( "MD5" );
            byte[] bytes = md.digest( source.getBytes("UTF-8") );
            return getString( bytes );
        } catch( Exception e )  {
            e.printStackTrace();
            return null;
        }
    }//end MD5()

    private static String getString( byte[] bytes ) {
        StringBuffer sb = new StringBuffer();
        for( int i=0; i<bytes.length; i++ )
        {
            byte b = bytes[ i ];
            String hex = Integer.toHexString((int) 0x00FF & b);
            if (hex.length() == 1)
            {
                sb.append("0");
            }
            sb.append( hex );
        }
        return sb.toString();
    }// end getString()

Copy and Paste the previus code in this online compiler and press COMPILE AND EXECUTE; next compare this output with the Perl md5 online script output.


For input=35799510369

  • Perl output:

Md5 digest is .S<ë_»X³ëE&â®

The hexadecimal representation of the digest is: 012e533c9aeb5f96bb58b3eb4526e2ae

  • Java output:

res:012e533c9aeb5f96bb58b3eb4526e2ae

Good luck

Community
  • 1
  • 1
Marco S.
  • 512
  • 2
  • 6
  • 22