Codxplore.com

Latest news:

Sahih al-Bukhari (সহীহ বুখারী) is a free Hadith application for android. This application is advertisement free. Download now https://play.google.com/store/apps/details?id=com.akramhossin.bukharisharif

MD5 encryption using java


MD5 encryption using java

In this tutorial we will learn how to create md5 hash in java. To create md5 hash in java we need follow below steps.

  • Import java.math.BigInteger.
  • Import java.security.MessageDigest.
  • Import java.security.NoSuchAlgorithmException.
  • Create MessageDigest object for MD5
  • Update input string in message digest
  • Converts message digest value in base16 (hex)
  • If md5 generated successfully return md5 string else show an exception.

Below is the example class for java to generate md5 hash

package yourpackage;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
/**
 *
 *
 */
public class Encrypt {
    public static String md5(String input) {
        String md5 = null;
        if (null == input) {
            return null;
        }
        try {
            MessageDigest digest = MessageDigest.getInstance("MD5");
            digest.update(input.getBytes(), 0, input.length());
            md5 = new BigInteger(1, digest.digest()).toString(16);

        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        return md5;
    }
}

Tags : Java, MySQL,

Views : 1031

Subscribe Us


Follow Us