How to make a Bouncy Castle ECPublicKey

后端 未结 4 816
逝去的感伤
逝去的感伤 2021-02-04 18:14

I know the curve name (secp256k1) and the X and Y coordinates of the EC public key.

How do I make a org.bouncycastle.jce.int

4条回答
  •  天涯浪人
    2021-02-04 18:29

    A shorter variation on Thomas' answer, which does not use a KeyFactory would be:

    public static ECPublicKey decodeKey(byte[] encoded) {
      ECNamedCurveParameterSpec params = ECNamedCurveTable.getParameterSpec("secp256k1");
      ECPublicKeySpec keySpec = new ECPublicKeySpec(params.getCurve().decodePoint(encoded), params);
      return new BCECPublicKey("ECDSA", keySpec, BouncyCastleProvider.CONFIGURATION);
    }
    

    I assume that encoded was obtained by BCECPublicKey.getQ().getEncoded(bool), i.e. it is a byte 0x2-0x4 followed by one or two affine coordinates of the public point on secp256k1.

提交回复
热议问题