Java development of MD5 encryption algorithm

  • 2020-04-01 01:48:02
  • OfStack

Let's look at the code first:

package com.b510.note;

 import java.math.BigInteger;
 import java.security.MessageDigest;
 import java.security.NoSuchAlgorithmException;

 
 public class MD5 {

     public static void main(String[] args) {
         System.out.println(MD5.getMD5("123456"));
     }

     
     public static String getMD5(String sInput) {

         String algorithm = "";
         if (sInput == null) {
             return "null";
         }
         try {
             algorithm = System.getProperty("MD5.algorithm", "MD5");
         } catch (SecurityException se) {
         }
         MessageDigest md = null;
         try {
             md = MessageDigest.getInstance(algorithm);
         } catch (NoSuchAlgorithmException e) {
             e.printStackTrace();
         }
         byte buffer[] = sInput.getBytes();

         for (int count = 0; count < sInput.length(); count++) {
             md.update(buffer, 0, count);
         }
         byte bDigest[] = md.digest();
         BigInteger bi = new BigInteger(bDigest);
         return (bi.toString(16));
     }
 }

Operation effect:

1 186d636867f51c667893c1b1b3d96dc3

We can according to their own needs, to get what they want after the md5 encryption ciphertext;

1 //The Numbers here can be changed as needed
2 return (bi.toString(16));

Related articles: