Encryption is often used in daily work. Today I wrote a simple example of using MD5 encryption, and now I paste the source code.
Md5utils class
Package COM. dengsilinming. test; import Java. io. unsupportedencodingexception; import Java. security. messagedigest; import Java. security. nosuchalgorithmexception;/***** @ author dengsilinming * @ date 2012-12-27 am 10:49:16 **/public class md5utils {Private Static final string md5_suffix = "_ dengsilinming "; /*** character encryption ** @ Param STR plaintext * @ return returns the encrypted character with a suffix */public static string encryptbymd5 (string Str) {Try {If (STR = NULL | Str. length () <1 | "0 ". equals (STR) STR = "0"; string TMP = MD5 (STR + md5_suffix, "UTF-8"); If (null! = TMP) {return Replace (TMP, ":", "",-1 ). tolowercase () ;}} catch (nosuchalgorithmexception e) {e. printstacktrace ();} catch (unsupportedencodingexception e) {e. printstacktrace ();} return "";}/*** MD5 encryption method, irreversible * @ Param STR plaintext * @ Param charset character encoding * @ return * @ throws nosuchalgorithmexception * @ throws unsupportedencodingexception */public static string MD5 (string STR, string charset) throws Nosuchalgorithmexception, unsupportedencodingexception {byte [] tmpinput = NULL; If (null! = Str) {If (null! = Charset) {tmpinput = Str. getbytes (charset);} else {tmpinput = Str. getbytes () ;}} else {return NULL;} messagedigest ALG = messagedigest. getinstance ("MD5"); // or "SHA-1" alg. update (tmpinput); Return byte1hex (ALG. digest ();}/*** convert the bytecode into a hexadecimal string ** @ Param inputbyte * @ return */public static string byte1hex (byte [] inputbyte) {If (null = inputbyte) {return NULL;} string resultstr = ""; ST Ring tmpstr = ""; for (INT n = 0; n <inputbyte. length; n ++) {tmpstr = (integer. tohexstring (inputbyte [N] & 0xff); If (tmpstr. length () = 1) resultstr = resultstr + "0" + tmpstr; else resultstr = resultstr + tmpstr; If (n <inputbyte. length-1) resultstr = resultstr + ":";} return resultstr. touppercase ();} public static string Replace (string text, string repl, string with, int max) {If (isemp Ty (text) | isempty (repl) | with = NULL | max = 0) {return text;} int start = 0; int end = text. indexof (repl, start); If (END =-1) {return text;} int repllength = REPL. length (); int increase =. length ()-repllength; increase = (increase <0? 0: increase); Increase * = (max <0? 16: (max> 64? 64: max); stringbuffer Buf = new stringbuffer (text. Length () + increase); While (end! =-1) {Buf. append (text. substring (START, end )). append (with); Start = end + repllength; If (-- max = 0) {break;} End = text. indexof (repl, start);} Buf. append (text. substring (start); Return Buf. tostring ();} public static Boolean isempty (string Str) {return STR = NULL | Str. length () = 0 ;}}
Test class (md5test ):
Package COM. dengsilinming. test; /***** @ author dengsilinming * @ date 2012-12-27 10:48:24 **/public class md5test {/*** @ Param ARGs */public static void main (string [] ARGs) {system. out. println (md5utils. encryptbymd5 ("dengsilinming "));}}