This article uses the MessageDigest, which comes with Java, to implement the md5 encryption algorithm for text. The specific code is as follows:
/** *@Description: Convert string to MD5 */ package cn.yicha.novel.util; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; public class ParseMD5 { /** * @param str * @return * @Description: 32-bit lowercase MD5 */ public static String parseStrToMd5L32(String str){ String reStr = null; try { MessageDigest md5 = MessageDigest.getInstance("MD5"); byte[] bytes = md5.digest(str.getBytes()); StringBuffer stringBuffer = new StringBuffer(); for (byte b : bytes){ int bt = b&0xff; if (bt < 16){ stringBuffer.append(0); } stringBuffer.append(Integer.toHexString(bt)); } reStr = stringBuffer.toString(); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } return reStr; } /** * @param str * @return * @Description: 32-bit uppercase MD5 */ public static String parseStrToMd5U32(String str){ String reStr = parseStrToMd5L32(str); if (reStr != null){ reStr = reStr.toUpperCase(); } return reStr; } /** * @param str * @return * @Description: 16-bit lowercase MD5 */ public static String parseStrToMd5U16(String str){ String reStr = parseStrToMd5L32(str); if (reStr != null){ reStr = reStr.toUpperCase().substring(8, 24); } return reStr; } /** * @param str * @return * @Description: 16-bit uppercase MD5 */ public static String parseStrToMd5L16(String str){ String reStr = parseStrToMd5L32(str); if (reStr != null){ reStr = reStr.substring(8, 24); } return reStr; } }The second situation: During the Java software development process, some data will inevitably be encrypted. Therefore, Java provides the own MessageDigest implementation of the encryption algorithm for text. The following is an example of the code of MD5 encryption tool that encrypts text:
The full version of MD5 encryption algorithm in Java :
package net.yuerwan.commons.util;import java.security.MessageDigest;import java.security.NoSuchAlgorithmException;import org.apache.commons.lang.StringUtils;public class MD5Util {/*** 1. 32-bit lowercase MD5 encryption for text* @param plainText Text to be encrypted* @return Encrypted content*/public static String textToMD5L32(String plainText){String result = null;//First determine whether it is empty if(StringUtils.isBlank(plainText)){return null;}try{//First instantiate and initialize MessageDigest md = MessageDigest.getInstance("MD5");//Get a byte array in the default byte encoding format of the operating system byte[] btInput = plainText.getBytes();//File the obtained byte array md.update(btInput);//File the hash calculation and return the result byte[] btResult = md.digest();//Fit 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. Encrypt the text 32-bit MD5 uppercase * @param plainText 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();}The third case: 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")); }}