title: RSA in Java given n and e
body: <p>My application receives the raw pieces of a public RSA key (n and e) and needs to use these to encrypt a cipher text.  I've been trying to use BouncyCastle but my code isn't working.  The problem arises in trying to create the X509EncodedKeySpec.</p>
  
  <p>Can anyone help me get this working?  Here's the code I have:</p>
  
  <pre><code>public static PublicKey getPublicKeyFromString(String key) throws Exception
  {
      KeyFactory keyFactory = KeyFactory.getInstance(ALGORITHM);
      EncodedKeySpec publicKeySpec = new X509EncodedKeySpec(Base64Encoder.decode(key));
  
      PublicKey publicKey = keyFactory.generatePublic(publicKeySpec);
      return publicKey;
  }
  </code></pre>
  
  <p>I guess the real problem is that n and e are separate and I don't know how to combine them.</p>