Home>

Java calculate text md5 value for encryption

import java.security.messagedigest;
import java.security.nosuchalgorithmexception;
public class getmd5 {
  public static void main (string [] args) {
    string a="123";
  system.out.println (getmd5 (a));
  }
  /**
   * Get md5 value of text field
   * @param txt
   * @return
   * /
  public static string getmd5 (string txt) {
    string rs="";
    string [] hexdigits={"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f"};
    try {
      messagedigest messagedigest=messagedigest.getinstance ("md5");
      byte [] b=messagedigest.digest (txt.getbytes ());
      stringbuffer resultsb=new stringbuffer ();
      for (int i=0;i<b.length;i ++) {
        int n=b [i];
        if (n<0)
          n=256 + n;
        int d1=n/16;
        int d2=n%16;
        resultsb.append (hexdigits [d1] + hexdigits [d2]);
      }
      rs=resultsb.tostring ();
    } catch (nosuchalgorithmexception e) {
      e.printstacktrace ();
    }
    return rs;
  }
}

output value:

202cb962ac59075b964b07152d234b70

ps:Friends who are interested in encryption and decryption can also refer to our online tools:

  • Previous Python3 implementation of sorting a list by a specified column of tuples
  • Next Python implementation reads communication up to day file as DataFrame