在我的場景中,Alice 和 Bob 就使用哪條曲線達成了一致。Alice 生成公鑰和私鑰Alice 將公鑰發送給 BobBob 生成他的密鑰并根據他收到的 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 坐標的原始字節數組,我應該使用哪種格式?
添加回答
舉報
0/150
提交
取消