First, let’s understand the MD5 encryption implementation from the following figure, as follows
package com.pb;import java.io.UnsupportedEncodingException;import java.security.MessageDigest;import java.security.NoSuchAlgorithmException;import java.util.Scanner;/* * Verify MD5 * 1. Initialize the MessageDigest information summary object* 2. Pass in the string to be calculated to update the summary information* 3. Calculate the information summary* 4. Convert byte[] to a hexadecimal string with a 32-bit search*/public class MD5 { /* * Generate md5 with passed in parameter string*/ public void generateMD5(String input){ try { //1. Initialize the MessageDigest information summary object and specify it as MD5 without case-independent MessageDigest md=MessageDigest.getInstance("md5"); //2. Pass in the string to be calculated to update the summary information, the passed in is the byte array byte[], // Convert the string to byte array using the getBytes() method// When specified, its character encoding is utf-8 md.update(input.getBytes("utf-8")); //3. Calculate the information summary digest() method//Return the value is the byte array byte[] hashCode=md.digest(); //4. Convert byte[] Convert to a hexadecimal string with a 32-bit search //Declare a StringBuffer object to store the last value StringBuffer sb=new StringBuffer(); //Transfer the byte array for(byte b:hashCode){ //Convert the array content into hexadecimal, sb.append(Character.forDigit(b>>4&0xf, 16)); //Transfer to the 32-bit hexadecimal sb.append(Character.forDigit(b&0xf, 16)); } System.out.println("The result after encryption 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 that encrypts the MD5 class MD5 md5=new MD5(); //Use Scanner to enter a character Scanner scanner=new Scanner(System.in); System.out.println("Please enter the content to be encrypted:"); String input = scanner.nextLine(); //Calling the encryption method md5.generateMD5(input); } } result:
Please enter the content to be encrypted:
The result after learning the encryption process of MD5 is: b826cdac46f01dcc8ccc60a76cebf858
Second code:
package test.md5;import java.security.MessageDigest;public class MD5Util { public final static String MD5(String s) { char hexDigits[]={'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'}; try { byte[] btInput = s.getBytes(); // Get the MessageDigest object of the MD5 digest algorithm MessageDigest mdInst = MessageDigest.getInstance("MD5"); // Update the digest mdInst.update(btInput); // Get the ciphertext byte[] md = mdInst.digest(); // Convert the ciphertext into a hex string form int j = md.length; char str[] = new char[j * 2]; int k = 0; for (int i = 0; i < j; i++) { byte byte0 = md[i]; str[k++] = hexDigits[byte0 >>> 4 & 0xf]; str[k++] = hexDigits[byte0 & 0xf]; } return new String(str); } catch (Exception e) { e.printStackTrace(); return null; } } public static void main(String[] args) { System.out.println(MD5Util.MD5("20121221")); System.out.println(MD5Util.MD5("encryption")); }}The third piece of code: Java implementation of MD5 encryption algorithm
package other;import java.security.MessageDigest;import java.security.NoSuchAlgorithmException;/* * MD5 algorithm*/public class MD5 { // global array private final static String[] strDigits = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f" }; public MD5() { } // Return form as number and string private static String byteToArrayString(byte bByte) { int iRet = bByte; // System.out.println("iRet="+iRet); if (iRet < 0) { iRet += 256; } int iD1 = iRet / 16; int iD2 = iRet % 16; return strDigits[iD1] + strDigits[iD2]; } // Return form is only a digit private static String byteToNum(byte bByte) { int iRet = bByte; System.out.println("iRet1=" + iRet); if (iRet < 0) { iRet += 256; } return String.valueOf(iRet); } // Convert the byte array to a hexadecimal string private static String byteToString(byte[] bByte) { StringBuffer sBuffer = new StringBuffer(); for (int i = 0; i < bByte.length; i++) { sBuffer.append(byteToArrayString(bByte[i])); } return sBuffer.toString(); } public static String GetMD5Code(String strObj) { String resultString = null; try { resultString = new String(strObj); MessageDigest md = MessageDigest.getInstance("MD5"); // md.digest() The return value of this function is the byte array that stores the hash resultString = byteToString(md.digest(strObj.getBytes())); } catch (NoSuchAlgorithmException ex) { ex.printStackTrace(); } return resultString; } public static void main(String[] args) { MD5 getMD5 = new MD5(); System.out.println(getMD5.GetMD5Code("000000")); }}The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.