In this tutorial, I am going to show you how to use Java MD5 Encryption.

Java MD5 Encryption :

The below example generate a MD5 encryption (generating hash value) for a given password (String).

public class JavaMD5Example {

    public static String getMD5EncryptedValue(String password) {
        final byte[] defaultBytes = password.getBytes();
        try {
            final MessageDigest md5MsgDigest = MessageDigest.getInstance("MD5");
            final byte messageDigest[] = md5MsgDigest.digest();

            final StringBuffer hexString = new StringBuffer();
            for (final byte element : messageDigest) {
                final String hex = Integer.toHexString(0xFF & element);
                if (hex.length() == 1) {
            password = hexString + "";
        } catch (final NoSuchAlgorithmException nsae) {
        return password;

    public static void main(String[] args) {


Output :


Reference :

MD5 wiki

Java MessageDigest


  1. VRadhe February 21, 2019 at 4:04 pm - Reply

    have you any tutorial for MD5 decryption ?

  2. Meepa August 9, 2021 at 10:14 am - Reply

    Any chance to get the rainbow table generation by doing a reduction formula into the generated-hash say 4 times, and then adding the word and the last hash to a table of some sort?
    Preferably a table that can print to a file in the sorted by hash value and the word it came from?

    There is very little clear examples of this on the internet so you’ll be the first!

Leave A Comment