BouncyCastle 错误:无法识别基于 ECDSA 的签名者中的密钥类型

发布于 2024-11-06 09:00:56 字数 675 浏览 0 评论 0原文

我一直在使用 BouncyCastle 的加密库和 RSA 的加密库进行一些简单的测试。我正在做的是生成一个私钥/公钥对,如下所示:

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

然后我像这样生成签名

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

,然后尝试签署私钥:

sig.initSign(priv);

所有这些都会给我带来错误:

java.security.InvalidKeyException:无法识别密钥输入基于 ECDSA 的签名者

当我 BC 和 JsafeJCE 时,我没有收到任何错误,一切都很好。如果两个提供商都是 BC,也适用。那么为什么我无法使用 BC 库对 JsafeJCE 生成的密钥进行签名?

I have been doing some simple testing with BouncyCastle's crypto library and RSA's crypto library. What I am doing is generating a private/public key pair like so:

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 the signature like so

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

and I try to sign the private key:

sig.initSign(priv);

All of this gets me the error:

java.security.InvalidKeyException: can't recognise key type in ECDSA based signer

When I BC and JsafeJCE, I get no error and everything is fine. Works also if both providers are BC. So why is it I can't sign the JsafeJCE generated key with the BC lib?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

浅忆 2024-11-13 09:00:56

Bouncycastle 要求用于其签名实现的私钥必须是它自己的私钥之一。我不知道为什么您无论如何都会为不同的部件选择不同的提供商。

Bouncycastle requires that the private key used for its signing implementation must be one of its own. I don't know why you'd choose different providers for the various pieces in any event.

云朵有点甜 2024-11-13 09:00:56

我通过以下步骤解决了同样的问题:

1)创建静态提供程序:

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

2)生成密钥对:

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

3)如果您想使用密钥签署某些内容:

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

它对我有用,希望也对您有用。

I solved the same issue by following the below steps:

1) Create 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 something using key:

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

It worked for me, and will hopefully work for you as well.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文