Java comes with MessageDigest to implement the md5 encryption algorithm for text

  • 2020-04-01 04:34:18
  • OfStack

This article USES Java's built-in MessageDigest to implement the md5 encryption algorithm for the text, the specific code is as follows:


   
package cn.yicha.novel.util;  
 
import java.security.MessageDigest; 
import java.security.NoSuchAlgorithmException; 
  
public class ParseMD5 { 
 
   
  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; 
  } 
   
   
  public static String parseStrToMd5U32(String str){ 
    String reStr = parseStrToMd5L32(str); 
    if (reStr != null){ 
      reStr = reStr.toUpperCase(); 
    } 
    return reStr; 
  } 
   
   
  public static String parseStrToMd5U16(String str){ 
    String reStr = parseStrToMd5L32(str); 
    if (reStr != null){ 
      reStr = reStr.toUpperCase().substring(8, 24); 
    } 
    return reStr; 
  } 
   
   
  public static String parseStrToMd5L16(String str){ 
    String reStr = parseStrToMd5L32(str); 
    if (reStr != null){ 
      reStr = reStr.substring(8, 24); 
    } 
    return reStr; 
  } 
} 

The second case: In the process of Java software development, it is inevitable that some data will be encrypted, so Java provides its own implementation of MessageDigest text encryption algorithm, the following is a code example of the MD5 encryption tool class for text encryption:

MD5 encryption algorithm in Java The full version:


package net.yuerwan.commons.util;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

import org.apache.commons.lang.StringUtils;
public class MD5Util {

public static String textToMD5L32(String plainText){
String result = null;
//First determine whether it is null
if(StringUtils.isBlank(plainText)){
return null;
}
try{
//It is first instantiated and initialized
MessageDigest md = MessageDigest.getInstance("MD5");
//Gets an array of bytes in the operating system's default byte encoding format
byte[] btInput = plainText.getBytes();
//The resulting byte array is processed
md.update(btInput);
//Perform the hash calculation and return the result
byte[] btResult = md.digest();
//Length of data 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;
}


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;

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() {
  }

  //Returns a number and a 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 only for Numbers
  private static String byteToNum(byte bByte) {
    int iRet = bByte;
    System.out.println("iRet1=" + iRet);
    if (iRet < 0) {
      iRet += 256;
    }
    return String.valueOf(iRet);
  }

  //Converts 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");
      //The function md.digest() returns a byte array of hash results
      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"));
  }
}


Related articles: