MD5, full name message Digest algorithm 5, the Chinese name is the fifth edition of the Messaging Digest algorithm, a hash function widely used in the field of computer security to provide message integrity protection. The following is a tool class that uses MD5 encryption in the Java language.
Importjava.security.MessageDigest;Importjava.security.NoSuchAlgorithmException; Public classMd5util {/*** MD5 Digest encryption of strings, return results consistent with MySQL's MD5 function * *@paramInput *@returnthe letter in the return value is lowercase*/ Public Staticstring MD5 (string input) {if(NULL==input) {Input= ""; The String result= ""; Try { //the MessageDigest class is used to provide information digest algorithms for applications such as the MD5 or SHA algorithmsMessageDigest MD = messagedigest.getinstance ("MD5"); //Get inputmd.update (Input.getbytes ()); //get output (signed hash byte array with 16 elements) byteOutput[] =md.digest (); //32-bit encrypted stringStringBuilder Builder =NewStringBuilder (32); //the following hexadecimal conversions for(intoffset = 0; Offset < output.length; offset++) { //Converted to the corresponding Assic value intValue =Output[offset]; //Convert negative numbers to positive numbers (the final result is unsigned) if(Value < 0) {Value+ = 256; } //less than 16, only one byte after hexadecimal, and 2 bytes for the left margin if(Value < 16) {builder.append ("0"); } //convert 16-bit byte[] to 32-bit unsigned stringbuilder.append (integer.tohexstring (value)); } result=builder.tostring (); } Catch(nosuchalgorithmexception e) {e.printstacktrace (); } returnresult; } //Test Public Static voidMain (string[] args) {String M1= MD5 ("123"); String m2= MD5 ("124"); String M3= MD5 (""); System.out.println ("M1=" +M1); System.out.println ("M2=" +m2); System.out.println ("M3=" +m3); }}
Algorithms for using MD5 encryption in Java