Java-continue to seek help from the second wave. The Great God can write this Javamd5 method into a php version. the encryption results must be consistent. thank you!

Source: Internet
Author: User
Tags md5 digest
I went on to ask for help from the second wave. The big God was able to write a php version of this Javamd5 method, and the encryption results were consistent. thank you. the first wave was written by a talented person, but the encryption results are different. for details, refer! The java method is described as follows: 1. convert the key and source string to the byte array. declare two 64-bit arrays to fill in the byte array of the key with an exclusive or operation, and add 54 and 92 to fill the 64-bit length respectively. obtain the MessageDigest object of the md5 digest algorithm. 4. update the MessageDigest Digest using an array and an array of the source string to complete hash calculation. reset Summary 6. make... javaphp-encrypted md5

I went on to ask for help from the second wave. The big God was able to write a php version of the Java md5 method. the encryption results were consistent. Thanks. The first wave was written by a talented person, but the encryption results are different. for details, refer!
The java method is described as follows:
1. convert the key and source string into byte arrays respectively.
2. declare two 64-bit arrays to fill the byte array of the key with an exclusive or operation, and add 54 and 92 to fill the 64-bit length respectively.
3. get the MessageDigest object of the md5 digest algorithm
4. update the MessageDigest Digest using an array and an array of the source string to complete hash calculation.
5. reset the summary
6. use another array to update the Digest. use the result in 4 to update the digest from 0 to 16 to complete hash calculation.
7. convert strings
The method is as follows:
Public String cryptMd5 (String source, String key ){
Byte [] k_ipad = new byte [64];
Byte [] k_opad = new byte [64];
Byte [] keyb;
Byte [] value;
Try {byte [] keyb = key. getBytes ("UTF-8 ");
Value = source. getBytes ("UTF-8 ");
}
Catch (UnsupportedEncodingException e)
{
Byte [] value;
Keyb = key. getBytes ();
Value = source. getBytes ();
}
Arrays. fill (k_ipad, keyb. length, 64, 54 );
Arrays. fill (k_opad, keyb. length, 64, 92 );
For (int I = 0; I <keyb. length; I ++)
{
K_ipad [I] = (byte) (keyb [I] ^ 0x36 );
K_opad [I] = (byte) (keyb [I] ^ 0x5C );
}
MessageDigest md = null;
Try
{
Md = MessageDigest. getInstance ("MD5 ");
}
Catch (NoSuchAlgorithmException e)
{
Return null;
}
Md. update (k_ipad );
Md. update (value );
Byte [] dg = md. digest ();
Md. reset ();
Md. update (k_opad );
Md. update (dg, 0, 16 );
Dg = md. digest ();
Return toHex (dg );}
Public static String toHex (byte [] input)
{
If (input = null ){
Return null;
}
StringBuffer output = new StringBuffer (input. length * 2 );
For (int I = 0; I <input. length; I ++)
{
Int current = input [I] & 0xFF;
If (current <16)
Output. append ("0 ");
Output. append (Integer. toString (current, 16 ));
}
Return output. toString ();
}

The following is what was written by the person who asked the question last time. the encryption result is inconsistent with that of Java.
Function cryptMd5 ($ source, $ key ){
If (! Mb_check_encoding ($ source, 'utf-8') $ source = mb_convert_encoding ($ source, "UTF-8", "auto ");
If (! Mb_check_encoding ($ key, 'utf-8') $ key = mb_convert_encoding ($ key, "UTF-8", "auto ");
$ K_ipad = str_pad ($ key, 64, chr (54 ));

$ K_opad = str_pad ($ key, 64, chr (92 ));
For ($ I = 0; $ I $ K_ipad {$ I }=$ key {$ I} ^ chr (0x36 );
$ K_opad {$ I }=$ key {$ I} ^ chr (0x5c );
}
$ Dg = md5 ($ source. substr ($ k_ipad, strlen ($ source), true );
$ Dg = md5 (substr ($ dg, 0, 16). substr ($ k_opad, 16), true );
Return bin2hex ($ dg );
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.