Package com. mobi5.commons. utils;
Import java. Security. messagedigest;
Public class md5encrypt {
Public md5encrypt (){}
Private Final Static string [] hexdigits = {
"0", "1", "2", "3", "4", "5", "6", "7 ",
"8", "9", "A", "B", "C", "D", "E", "F "};
/**
* Convert the byte array to a hexadecimal string.
* @ Param B byte array
* @ Return hexadecimal string
*/
Public static string bytearraytostring (byte [] B ){
Stringbuffer resultsb = new stringbuffer ();
For (INT I = 0; I <B. length; I ++ ){
Resultsb. append (bytetohexstring (B [I]); // if you use this function to convert, you can obtain the hexadecimal representation of the encrypted result, that is, the combination of digits and letters.
// Resultsb. append (bytetonumstring (B [I]); // use this function to return the 10-digit numeric string of the encrypted result, that is, the Full Digit form
}
Return resultsb. tostring ();
}
Private Static string bytetonumstring (byte B ){
Int _ B = B;
If (_ B <0 ){
_ B = 256 + _ B;
}
Return string. valueof (_ B );
}
Private Static string bytetohexstring (byte B ){
Int n = B;
If (n <0 ){
N = 256 + N;
}
Int d1 = N/16;
Int D2 = n % 16;
Return hexdigits [D1] + hexdigits [D2];
}
Public static string md5encode (string origin ){
String resultstring = NULL;
Try {
Resultstring = origin;
Messagedigest MD = messagedigest. getinstance ("MD5 ");
Resultstring = bytearraytostring (Md. Digest (resultstring. getbytes ("gb2312 ")));
}
Catch (exception ex ){
}
Return resultstring;
}
Public static void main (string [] ARGs ){
System. Out. println (md5encode ("3481471j2hy6g $ Kuh * grt73fv9kjdy34 "));
// System. Out. println (md5encode (md5encode ("China ")));
}
}