Implement MD5 encryption and md5 Encryption

Source: Internet
Author: User

Implement MD5 encryption and md5 Encryption

Package com. pb; import java. io. unsupportedEncodingException; import java. security. messageDigest; import java. security. noSuchAlgorithmException; import java. util. verify;/** verify MD5 * 1. initialize the MessageDigest information Summary object * 2. input the string to be computed to update the summary information * 3. computing Information summary * 4. convert byte [] to a 32-bit hexadecimal String */public class MD5 {/** to generate an md5 input parameter String */public void generateMD5 (String input) {try {// 1. initialize the MessageDigest information digest object, which is set to MessageDigest md = MessageDigest if MD5 is case-insensitive. getInstance ("md5"); // 2. input the string to be calculated to update the summary information. input the byte array byte [], // convert the string to a byte array using getBytes () method completion // indicates that the character encoding is UTF-8 md. update (input. getBytes ("UTF-8"); // 3. calculation information digest () method // The returned value is a byte array byte [] hashCode = md. digest (); // 4. convert byte [] to a 32-bit hexadecimal string // declare the StringBuffer object to store the final value StringBuffer sb = new StringBuffer (); // traverse the byte array for (byte B: hashCode) {// convert the array content to hexadecimal notation, sb. append (Character. forDigit (B> 4 & 0xf, 16); // For a 32-bit hexadecimal sb. append (Character. forDigit (B & 0xf, 16);} System. out. println ("the encrypted result is:" + sb. toString ();} catch (NoSuchAlgorithmException e) {// TODO Auto-generated catch block e. printStackTrace ();} catch (UnsupportedEncodingException e) {// TODO Auto-generated catch block e. printStackTrace () ;}} public static void main (String [] args) {// declare the object MD5 of the encrypted MD5 class = new md5 (); // use the delimiter to enter a character = new delimiter (System. in); System. out. println ("Enter the content to be encrypted:"); String input = encrypted. nextLine (); // call the encryption method md5.generateMD5 (input );}}

  

Enter the content to be encrypted: after learning the MD5 encryption process, the result is: b826cdac46f01dcc8ccc60a76cebf858

  

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.