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 ie 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) 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
Md5 digest is .S<ë_»X³ëE&â®
The hexadecimal representation of the digest is: 012e533c9aeb5f96bb58b3eb4526e2ae
res:012e533c9aeb5f96bb58b3eb4526e2ae
Good luck
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.