java-continue to help the second wave, the great God can put this Java MD5 method to write a PHP version, the encryption results consistent, thank you!

Source: Internet
Author: User
Keywords Java PHP encryption MD5
Tags chr md5 digest
javaphp encryption MD5

Continue to help the second wave, the great God can this Java MD5 method to write a PHP version, the encryption results consistent, thank you, the first wave has a talent to write a, but the encryption results are not the same, we can refer to the next!
The Java method describes the following ideas
1. Convert the secret key, source string, respectively, to byte array
2. Declare 2 64-bit arrays to fill in the byte array of key, respectively, and add 54, 92 to fill 64 length
3. Get the MessageDigest object for the MD5 digest algorithm
4. Update the MessageDigest digest with an array of one array and source string to complete the hash calculation
5. Reset Summary
6. Using another array update summary use 4 results from 0 to 16 to update the summary to complete the hash calculation
7. Converting strings
Methods are 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 the last question the talent wrote, encryption results and Java or inconsistent
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, +, Chr (54));

$k _opad = Str_pad ($key, +, 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, +). substr ($k _opad, +), 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.