ConsenSysMesh / cava

ConsenSys core libraries for Java & Kotlin
Apache License 2.0
84 stars 34 forks source link

java.lang.IllegalStateException: BouncyCastleProvider is not available #212

Closed wangjunbao2018 closed 5 years ago

wangjunbao2018 commented 5 years ago

Hi there,

I hava added cava to my project, add then I used the SECP256K1 class to sign my data, but some errors happened. Is there anyone met the problem and tells me why? thx

The error message is like this:

Exception in thread "main" java.lang.ExceptionInInitializerError
    at net.consensys.cava.crypto.SECP256K1$PublicKey.fromSecretKey(SECP256K1.java:527)
    at net.consensys.cava.crypto.SECP256K1$KeyPair.fromSecretKey(SECP256K1.java:684)
    at one.contentbox.boxd.samples.SignSample.main(SignSample.java:40)
Caused by: java.lang.IllegalStateException: BouncyCastleProvider is not available, see https://www.bouncycastle.org/wiki/display/JA1/Provider+Installation
    at net.consensys.cava.crypto.SECP256K1$Parameters.<clinit>(SECP256K1.java:111)
    ... 3 more
Caused by: java.security.NoSuchProviderException: no such provider: BC
    at sun.security.jca.GetInstance.getService(GetInstance.java:83)
    at sun.security.jca.GetInstance.getInstance(GetInstance.java:206)
    at java.security.KeyPairGenerator.getInstance(KeyPairGenerator.java:279)
    at net.consensys.cava.crypto.SECP256K1$Parameters.<clinit>(SECP256K1.java:109)
    ... 3 more

My code is like this:

String privKeyHex = "29fbf01166fc31c941cadc1659a5f684f81c22c1113e5aa5b0af28b7dd453269";
BigInteger privKeyBigInteger = new BigInteger(privKeyHex, 16);
String data = "46f7f67f515e7d053525459991c7a6b3e673950809af776324f46d91df9e600d";

 SECP256K1.Signature signature = SECP256K1
                .sign(Hex.decode(data), SECP256K1.KeyPair.
                        fromSecretKey(SECP256K1.SecretKey.fromInteger(privKeyBigInteger)));

byte[] data  = signature.bytes().toArray();
wangjunbao2018 commented 5 years ago

For more infomation, I'm sure org.bouncycastle:bcprov-jdk15on is already added to classpath.

atoulme commented 5 years ago

Hello! Thank you for your interest!

It's not enough to add bouncy castle to the classpath. You also need to add it to the security providers of the JDK. See https://people.eecs.berkeley.edu/~jonah/bc/org/bouncycastle/jce/provider/BouncyCastleProvider.html