java implementation of MD5 abstract algorithm complete example

  • 2020-05-27 05:35:40
  • OfStack

The example of this paper describes the MD5 abstract algorithm implemented by java. I will share it with you for your reference as follows:

 * @author WHD
public class MD5Test {
  // MD5  One-way encryption 
  public static void main(String[] args) throws NoSuchAlgorithmException,
      UnsupportedEncodingException {
    String str = "hellomd Abstract algorithm start ";
    System.out.println(" The original value " + str);
    System.out.println(" The encrypted " + MD5Test.afterMD5(str));
    String digest = MD5Test.afterMD5(str);
  public static String afterMD5(String str) throws NoSuchAlgorithmException,
      UnsupportedEncodingException {
    //  To obtain MD5  Encrypted object , You can also get SHA Encrypted object 
    MessageDigest md5 = MessageDigest.getInstance("MD5");
    //  Retrieves the bytes of the input information using the specified encoding 
    byte[] bytes = str.getBytes("UTF-8");
    //  use md5  Class to get the digest, that is, the encrypted bytes 
    byte[] md5encode = md5.digest();
    StringBuffer buffer = new StringBuffer();
    for (int i = 0; i < md5encode.length; i++) {
      //  use &0xff  insufficient 24 High, because only occupied 8 low 
      int val = ((int) md5encode[i]) & 0xff;
      if (val < 16) {
      //  In order to 106 Base (base)  16 ) returned as an unsigned integer 1 A string representation of an integer parameter. 
    return buffer.toString();

Using org. apache. commons. codec. digest. DigestUtilsorg. apache. commons. codec. digest. DigestUtils to achieve md5 encryption

Configuration in maven:


Note that there is a big difference between version 1.2 and version 1.4, because there are many methods that are extended in 1.4.
The specific code is as follows:

public static String afterMd5(String str){
      try {
      String md5 = DigestUtils.md5Hex(str.getBytes("UTF-8"));
      return md5;
    } catch (UnsupportedEncodingException e) {
      // TODO Auto-generated catch block
      return null;

PS: about encryption and decryption interested friends can also refer to the website online tools:

Password security online detection:

High strength password generator:

Thunderbolt, express, and whirlwind encryption/decryption tools:

Online hash/hash algorithm encryption tool:

MD5/hash/ SHA-1 / SHA-2 / SHA-256 / SHA-512 / SHA-3 / RIPEMD-160 encryption tools:

Online sha1 sha224 / sha256 sha384 / sha512 encryption tools:

I hope this article is helpful to you java programming.

Related articles: