Package COM. liujun. util; import Java. security. messagedigest; /*** MD5 encryption and matching tool * @ author Asus **/public class md5util {// ing array of numbers to characters in hexadecimal notation private final static string [] hexdigits = {"0 ", "1", "2", "3", "4", "5", "6", "7", "8", "9", "", "B", "C", "D", "E", "F "}; /*** replace a byte with a hexadecimal string * @ Param B hexadecimal byte * @ return hexadecimal string */Private Static string bytetohexstr (byte B) {int n = B; If (n <0) {n = 256 + N;} int d1 = N/16; int d 2 = n % 16; return hexdigits [D1] + hexdigits [D2];} /*** convert a byte array to a hexadecimal string * @ Param bytes byte array * @ return a hexadecimal string */Private Static string bytearraytohexstr (byte [] bytes) {stringbuffer sb = new stringbuffer (); For (INT I = 0; I <bytes. length; I ++) {sb. append (bytetohexstr (Bytes [I]);} return sb. tostring ();}/*** MD5 encryption of the string * @ Param originstr original password * @ returnmd5 encrypted password string */public static string encodetomd5str (string Origins Tr) {If (originstr! = NULL) {try {// create MD5 Algorithm information messagedigest = messagedigest. getinstance ("MD5"); // encrypt and convert the original password to an MD5 byte array byte [] md5bytes = messagedigest. digest (originstr. getbytes (); // convert the encrypted byte array to string resultstr = bytearraytohexstr (md5bytes); // convert it to return resultstr. touppercase ();} catch (exception e) {e. printstacktrace () ;}} return NULL ;} /*** use the most string to match the MD5 password * @ Param md5str * @ Param originstr * @ return */public static Boolean validatepassword (string md5str, string originstr) {If (md5str. equals (encodetomd5str (originstr) {return true ;}else {return false ;}}}
Package COM. liujun. main; import COM. liujun. util. md5util; public class main {public static void main (string [] ARGs) {// original password string orginstr = "12345"; // MD5 encrypted password string md5str; // generate the original password md5str = md5util. encodetomd5str (orginstr); system. out. println ("original password:" + orginstr); system. out. println ("MD5 encryption password:" + md5str); // verify that the password is correct if (md5util. validatepassword (md5str, orginstr) {system. out. println ("password matched");} else {system. out. println ("password matching failed ");}}}
Source code: http://download.csdn.net/detail/u010739551/7875377
MD5 encryption tool