NoSuchAlgorithm exception when using MD5

1.9k views Asked by At
import java.security.*;

MessageDigest md = MessageDigest.getInstance("MD5");

fails with NoSuchAlgorithm exception.

MessageDigest docs](http://docs.oracle.com/javase/7/docs/api/java/security/MessageDigest.html) say:

Every implementation of the Java platform is required to support the following standard MessageDigest algorithms: MD5 SHA-1 SHA-256 These algorithms are described in the MessageDigest section of the Java Cryptography Architecture Standard Algorithm Name Documentation. Consult the release documentation for your implementation to see if any other algorithms are supported.

So how come it throws the exception?

luckily

import org.apache.commons.codec.digest.DigestUtils;

System.out.println( "md5 = "+DigestUtils.md5Hex( string ) );    

works perfectly, plus it is elegant, but still looks like a pretty basic failure. What am I missing?

1

There are 1 answers

4
İlker Korkut On

I have just examined DigestUtils class , there is a try-catch handle for NoSuchAlgorithmException.

You can check in here.

You are missing throws declaration or try-catch block to handle exception. The error should be compilation error. If it's not a compilation error check the "MD5" string typo.

For compilation error , Try to surrond your code with try-catch block.

try {
    MessageDigest md = MessageDigest.getInstance("MD5");
} catch (NoSuchAlgorithmException e) {  
    e.printStackTrace();
}

Or add your method throws declaration.

public static void main(String[] args) throws NoSuchAlgorithmException {