在 Java 中,我有一个以字节数组形式发送的 ECDH 公钥。
收到字节数组后,我该如何将其转回公钥?
我正在使用 Bouncy CaSTLe,但 Java 解决方案同样有用。
谢谢
最佳答案
当您获得编码 key 时,假设您使用默认的“[your keyPair].getPublic().getEncoded()”方法,这将起作用。
X509EncodedKeySpec ks = new X509EncodedKeySpec(pubKeyByteString.toByteArray());
KeyFactory kf;
try {
kf = java.security.KeyFactory.getInstance("ECDH");
} catch (NoSuchAlgorithmException e) {
log.error("Cryptography error: could not initialize ECDH keyfactory!", e);
return;
}
ECPublicKey remotePublicKey;
try {
remotePublicKey = (ECPublicKey)kf.generatePublic(ks);
} catch (InvalidKeySpecException e) {
log.warn("Received invalid key specification from client",e);
return;
} catch (ClassCastException e) {
log.warn("Received valid X.509 key from client but it was not EC Public Key material",e);
return;
}
关于java - 在 Bouncy CaSTLe 中将字节编码的 key 转回其原始 ECPublicKey,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2218879/