在我的程序中,我可以使用Spongy Castle(Bouncy Castle等效于Android)从PEM文件成功恢复EC私钥。由于该文件不包含公钥,我试图从私钥重新计算公钥。
我能够计算有效的公钥。但是,我的公钥包含比原始引用(92字节)更多的数据(311字节)。这甚至超过私钥(152字节)。
如何获取公钥的压缩(?)版本
PrivateKey privateKey; // private key from pem file
PublicKey referencePublicKey; // public key in my testing environment, generated with an EC KeyPairGenerator
KeyFactory factory = KeyFactory.getInstance("EC"); // using SpongyCastle provider
ECPrivateKeySpec privSpec = factory.getKeySpec(privateKey, ECPrivateKeySpec.class);
ECParameterSpec params = privSpec.getParams();
ECPoint q = params.getG().multiply(privSpec.getD());
ECPublicKeySpec pubSpec = new ECPublicKeySpec(q, params);
PublicKey publicKey = factory.generatePublic(pubSpec);
System.out.println( referencePublicKey.equals(publicKey) ); // true
System.out.println( referencePublicKey.getEncoded().length ); // 92
System.out.println( publicKey.getEncoded().length ); // 311