Java MD5 encryption tool class, javamd5 tool class

Source: Internet
Author: User

Java MD5 encryption tool class, javamd5 tool class

Import java. security. messageDigest;/*** MD5 encryption tool class * @ author zwq */public class MD5Util {/*** MD5 encryption ** @ param message the information to be encrypted, for example: 123456 * @ return returns the MD5 encrypted 32-bit uppercase String, for example, E10ADC3949BA59ABBE56E057F20F883E */public static String encode (String message) throws Exception {String md5 = ""; messageDigest md = MessageDigest. getInstance ("MD5"); // create an md5 Algorithm object byte [] messageByte = message. getBytes ("UTF-8"); byte [] md5Byte = md. digest (messageByte); // obtain the MD5 byte array, 16*8 = 128-bit md5 = bytesToHex (md5Byte); // convert it to a hexadecimal string return md5 ;} /*** convert the byte array to a hexadecimal string * @ param bytes the byte array to be converted, for example, [-, 10,-, 73,-, 89, -85,-15,-] * @ return returns the converted uppercase String, for example, bytes */public static String bytesToHex (byte [] bytes) {StringBuffer hexStr = new StringBuffer (); int num; for (int I = 0; I <bytes. length; I ++) {num = bytes [I]; if (num <0) {num + = 256;} if (num <16) {hexStr. append ("0");} hexStr. append (Integer. toHexString (num);} return hexStr. toString (). toUpperCase ();}}

 

Related Article

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.