我正在向我的方法传递一个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)
1条答案
按热度按时间rkttyhzu1#
如果你的
pubKey
是一个x9.62压缩或未压缩的点,您可以使用decodePoint()
-上的方法ECCurve
: