In the Java software development process, some data will inevitably be encrypted, so Java provides the messagedigest implementation of the text encryption algorithm, the following is a text to encrypt the MD5 cryptographic tool Class code example:
Package Net.yuerwan.commons.util;import Java.security.messagedigest;import Java.security.NoSuchAlgorithmException ; Import org.apache.commons.lang.stringutils;/*** function: MD5 Encryption Tool Class * Description: Reprint Please specify: Wenbo Station---http://www.iwwenbo.com*/public Class Md5util {/*** 1.32-bit lowercase MD5 encryption of text * @param plaintext the text to be encrypted * @return encrypted content */public static String texttomd5l32 (Strin G plaintext) {String result = null;//First determines if NULL if (Stringutils.isblank (plaintext)) {return null;} try{//first Instantiates and initializes messagedigest MD = messagedigest.getinstance ("MD5");//Gets an OS default byte-encoded format byte array byte[] Btinput = Plaintext.getbytes ();//processing of the resulting byte array md.update (BTINPUT);//hash calculation and return result byte[] Btresult = Md.digest ();// The length of the data obtained after hashing stringbuffer sb = new StringBuffer (); for (byte b:btresult) {int bt = B&0XFF;IF (bt<16) {sb.append (0) ;} Sb.append (Integer.tohexstring (BT));} result = Sb.tostring ();} catch (NoSuchAlgorithmException e) {e.printstacktrace ();} return result;} /*** 2.32-bit MD5 Capital encryption for text * @param plaintext the text to be encrypted * @return encrypted content */public static String texttomd5u32 (StRing plaintext) {if (Stringutils.isblank (plaintext)) {return null;} String result = Texttomd5l32 (plaintext); return result.touppercase ();}
full read >> Click me;
MD5 encryption algorithm in Java full version