将字节编码的密钥转回 Bouncy Castle 中的原始 ECPublicKey

发布于 2024-08-21 03:41:55 字数 126 浏览 4 评论 0原文

在 Java 中,我有一个 ECDH 公钥,我将其作为字节数组发送。

一旦我收到字节数组,如何将其转回公钥?

我正在使用 Bouncy Castle,但 Java 解决方案也同样有用。

谢谢

In Java I have a ECDH public Key that I am sending as a byte array.

Once I have received the byte array how can I turn it back into a public key?

I am using Bouncy Castle but a Java solution would be just as useful.

Thanks

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

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

发布评论

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

评论(3

秋风の叶未落 2024-08-28 03:41:55

当您获得编码密钥时,假设您使用默认的“[your keyPair].getPublic().getEncoded()”方法,这将起作用。

    X509EncodedKeySpec ks = new X509EncodedKeySpec(pubKeyByteString.toByteArray());
    KeyFactory kf;
    try {
         kf = java.security.KeyFactory.getInstance("ECDH");
    } catch (NoSuchAlgorithmException e) {
        log.error("Cryptography error: could not initialize ECDH keyfactory!", e);
        return;
    }

    ECPublicKey remotePublicKey;

    try {
        remotePublicKey = (ECPublicKey)kf.generatePublic(ks);
    } catch (InvalidKeySpecException e) {
        log.warn("Received invalid key specification from client",e);
        return;
    } catch (ClassCastException e) {
        log.warn("Received valid X.509 key from client but it was not EC Public Key material",e);
        return;
    }

When you got the encoded key, assuming you used the default "[your keyPair].getPublic().getEncoded()" method, this will work.

    X509EncodedKeySpec ks = new X509EncodedKeySpec(pubKeyByteString.toByteArray());
    KeyFactory kf;
    try {
         kf = java.security.KeyFactory.getInstance("ECDH");
    } catch (NoSuchAlgorithmException e) {
        log.error("Cryptography error: could not initialize ECDH keyfactory!", e);
        return;
    }

    ECPublicKey remotePublicKey;

    try {
        remotePublicKey = (ECPublicKey)kf.generatePublic(ks);
    } catch (InvalidKeySpecException e) {
        log.warn("Received invalid key specification from client",e);
        return;
    } catch (ClassCastException e) {
        log.warn("Received valid X.509 key from client but it was not EC Public Key material",e);
        return;
    }
猥︴琐丶欲为 2024-08-28 03:41:55

我发现@LaceCard 的上述解决方案对我不起作用。一般来说,这并不明显,但密码学中没有什么是明显的;)

String key = "MihU9ztW9sEvkBL6BxyaOMgkSbodNS8yoHaHcio+WE...blahblah"
byte[] keyBytes = Base64.decode(key);

//If using Android and Spongycastle provider should be "SC"
KeyFactory kf = KeyFactory.getInstance("ECDH", "BC");
//CURVE_NAME e.g prime192v1
ECParameterSpec ecSpec = ECNamedCurveTable.getParameterSpec(CURVE_NAME);
ECPoint point = ecSpec.getCurve().decodePoint(keyBytes);
ECPublicKeySpec pubSpec = new ECPublicKeySpec(point, ecSpec);

ECPublicKey myECPublicKey = (ECPublicKey) kf.generatePublic(pubSpec);

注意:您需要适当地处理潜在的异常

I found the above solution by @LaceCard didn't work for me. In general this isn't obvious but then again nothing in cryptography is ;)

String key = "MihU9ztW9sEvkBL6BxyaOMgkSbodNS8yoHaHcio+WE...blahblah"
byte[] keyBytes = Base64.decode(key);

//If using Android and Spongycastle provider should be "SC"
KeyFactory kf = KeyFactory.getInstance("ECDH", "BC");
//CURVE_NAME e.g prime192v1
ECParameterSpec ecSpec = ECNamedCurveTable.getParameterSpec(CURVE_NAME);
ECPoint point = ecSpec.getCurve().decodePoint(keyBytes);
ECPublicKeySpec pubSpec = new ECPublicKeySpec(point, ecSpec);

ECPublicKey myECPublicKey = (ECPublicKey) kf.generatePublic(pubSpec);

Note: you'll need to handle the potential exceptions appropriately

秋风の叶未落 2024-08-28 03:41:55

ECDH 密钥的原始字节如何格式化?你从哪里获得原始字节?

通常,使用适当的 *Spec 类将原始密钥材料转换为密钥,但 ECPublicKeySpecDHPublicKeySpec 类不接受原始字节数组。

How are the raw bytes formatted for the ECDH key? Where are you getting the raw bytes?

Generally, one uses the appropriate *Spec class to turn raw key material into a Key but the ECPublicKeySpec and DHPublicKeySpec classes don't accept a raw byte array.

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