title: 'No installed provider supports this key: sun.security.provider.DSAPublicKeyImpl' while using RSA encryption
body: <p>I'm trying to encrypt a text file using a key I generated.
  Now I should encrypt this key with RSA algorithm, using the public key of the side that eventually will receive the encrypted data.</p>
  
  <p>This is my code so far:</p>
  
  <pre><code>package cryptogaphy;
  
  
  import java.io.FileInputStream;
  import java.io.FileOutputStream;
  import java.io.IOException;
  import java.io.InputStream;
  import java.io.OutputStream;
  
  import javax.crypto.Cipher;
  import javax.crypto.CipherInputStream;
  import javax.crypto.KeyGenerator;
  import javax.crypto.SecretKey;
  import javax.crypto.spec.IvParameterSpec;
  
  import java.security.PublicKey;
  import java.security.SecureRandom;
  import java.security.cert.Certificate;
  import java.security.KeyPair;
  import java.security.Key;
  import java.security.KeyStore;
  import java.security.KeyStoreException;
  import java.security.NoSuchAlgorithmException;
  import java.security.PrivateKey;
  import java.security.UnrecoverableKeyException;
  import java.security.cert.CertificateException;
  
  
  
  public class trythree {
  
  public static void main(String[] args) throws Exception {
  
      try {
  
      // Generate a symmetric key
      KeyGenerator keyGenerator = KeyGenerator.getInstance("AES");
      SecureRandom secureRandom = new SecureRandom();
      int keyBitSize = 128;
      keyGenerator.init(keyBitSize, secureRandom);
      SecretKey symmetricKey = keyGenerator.generateKey();
      Key key = symmetricKey;
  
      // Generate random IV
      byte[] ivBytes = new byte[16];
      IvParameterSpec iv = new IvParameterSpec(ivBytes);
  
      System.out.println("Random Iv: " + iv);
  
      // File Encryption
      FileInputStream fis = new FileInputStream("C:\\Users\\Victoria\\Desktop\\plainData.txt");
      FileOutputStream fos = new FileOutputStream("C:\\Users\\Victoria\\Desktop\\encryptedData.txt");
  
      Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
  
      cipher.init(Cipher.ENCRYPT_MODE, key, iv, SecureRandom.getInstance("SHA1PRNG"));
      CipherInputStream cis = new CipherInputStream(fis, cipher);
      write(cis, fos);
  
  
      // load Alice's keystore - input method should be changed
      char[] aliceStorePass = "123456".toCharArray();
      char[] aliceKeypass = "aliceKey".toCharArray();
  
      FileInputStream aliceInput = new FileInputStream("C:\\Users\\Victoria\\Desktop\\aliceKeystore.jks");
      KeyStore aliceKeystore = KeyStore.getInstance("JKS");
      aliceKeystore.load(aliceInput, aliceStorePass);
  
      String alias = "alice";
      String alias2 = "bobcert";
  
      Key aliceKey = aliceKeystore.getKey(alias, aliceKeypass);
      PublicKey bobPublicKey = null;
  
      // retrieve Alice's private key and Bob's public key from Alice's keystore
      if (aliceKey instanceof PrivateKey) {
        Certificate cert = aliceKeystore.getCertificate(alias2);
        bobPublicKey = cert.getPublicKey();
        new KeyPair(bobPublicKey, (PrivateKey) aliceKey);
        System.out.println((PrivateKey) aliceKey);
      }
      System.out.println("Bob's public key: " + bobPublicKey);
  
      // Encrypt the Symmetric Key with asymmetric RSA algorithm
      byte[] encryptedkey = symmetricKey.getEncoded();
      Cipher keyCipher = Cipher.getInstance("RSA");  
      keyCipher.init(Cipher.ENCRYPT_MODE, bobPublicKey );  
      keyCipher.doFinal(encryptedkey);
  
      System.out.println("Asymmetric Secret Key: " + encryptedkey);
      System.out.println("Symmetric Key: " + key);
  
  
  
  } catch (KeyStoreException | NoSuchAlgorithmException | UnrecoverableKeyException | CertificateException e){
  System.out.println(e); 
  }
  
      }
  
  
  
      private static void write(InputStream in, OutputStream out) throws IOException {
          byte[] buf = new byte[64];
          int numOfBytesRead;
          while((numOfBytesRead = in.read(buf))!=-1)
               {
                   out.write(buf, 0 ,numOfBytesRead);
               }     
             out.close();
             in.close();  
             System.out.println("-----ENCRYPTION COMPLETED!-----");
      }
  }
  </code></pre>
  
  <p>Now, I get this error-</p>
  
  <p><strong>java.security.InvalidKeyException: No installed provider supports this key: sun.security.provider.DSAPublicKeyImpl</strong></p>
  
  <p>The error mentions DSA, although I used RSA.
  Any ideas how to solve it? any help would be appreciated!</p>