JAVA MD5 String Encryption

Hi,

Today I explain how to generate MD5 string in Java.

The MD5 Message-Digest Algorithm is a widely used cryptographic hash function that produces a 128-bit (16-byte) hash value. MD5 has been employed in a wide variety of security applications, and is also commonly used to check data integrity. MD5 was designed by Ron Rivest in 1991 to replace an earlier hash function, MD4. An MD5 hash is typically expressed as a 32-digit hexadecimal number.

Java security package java.security provides certain useful classes to generate Hash values. Especially the class java.security. Message-digest provides applications the functionality of a message digest algorithm, such as MD5 or SHA.

This generated String can be save on database for securing the user password.

Create a class as follows

import java.security.*;

public class Test {
               
                     String val_1="abc",val_2="xyz";

                public static void main(String[] args) {
                        System.out.println("abc = "+MD5(val_1));
                        System.out.println("xyz = "+MD5(val_2));
                    }

    public String MD5(String md5) {
        try {
            MessageDigest md = MessageDigest.getInstance("MD5");
            byte[] array = md.digest(md5.getBytes());
            StringBuffer sb = new StringBuffer();
            for (int i = 0; i < array.length; ++i) {
                sb.append(Integer.toHexString((array[i] & 0xFF) | 0x100).substring(1, 3));
            }
            return sb.toString();
        } catch (Exception e) {
        }
        return null;
    }
}

The output of above program is as flollows

abc = 900150983cd24fb0d6963f7d28e17f72
xyz = d16fb36f0911f878998c136191af705e

The String MD5 function having the parameter of a string value and it will convert the String to MD5 value. The generated value is not a reversible.

MD5 generate the irreversible value.


Thanks
Mahesh Nawale


Comments

Popular posts from this blog

JTable Connection with JDBC-MYSQL and Pagination Over JTable

JAVA Chart

Database connection with mysql in Java