Algorithms for using MD5 encryption in Java

Source: Internet
Author: User
Tags md5 digest md5 encryption

MD5, full name message Digest algorithm 5, the Chinese name is the fifth edition of the Messaging Digest algorithm, a hash function widely used in the field of computer security to provide message integrity protection. The following is a tool class that uses MD5 encryption in the Java language.

Importjava.security.MessageDigest;Importjava.security.NoSuchAlgorithmException; Public classMd5util {/*** MD5 Digest encryption of strings, return results consistent with MySQL's MD5 function * *@paramInput *@returnthe letter in the return value is lowercase*/     Public Staticstring MD5 (string input) {if(NULL==input) {Input= ""; The String result= ""; Try {            //the MessageDigest class is used to provide information digest algorithms for applications such as the MD5 or SHA algorithmsMessageDigest MD = messagedigest.getinstance ("MD5"); //Get inputmd.update (Input.getbytes ()); //get output (signed hash byte array with 16 elements)            byteOutput[] =md.digest (); //32-bit encrypted stringStringBuilder Builder =NewStringBuilder (32); //the following hexadecimal conversions             for(intoffset = 0; Offset < output.length; offset++) {                //Converted to the corresponding Assic value                intValue =Output[offset]; //Convert negative numbers to positive numbers (the final result is unsigned)                if(Value < 0) {Value+ = 256; }                //less than 16, only one byte after hexadecimal, and 2 bytes for the left margin                if(Value < 16) {builder.append ("0"); }                //convert 16-bit byte[] to 32-bit unsigned stringbuilder.append (integer.tohexstring (value)); } result=builder.tostring (); } Catch(nosuchalgorithmexception e) {e.printstacktrace (); }        returnresult; }    //Test     Public Static voidMain (string[] args) {String M1= MD5 ("123"); String m2= MD5 ("124"); String M3= MD5 (""); System.out.println ("M1=" +M1); System.out.println ("M2=" +m2); System.out.println ("M3=" +m3); }}

Algorithms for using MD5 encryption in Java

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.