如何解压x9.62

afdcj2ne  于 2021-07-08  发布在  Java
关注(0)|答案(1)|浏览(607)

我正在向我的方法传递一个byte[],并尝试生成一个pubkey。看看这个错误,我首先需要在生成pubkey之前解压。
如何解码压缩坐标?

private static PublicKey getPublicKeyFromBytes(byte[] pubKey) throws NoSuchAlgorithmException, InvalidKeySpecException 
{

        ECPoint point = new ECPoint(new BigInteger(pubKey), BigInteger.ZERO);
        ECNamedCurveParameterSpec spec = ECNamedCurveTable.getParameterSpec("prime256v1");
        KeyFactory kf = KeyFactory.getInstance("ECDSA", new BouncyCastleProvider());
        ECNamedCurveSpec params = new ECNamedCurveSpec("prime256v1", spec.getCurve(), spec.getG(), spec.getN());
        ECPublicKeySpec pubKeySpec = new ECPublicKeySpec(point, params);
        ECPublicKey pk = (ECPublicKey) kf.generatePublic(pubKeySpec);
        return pk;
}

错误消息:

Exception in thread "main" java.security.spec.InvalidKeySpecException: invalid KeySpec: x value invalid for SecP256R1FieldElement
    at org.bouncycastle.jcajce.provider.asymmetric.ec.KeyFactorySpi.engineGeneratePublic(Unknown Source)
rkttyhzu

rkttyhzu1#

如果你的 pubKey 是一个x9.62压缩或未压缩的点,您可以使用 decodePoint() -上的方法 ECCurve :

ECPoint point = spec.getCurve().decodePoint(pubKey);

相关问题