2015-05-07 23 views
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); 

我必須添加一個提供程序來支持此公鑰嗎?

+0

您正在嘗試將ECC算法與ECC密鑰一起使用。要使用AES(直接,而不是基於密碼的混合等),您需要使用AES密鑰,而不是任何種類的ECC密鑰。要使用ECC密鑰進行加密,您需要使用使用ECC密鑰的*算法*。標準Sun/Oracle JCE沒有ECC,唯一簽名(ECDSA)和密鑰簽名(ECDH)的任何加密算法。 BouncyCastle顯然支持ECIES,它執行混合ECC /對稱加密,但我沒有嘗試過。如果您通過JCA使用BouncyCastle,則需要將其作爲提供者添加。 –

+0

謝謝@ dave_thompson_085。我使用ECC密鑰,因爲我使用ECDSA進行簽名。我會嘗試BouncyCastle,我會給你答案。 – Hakim

回答

0

最後,我發現這個異常的來源。問題是密碼初始化:

//This is the wrong initialization 
Cipher cipher = Cipher.getInstance("AES/CTR/NoPadding"); 

//This is the right initialization 
Cipher cipher = Cipher.getInstance("AES/CTR/NoPadding","SunJCE"); 

但現在,我還有一個例外,這是(這是比以前少了一個重要):

java.security.InvalidKeyException: Invalid AES key length: 170 bytes 

所以我必須與加密算法使用ECDSA公共密鑰現在?

相關問題