如何从 EC 公钥字节中获取公钥对象?

2022-09-04 01:33:17

我正在开发一个应用程序,该应用程序需要在(NIST P-256,P-256,prime256v1)公钥的帮助下验证签名。SHA256withECDSAsecp256r1

公钥由不同的应用程序在较早的时间点生成,并以十六进制编码存储在我的数据库中。此处的十六进制字符串的格式等效于 OpenSSL 在调用以前由 生成的文件时生成的十六进制字符串。消息和签名是从其他应用程序接收的。请考虑以下测试数据:openssl ec -in x.pem -noout -textx.pemopenssl ecparam -genkey -name secp256r1 -out x.pem

// Stored in Database
byte[] pubKey = DatatypeConverter.parseHexBinary("049a55ad1e210cd113457ccd3465b930c9e7ade5e760ef64b63142dad43a308ed08e2d85632e8ff0322d3c7fda14409eafdc4c5b8ee0882fe885c92e3789c36a7a");

// Received from Other Application
byte[] message = DatatypeConverter.parseHexBinary("54686973206973206a75737420736f6d6520706f696e746c6573732064756d6d7920737472696e672e205468616e6b7320616e7977617920666f722074616b696e67207468652074696d6520746f206465636f6465206974203b2d29");
byte[] signature = DatatypeConverter.parseHexBinary("304402205fef461a4714a18a5ca6dce6d5ab8604f09f3899313a28ab430eb9860f8be9d602203c8d36446be85383af3f2e8630f40c4172543322b5e8973e03fff2309755e654");

现在,这应该是一个有效的签名。

我的目标是使用Java和/或Bouncycastle加密API验证消息的签名。我为此创建了一个方法:isValidSignature

private static boolean isValidSignature(byte[] pubKey, byte[] message,
        byte[] signature) throws NoSuchAlgorithmException, NoSuchProviderException, InvalidKeyException, SignatureException, InvalidKeySpecException {
    Signature ecdsaVerify = Signature.getInstance("SHA256withECDSA", new BouncyCastleProvider());
    ecdsaVerify.initVerify(getPublicKeyFromHex(pubKey));
    ecdsaVerify.update(message);
    return ecdsaVerify.verify(signature);
}

我试图提取公钥:

KeyFactory.generatePublic:

private static PublicKey getPublicKeyFromHex(byte[] pubKey) throws NoSuchAlgorithmException, NoSuchProviderException, InvalidKeySpecException {
    KeyFactory fact = KeyFactory.getInstance("ECDSA", new BouncyCastleProvider());
    return fact.generatePublic(new X509EncodedKeySpec(pubKey));
}

但这会抛出一个(DER 长度超过 4 个字节:26)。我该怎么做来解析它?java.security.spec.InvalidKeySpecException


答案 1

关于椭圆曲线密钥对生成和密钥工厂的充气城堡示例代码让我非常接近。

一旦我设法创建了一个ECDSA密钥工厂和///曲线的曲线规范,我就能够使用它来获得一个曲线点。然后,我可以生成一个公钥规范,使我能够生成如下的公钥:secp256r1NIST P-256P-256prime256v1ECPointUtil.decodePoint

private PublicKey getPublicKeyFromBytes(byte[] pubKey) throws NoSuchAlgorithmException, InvalidKeySpecException {
    ECNamedCurveParameterSpec spec = ECNamedCurveTable.getParameterSpec("prime256v1");
    KeyFactory kf = KeyFactory.getInstance("ECDSA", new BouncyCastleProvider());
    ECNamedCurveSpec params = new ECNamedCurveSpec("prime256v1", spec.getCurve(), spec.getG(), spec.getN());
    ECPoint point =  ECPointUtil.decodePoint(params.getCurve(), pubKey);
    ECPublicKeySpec pubKeySpec = new ECPublicKeySpec(point, params);
    ECPublicKey pk = (ECPublicKey) kf.generatePublic(pubKeySpec);
    return pk;
}

答案 2

推荐