在我的场景中,Alice 和 Bob 就使用哪条曲线达成了一致。
Alice 生成公钥和私钥
Alice 将公钥发送给 Bob
Bob 生成他的密钥并根据他收到的 Alice 公钥生成会话密钥(或秘密密钥或共享密钥)。
我的问题是 Alice 的公钥实际上是一个点,因此它具有 xy 格式。
我需要将 x,y 坐标字节转换为 ECPublicKey。
这是我正在使用的源代码
// outerPublicKey is the raw bytes from x,y coordinates in hex format
KeyFactory kf = KeyFactory.getInstance("EC");
PublicKey remoteAlicePub = kf.generatePublic(new X509EncodedKeySpec(outerPublicKey));
KeyPairGenerator bobKeyGen = KeyPairGenerator.getInstance("ECDH", "BC");
bobKeyGen.initialize(new ECGenParameterSpec(properties.getCurveName()), new SecureRandom());
KeyPair bobPair = bobKeyGen.generateKeyPair();
ECPublicKey bobPub = (ECPublicKey)bobPair.getPublic();
ECPrivateKey bobPvt = (ECPrivateKey)bobPair.getPrivate();
byte[] bobPubEncoded = bobPub.getEncoded();
byte[] bobPvtEncoded = bobPvt.getEncoded();
KeyAgreement bobKeyAgree = KeyAgreement.getInstance("ECDH");
bobKeyAgree.init(bobPvt);
bobKeyAgree.doPhase(remoteAlicePub, true);
return DatatypeConverter.printHexBinary(bobKeyAgree.generateSecret());
问题是:
new X509EncodedKeySpec(outerPublicKey);
如何从点的 xy 坐标创建公钥?因为outerPublicKey是 x,y 坐标的原始字节数组,我应该使用哪种格式?
相关分类