Using Java for MD5 encryption

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.