我正在尝试在 swift 和 java server 之间开发 key-exchange diffe-Hellman 。我阅读了有关 Diffie hetlman 的所有链接,并且在所有链接中都告诉我应该使用它(g^a mod p)^b mod p = g^ab mod p
来制作 PublicKey。
但是当我开始与服务器共享我的公钥时,我遇到了这个异常
java.security.spec.InvalidKeySpecException: Inappropriate key specification
at com.sun.crypto.provider.DHKeyFactory.engineGeneratePublic(DHKeyFactory.java:85)
at java.security.KeyFactory.generatePublic(KeyFactory.java:334)
at com.pushro.server.managers.server.RequestsHandler.processKeyExchange(RequestsHandler.java:867)
at com.pushro.server.managers.server.RequestsHandler.processPacket(RequestsHandler.java:275)
at com.pushro.server.managers.server.RequestsHandler.processPacket(RequestsHandler.java:1315)
at com.pushro.server.managers.server.RequestsHandler.access$000(RequestsHandler.java:40)
at com.pushro.server.managers.server.RequestsHandler$1.handleEvent(RequestsHandler.java:96)
at com.pushro.server.managers.queue.DisruptorManager$WriteEventHandler.onEvent(DisruptorManager.java:37)
at com.pushro.server.managers.queue.DisruptorManager$WriteEventHandler.onEvent(DisruptorManager.java:34)
at com.lmax.disruptor.BatchEventProcessor.run(BatchEventProcessor.java:129)
at java.lang.Thread.run(Thread.java:748)
Caused by: java.security.InvalidKeyException: Invalid key format
at com.sun.crypto.provider.DHPublicKey.<init>(DHPublicKey.java:127)
at com.sun.crypto.provider.DHKeyFactory.engineGeneratePublic(DHKeyFactory.java:78)
... 10 more
我使用标准 X509EncodedKeySpec 获取密钥并在 java 中生成密码。
X509EncodedKeySpec ks = new X509EncodedKeySpec(PublicKey_byte);
KeyFactory keyFactory = KeyFactory.getInstance("DH");
PublicKey clientPublicKey = keyFactory.generatePublic(ks);
我已阅读所有此链接,但他们没有帮助我。
- https://github.com/gsurma/diffie_hellman_key_exchange
- https://gist.github.com/jmcd/b9a212df5dfa88b6d8dcb52c51b6be6b
- https://developer.apple.com/documentation/security/1643701-seckeycreatewithdata
- https://developer.apple.com/documentation/security/seckey
- https://developer.apple.com/documentation/security/secure_transport#//apple_ref/doc/uid/TP30000155