0
,我發現了以下情況例外,當我嘗試用EC公共密鑰加密的字節數組:InvalidKeyException將使用ECPublicKey
java.security.InvalidKeyException: No installed provider supports this key:
sun.security.ec.ECPublicKeyImpl
當我打電話Cipher.init產生此異常()。下面的行顯示我在我的程序中做了什麼:
ECPublicKey publicKey ;
ECPrivateKey privateKey;
//Generating key paire (public and private keys)
KeyPairGenerator keyGen = KeyPairGenerator.getInstance("EC", "SunEC");
SecureRandom random = SecureRandom.getInstance("SHA1PRNG", "SUN");
keyGen.initialize(571, random);
KeyPair pair = keyGen.generateKeyPair();
privateKey = (ECPrivateKey) pair.getPrivate();
publicKey = (ECPublicKey) pair.getPublic();
// get an AES cipher object with CTR encription mode
Cipher cipher = Cipher.getInstance("AES/CTR/NoPadding");
// encrypt the sharedSecret using the public key
cipher.init(Cipher.ENCRYPT_MODE, publicKey);**
byte[] result = cipher.doFinal(data);
我必須添加一個提供程序來支持此公鑰嗎?
您正在嘗試將ECC算法與ECC密鑰一起使用。要使用AES(直接,而不是基於密碼的混合等),您需要使用AES密鑰,而不是任何種類的ECC密鑰。要使用ECC密鑰進行加密,您需要使用使用ECC密鑰的*算法*。標準Sun/Oracle JCE沒有ECC,唯一簽名(ECDSA)和密鑰簽名(ECDH)的任何加密算法。 BouncyCastle顯然支持ECIES,它執行混合ECC /對稱加密,但我沒有嘗試過。如果您通過JCA使用BouncyCastle,則需要將其作爲提供者添加。 –
謝謝@ dave_thompson_085。我使用ECC密鑰,因爲我使用ECDSA進行簽名。我會嘗試BouncyCastle,我會給你答案。 – Hakim