我正在尝试使用 javax.crypto.KeyAgreement 生成共享密钥:
public static byte[] generateSharedSecret(KeyPair localKeyPair, PublicKey clientPublicKey) {
byte[] sharedSecret = {};
try {
KeyAgreement keyAgreement = KeyAgreement.getInstance("ECDH");
keyAgreement.init(localKeyPair.getPrivate());
keyAgreement.doPhase(clientPublicKey, true);
sharedSecret = keyAgreement.generateSecret();
MessageDigest hash = MessageDigest.getInstance("SHA-256");
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (InvalidKeyException e) {
e.printStackTrace();
}
return sharedSecret;
}
但是在运行程序后我给出了这个错误:
Exception in thread "main" java.lang.IllegalStateException
at sun.security.ec.ECDHKeyAgreement.deriveKey(Native Method)
at sun.security.ec.ECDHKeyAgreement.engineGenerateSecret(ECDHKeyAgreement.java:130)
at javax.crypto.KeyAgreement.generateSecret(KeyAgreement.java:586)
at io.softitecture.jpushup.keygen.ECDHKeyGenerator.generateSharedSecret(ECDHKeyGenerator.java:47)
at io.softitecture.jpushup.payload.PayloadEncryptor.encrypt(PayloadEncryptor.java:19)
at io.softitecture.App.main(App.java:15)
你能帮我解决这个问题并告诉我哪里出错了吗?