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