Search code examples
javamavenrsabouncycastlesha

Why am I getting this NoSuchProviderException


I am trying to use Bouncy Castle as a security provider in a Maven project. It seems to work fine in certain places in the codebase and works completely fine everywhere for some people. However, if I try something like:

public class Foo {
    public static void main(String[] args) throws Exception {
        Signature signature = Signature.getInstance("SHA256withRSA", "BC");
    }
}

then I get a NoSuchProviderException exception.

In the pom I have Bouncy Castle as a dependency as

<dependency>
          <groupId>bouncycastle</groupId>
          <artifactId>bcprov-jdk16</artifactId>
          <version>140</version>
          <scope>compile</scope>
</dependency>

I have tried with several different JDKs and all give the same error.


Solution

  • As answered in this related SO question, adding the following line should solve your problem.

    Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());