MD5 Encryption Java Program(not Decryption)-Message Digest Algorithm

MD5 is one of the famous Hash algorithm.  This algorithm is used in many websites for storing the password in encrypted form. The MD5 message-digest algorithm was developed by Ronald Rivest at MIT in 1992. This algorithm takes a input message of arbitrary length and produces a 128-bit hash value of the message.
  MD5 is one way encryption method,means only encryption no decryption(but there is lot of hackers helping site to decrypt it).

For java programmers it is easy to encrypt the password with MD5.  How?  go ahead…

For two days ,  i didn’t post anything because i was doing my NIIT project.  Yesterday only i finished it.  My project is Online banking service.   In my project i have planned to store the password in encrypted password.  But i don’t know how to do that?  When i search for algorithm in internet, i came to know that java itself have MD5 algorithm Class.  I got surprised.  Now my project’s customers password are stored in 128 bit encrypted form.

Now  I will help you how to use the MD5 Class in java.

Step 1:

  • First of all you have to create  a class named as “ByteToHex” for future use.
  • Delete Main function(if you are working in  netbeans)
  • Create a new Function named as “byteToHex()”

The Source code should be :

package Model;

public class ByteToHex {

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();


Save the file

Step 2:

  • create a new class with main fucntion.
  • import the following packages :
import Model.ByteToHex;


Use the following code for MD5 encryption:

/*MD5 Encryption goes here-1 way encryption*/

String cipher=null;



MessageDigest md = MessageDigest.getInstance(“MD5”);

String input=password;  //storing the password in input string

md.update(input.getBytes()); //storing the password in md object as bytes

byte[] output = md.digest();  //producing the 128 bit output using the md5 algorithm

cipher= ByteToHex.bytesToHex(output); //converting the bytes to string and storing in cipher String


catch (Exception e) {

System.out.println(“Exception: “+e);


/*MD5 Encryption ends here-1 way encryption*/

I hope you understand.  if you have doubts ,comment here.  I will help you.  This program is only for Java programs.  in future i will give you MD5  code for PHP.

you can use SHA-1 algorithm also .By Editing the following line :