Java – bouncycastle error: the key type in the ECDSA based signer is not recognized

I have been using bouncy castle's encryption library and RSA's encryption library for some simple tests What I'm doing is generating private / public key pairs, as follows:

KeyPairGenerator kpg = KeyPairGenerator.getInstance("EC","JsafeJCE");
kpg.initialize(new ECGenParameterSpec("secp384r1"));
KeyPair kp = kpg.genKeyPair();
PrivateKey priv = kp.getPrivate();
PublicKey pub = kp.getPublic();

Then I generate such a signature

Signature sig = Signature.getInstance("SHA384/ECDSA","BC");

I tried to sign the private key:

sig.initSign(priv);

All this makes me wrong:

java. security. Invalidkeyexception: the key type in the ECDSA based signer is not recognized

When I BC and jsafejce, I didn't get any mistakes and everything was fine It is also possible if both providers are British Columbia So why can't I sign the key generated by jsafejce with BC lib?

Solution

I solved the same problem as follows:

1) To create a static provider:

private static BouncyCastleProvider bouncyCastleProvider;
public static final BouncyCastleProvider BOUNCY_CASTLE_PROVIDER = new BouncyCastleProvider();
static {
    bouncyCastleProvider = BOUNCY_CASTLE_PROVIDER;
}

2) Generate keypair:

KeyPairGenerator g = KeyPairGenerator.getInstance("ECDSA",bouncyCastleProvider);
g.initialize(ecSpec,random);
KeyPair keyPair = g.generateKeyPair();

3) If you want to sign with a key:

Signature signature = Signature.getInstance("SHA256withECDSA",bouncyCastleProvider);
signature.initSign(privateKey);
signature.update(signedData);
signature.sign();

It works for me and I hope it works for you

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>