Error generating keys: java.security.NoSuchAlgorithmException

2

I'm doing some cryptography tests, by executing the following line of code:

Signature sgn = Signature.getInstance( "MD5" );

I get the error:

  

java.security.NoSuchAlgorithmException: MD5 Signature not available

I have tried SHA, SHA256, ... It is always the same, when in other cases it did not fail, for example:

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.Signature;

public class TestAlgoriths {

    public static void main( String [] args ) {

        try {
            MessageDigest msgDg = MessageDigest.getInstance( "MD5" );

        } catch (NoSuchAlgorithmException e) {
            System.err.println(
                "ERR -> Generando MessageDigest... \n" + e.toString( ));
        }

        try {
            Signature dsa = Signature.getInstance( "MD5" );

        } catch (NoSuchAlgorithmException e) {
            System.err.println(
                "ERR -> Generando Signature... \n" + e.toString( ));
        }
    }

} //class

If I can get the instance of MessageDigest using MD5.

Signatures of the available algorithms:

MD2withRSA
MD5andSHA1withRSA
MD5withRSA
NONEwithDSA
NONEwithECDSA
NONEwithRSA
SHA1withDSA
SHA1withECDSA
SHA1withRSA
SHA224withDSA
SHA224withECDSA
SHA224withRSA
SHA256withDSA
SHA256withECDSA
SHA256withRSA
SHA384withECDSA
SHA384withRSA
SHA512withECDSA
SHA512withRSA

I get them with the following class:

/**
 * Gets a list of signature algorithms supported by your Java installation
 * From: https://stackoverflow.com/questions/35922727
 *
 */
import java.security.Provider;
import java.security.Provider.Service;
import java.security.Security;
import java.util.TreeSet;

public class ShowAlgoriths {

    public static void main( String [] args ) {

        Provider[] providers = Security.getProviders();
        System.out.println("Disponibles " + providers.length + " providers.");

        TreeSet<String> algorithms = new TreeSet<>();

        for (Provider provider : providers) {
            for (Service service : provider.getServices( )) {

                if (service.getType().equals("Signature")) {
                    algorithms.add(service.getAlgorithm( ));
                }
            }
        }

        System.out.println("\nAlgoritmos: ");
        for (String algorithm : algorithms) {
            System.out.println(algorithm);
        }
    }

} //class

Edited:

  

I am working on Windows7 with Netbeans 8.2

     

java version "1.8.0_161"

     

I took the compiled files to another computer, run in console and same result.

    
asked by Orici 13.02.2018 в 19:18
source

1 answer

0

Actually the problem is not getting MessageDigest of MD5 is when trying to get the signature of MD5 , in fact this is incorrect:

  Signature dsa = Signature.getInstance( "MD5" );

The signature for MD5 must be obtained in this way:

  Signature dsa  = Signature.getInstance("MD5withRSA"); 

This is a test to generate the instance of the signature with DSA, SHA1withRSA and MD5withRSA, I can even print your algorithm and its provider:

    
answered by 13.02.2018 / 20:42
source