Using Java's MessageDigest class makes it easy to implement MD5 encryption, except that the byte array is encrypted and we need to convert it to a 16-character binary.
The code is as follows:
Packagecom.stepsoft.test;Importjava.security.MessageDigest;/*** * * @authorSam <br/> * Encryption Tool class **/ Public classEncrypter {Private Static Final Char[] hex_digits = {' 0 ', ' 1 ', ' 2 ', ' 3 ', ' 4 ', ' 5 ', ' 6 ', ' 7 ', ' 8 ', ' 9 ', ' A ', ' B ', ' C ', ' D ', ' E ', ' F ' }; /*** * use MD5 for encryption * *@paramtext *, words that require encryption *@return, MD5 ciphertext*/ Public Staticstring MD5 (string text) {Try { byte[] input =text.getbytes (); //MessageDigest object for obtaining the MD5 digest algorithmMessageDigest mdinst = messagedigest.getinstance ("MD5"); //updates the digest with the specified bytesmdinst.update (input); //Get ciphertext byte[] MD =mdinst.digest (); //convert ciphertext to 16-binary string form intj =md.length; CharStr[] =New Char[J * 2]; intK = 0; for(inti = 0; I < J; i++) { byteBYTE0 =Md[i]; Str[k+ +] = hex_digits[byte0 >>> 4 & 0xf]; Str[k+ +] = hex_digits[byte0 & 0xf]; } return NewString (str); } Catch(Exception e) {e.printstacktrace (); return NULL; } }}
Using Java for MD5 encryption