Phpjavamd5
The great God can write me this Java MD5 encryption method PHP version Oh, grateful Oh, Java code is as follows!
The person who wrote this 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
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();
}