0

I'm getting the following exception when I try to encrypt a byte array with a EC public key:

java.security.InvalidKeyException: No installed provider supports this key: sun.security.ec.ECPublicKeyImpl

This exception is generated when I call Cipher.init(). The lines below show what I did in my program:

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);

Must I add a provider to support this public key?

Hexaholic
  • 3,299
  • 7
  • 30
  • 39
Hakim
  • 434
  • 7
  • 21

3 Answers3

0

Finally, I found the source of this exception. The problem was initialization of cipher :

//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");

But now, i have another exception which is (it is less important than the previous one) :

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

So what must I use as encrypting algorithm with ECDSA public key now ?

Hakim
  • 434
  • 7
  • 21
0

ECDSA is not used for encryption rather you use RSA / Symetric cipher

0
  KeyGenerator keygen = KeyGenerator.getInstance("AES");
  keygen.init(128);
  key = keygen.generateKey();
String plain_input = "Hush-a-bye, baby, on the tree top,When the wind blows" ; 
  //encryption
  cipher = Cipher.getInstance(""AES/EBC/PKCS5Padding");
  cipher.init(Cipher.ENCRYPT_MODE, key);
  byte[] encrypted = cipher.doFinal(plain_input.getBytes("UTF8"));

  //decryption
  cipher = Cipher.getInstance(""AES/EBC/PKCS5Padding");
  cipher.init(Cipher.DECRYPT_MODE, key);
  byte[] decrypted = cipher.doFinal(encrypted);
  String plain_output = new String(decrypted, "UTF8");