How to use BouncyCastle in IntelliJ

Answered

I have problems to use BouncyCastle in my Plugin. If i use my custom gradle or idea built-in library of bouncycastle then i get a SecurityException.

error constructing MAC: java.lang.SecurityException: JCE cannot authenticate the provider BC
java.io.IOException: error constructing MAC: java.lang.SecurityException: JCE cannot authenticate the provider BC

My Code is:

Provider provider = Security.getProvider("BC");
if (provider == null) {
Security.addProvider(new BouncyCastleProvider());
provider = Security.getProvider("BC");
}

KeyStore keyStore = KeyStore.getInstance("PKCS12", provider);
keyStore.load(file.getInputStream(), "123456".toCharArray());

How can i use signed jars of BouncyCastle in IntelliJ to use this as JCE Provider in my Plugin?

1 comment

have create and the issue with description IDEA-181010
d
irty workaround is to load it manually with class loader
```

static {
    Provider bcp = null;
    try {
        ClassLoader cl = TestPlugin.class.getClassLoader();
        URL url =  cl.getResource("org/bouncycastle/jce/provider/BouncyCastleProvider.class");
        if ("jar".equals(url.getProtocol())) {
            url = new URL(url.getPath().substring(0, url.getPath().indexOf('!')));
            cl = new URLClassLoader(new URL[]{url}, null);
            Class cls = cl.loadClass("org.bouncycastle.jce.provider.BouncyCastleProvider");
            bcp = (Provider) cls.newInstance();
        }
    } catch (Exception ignored) {
        // do nothing, bcp will be zero here, probably error message will work
    }
    if (bcp == null) {
        // fallback to normal way, may not work
        bcp = new BouncyCastleProvider();
    }
    Security.addProvider(bcp);
}

```

(https://dkimitsa.github.io/2017/10/22/intellij-idea-bouncy-castle/) for more details 

0

Please sign in to leave a comment.