简体   繁体   English

perl Digest :: MD5 md5($ data)和java MessageDigest.getInstance(“ MD5”)。digest($ data)的输出不同

[英]Output of perl Digest::MD5 md5($data) and java MessageDigest.getInstance(“MD5”).digest($data) are different

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. 我有一个Perl脚本,该脚本使用Digest :: MD5 md5($ data)获取16字节的摘要(以不可读的形式-二进制),并且此摘要用于加密数据。 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. 现在我必须在Java中逆转上述过程,即首先我需要使用MessageDigest.getInstance(“ MD5”)。digest($ data)获得16字节的摘要并解密消息。

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. 现在,我不确定perl Digest :: MD5 md5($ data)的输出摘要和Java摘要MessageDigest.getInstance(“ MD5”)。digest($ data)的输出摘要是否相同,我如何验证这种情况。

1) Convert the Perl md5 from byte to hex 1)将Perl md5从字节转换为十六进制

2) Convert the Java md5 from byte to hex ( examples here ) 2)将Java md5从字节转换为十六进制( 此处示例

3) Compare the outputs 3)比较输出

This is the Java code for the MD5 and convertion in Hex: 这是MD5和十六进制转换的Java代码:

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; 将先前的代码复制并粘贴到此在线编译器中 ,然后按COMPILE AND EXECUTE; next compare this output with the Perl md5 online script output. 接下来,将该输出与Perl md5在线脚本输出进行比较。


For input=35799510369 输入= 35799510369

  • Perl output: Perl输出:

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

The hexadecimal representation of the digest is: 012e533c9aeb5f96bb58b3eb4526e2ae 摘要的十六进制表示形式是:012e533c9aeb5f96bb58b3eb4526e2ae

  • Java output: Java输出:

res:012e533c9aeb5f96bb58b3eb4526e2ae res:012e533c9aeb5f96bb58b3eb4526e2ae

Good luck 祝好运

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM