Java Encryption Algorithm series-MD5

Source: Internet
Author: User

 Package **;Importjava.security.MessageDigest;Importjava.security.NoSuchAlgorithmException; Public classMD5 {/*** MD5 encryption algorithm, parameter selection 16 or 32 bit * default is 32 bit *@paramplaintext text to be encrypted *@paramMd5type 16/32 *@returnciphertext after encryption*/     Public Staticstring Md5 (String plaintext,string md5type) {Try{messagedigest MD= Messagedigest.getinstance ("MD5");            Md.update (Plaintext.getbytes ()); byteB[] =md.digest (); inti; StringBuffer buf=NewStringBuffer ("");  for(intoffset = 0; Offset < b.length; offset++) {i=B[offset]; if(I < 0) I+ = 256; if(I < 16) Buf.append ("0");            Buf.append (integer.tohexstring (i)); }            if(md5type!=NULL&&!md5type.trim (). Equals ("") &&md5type.trim (). Equals ("16")){                returnBuf.tostring (). SUBSTRING (8). toUpperCase ();//16-bit encryption}Else{                returnBuf.tostring (). toUpperCase ();//32-bit encrypted capitalization            }        } Catch(nosuchalgorithmexception e) {e.printstacktrace (); return NULL; }    }     Public Static voidMain (string[] args) {Md5 ("123456", "16"); System.out.println (Md5 ("123456", "16")); MD5 ("123456", "32"); System.out.println (Md5 ("123456", "32")); System.out.println (Md5 ("123456", "+"). Equals ("e10adc3949ba59abbe56e057f20f883e")); System.out.println (Md5 ("123456", "+"). Equals ("49ba59abbe56e057")); }}

Java Encryption Algorithm series-MD5

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.