Java 使用ECPublicKey的InvalidKeyException

Java 使用ECPublicKey的InvalidKeyException,java,encryption,public-key-encryption,pki,key-generator,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

当我尝试使用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);

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

最后,我找到了此异常的来源。问题是密码的初始化:

//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");
但现在,我有另一个例外(它没有前一个重要):


那么现在我必须使用什么作为ECDSA公钥的加密算法呢?

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

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