Using MD5 Message Digest in Java

This section provides a tutorial example on how to use MD5 message digest algorithm in Java. The JDK JCE package offers the MD5 algorithm through a generic message digest class, javax.security.MessageDigest.

Sun provides MD5 algorithm in Java under their JCE (Java Cryptography Extension) package, which is included in JDK 1.5.

Sun's implementation of MD5 can be accessed through a generic class called MessageDigest. Here are the main methods of MessageDigest class:

Here is a sample Java program to show you how to use the MessageDigest class to perform some tests on MD5 algorithms.

/**
 * JceMd5Test.java
#- Copyright (c) 2013, HerongYang.com, All Rights Reserved.
 */
import java.security.*;
class JceMd5Test {
   public static void main(String[] a) {
      try {
         MessageDigest md = MessageDigest.getInstance("MD5");
         System.out.println("Message digest object info: ");
         System.out.println("   Algorithm = "+md.getAlgorithm());
         System.out.println("   Provider = "+md.getProvider());
         System.out.println("   toString = "+md.toString());

         String input = "";
         md.update(input.getBytes()); 
      	 byte[] output = md.digest();
         System.out.println();
         System.out.println("MD5(\""+input+"\") =");
         System.out.println("   "+bytesToHex(output));

         input = "abc";
         md.update(input.getBytes()); 
      	 output = md.digest();
         System.out.println();
         System.out.println("MD5(\""+input+"\") =");
         System.out.println("   "+bytesToHex(output));

         input = "abcdefghijklmnopqrstuvwxyz";
         md.update(input.getBytes()); 
      	 output = md.digest();
         System.out.println();
         System.out.println("MD5(\""+input+"\") =");
         System.out.println("   "+bytesToHex(output));
         
      } catch (Exception e) {
         System.out.println("Exception: "+e);
      }
   }
   public static String bytesToHex(byte[] b) {
      char hexDigit[] = {'0', '1', '2', '3', '4', '5', '6', '7',
                         '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
      StringBuffer buf = new StringBuffer();
      for (int j=0; j<b.length; j++) {
         buf.append(hexDigit[(b[j] >> 4) & 0x0f]);
         buf.append(hexDigit[b[j] & 0x0f]);
      }
      return buf.toString();
   }
}

If you run this sample program with JDK 1.5, you should get the following output:

Message digest object info:
   Algorithm = MD5
   Provider = SUN version 1.5
   toString = MD5 Message Digest from SUN, <initialized>

MD5("") =
   D41D8CD98F00B204E9800998ECF8427E

MD5("abc") =
   900150983CD24FB0D6963F7D28E17F72

MD5("abcdefghijklmnopqrstuvwxyz") =
   C3FCD3D76192E4007DFB496CCA67E13B

The output matches the testing result listed in RFC 1321.

Last update: 2013.

Table of Contents

 About This Book

 Cryptography Terminology

 Cryptography Basic Concepts

 Introduction to AES (Advanced Encryption Standard)

 Introduction to DES Algorithm

 DES Algorithm - Illustrated with Java Programs

 DES Algorithm Java Implementation

 DES Algorithm - Java Implementation in JDK JCE

 DES Encryption Operation Modes

 DES in Stream Cipher Modes

 PHP Implementation of DES - mcrypt

 Blowfish - 8-Byte Block Cipher

 Secret Key Generation and Management

 Cipher - Secret Key Encryption and Decryption

 Introduction of RSA Algorithm

 RSA Implementation using java.math.BigInteger Class

 Introduction of DSA (Digital Signature Algorithm)

 Java Default Implementation of DSA

 Private key and Public Key Pair Generation

 PKCS#8/X.509 Private/Public Encoding Standards

 Cipher - Public Key Encryption and Decryption

MD5 Mesasge Digest Algorithm

 What Is MD5 Message Digest Algorithm?

 MD5 Message Digest Algorithm Overview

Using MD5 Message Digest in Java

 Using MD5 Message Digest in PHP

 Using MD5 Message Digest in Perl

 SHA1 Mesasge Digest Algorithm

 OpenSSL Introduction and Installation

 OpenSSL Generating and Managing RSA Keys

 OpenSSL Managing Certificates

 OpenSSL Generating and Signing CSR

 OpenSSL Validating Certificate Path

 "keytool" and "keystore" from JDK

 "OpenSSL" Signing CSR Generated by "keytool"

 Migrating Keys from "keystore" to "OpenSSL" Key Files

 Certificate X.509 Standard and DER/PEM Formats

 Migrating Keys from "OpenSSL" Key Files to "keystore"

 Using Certificates in IE (Internet Explorer)

 Using Certificates in Firefox

 Using Certificates in Google Chrome

 Outdated Tutorials

 References

 PDF Printing Version