0

I'm trying to initialize SHA256withRSA Signature object:

Signature signer = Signature.getInstance("SHA256withRSA");

But compiler generates NoSuchAlgorithmException, but when I run this code:

import java.security.Signature;

public static void main(String[] args) throws IOException{
    TreeSet<String> algorithms = new TreeSet<>();
    for (Provider provider : Security.getProviders())
        for (Provider.Service service : provider.getServices())
            if (service.getType().equals("Signature"))
                algorithms.add(service.getAlgorithm());
    for (String algorithm : algorithms)
        System.out.println(algorithm);
}

It creates output:

...
SHA256withECDSAinP1363Format
SHA256withRSA
SHA384withECDSA
...

What i'm doing wrong or what libraries should I include into project?

  • Can you include the code which is not working? – Peter Lawrey Aug 07 '18 at 21:11
  • Thanks for response! Question updated. – alexandrgomd Aug 07 '18 at 21:29
  • 1
    I tried this and the code works. Which version of Java are you using? NOTE: The OpenJDK doesn't have the same cryptograph options as the Oracle JDK. – Peter Lawrey Aug 07 '18 at 21:31
  • That exception is from JVM at runtime, not the compiler. But yes if an algorithm is in the services list you should be able to instantiate it. Your posted code has wrong quotemarks and shouldn't compile at all; make sure your real code doesn't, and if it has been exposed to Windows (which likes to screw up quotemarks like that) also check it doesn't have any invisible characters like a BOM or bidi mark or something. – dave_thompson_085 Aug 07 '18 at 22:44

0 Answers0