使用ECPublicKey的InvalidKeyException

时间:2015-05-07 14:12:57

标签: java encryption public-key-encryption pki key-generator

当我尝试使用EC公钥加密字节数组时,我收到以下异常:

  

java.security.InvalidKeyException:没有安装的提供程序支持此功能   键:                                          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);

我必须添加一个提供程序来支持此公钥吗?

3 个答案:

答案 0 :(得分: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公钥?

答案 1 :(得分:0)

ECDSA不用于加密 而是使用RSA / Symetric密码

答案 2 :(得分:0)

template <typename... Args>
Container(Args&&... args) : dat(std::forward<Args>(args)...) { }