Full version of MD5 Encryption Algorithm in Java, javamd5 Encryption Algorithm

Source: Internet
Author: User

Full version of MD5 Encryption Algorithm in Java, javamd5 Encryption Algorithm

Some data is inevitably encrypted during Java software development. Therefore, Java provides its own MessageDigest to implement text encryption algorithms, the following is an example of the MD5 encryption tool code for text encryption:


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 note: Wenbo small station --- http://www.iwwenbo.com */public class MD5Util {/*** 1. 32-bit lowercase MD5 encryption for text * @ param plainText the text to be encrypted * @ return encrypted content */public static String textToMD5L32 (String plainText) {String result = null; // first determine whether it is null if (StringUtils. isBlank (plainText) {return null;} try {// first instantiate and initialize MessageDigest md = MessageDigest. getInstance ("MD5"); // get the byte array byte [] btInput = plainText in the default byte encoding format of the operating system. getBytes (); // process the byte array. md. update (btInput); // perform hash calculation and return the result byte [] btResult = md. digest (); // The length of the data obtained after hash calculation 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. perform 32-bit MD5 capitalized encryption on the 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 ();}
Complete reading> Click me;

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.