title: Effective Implementation of RSA Public Key Generation and Encryption in Java
body: <p>I am currently trying to write a program that will utilize a public key cryptosystem such as RSA or ElGamal.  I have been looking at different sources, and the closest I have gotten was in the <a href="https://bouncycastle.org/fips/BCUserGuide.pdf" rel="nofollow noreferrer">Bouncy Castle FIPS documentation</a> of public key encryption, where the sample code for RSA is somewhat simple:</p>
  
  <pre><code>public byte[] pkcs1Encrypt(RSAPublicKey pubKey, byte[] data) {    
     Cipher c = Cipher.getInstance(“RSA/NONE/PKCS1Padding”, “BCFIPS”);
     c.init(Cipher.ENCRYPT_MODE, pubKey);
     return c.doFinal(data);
  } 
  </code></pre>
  
  <p>I've worked often with symmetric key cryptosystems such as AES and Triple-DES (DESede), but I looked under the Bouncy Castle documentation, and found out that the <code>RSAPublicKey</code> is not a sub-interface/class of the <code>SecretKey</code> class. </p>
  
  <p>Is there any way to generate this <code>RSAPublicKey</code> object, or is there a more efficient way to implement this kind of encryption with Bouncy Castle or the JCE</p>