title: How to convert from String to PublicKey?
body: <p>I've used the following code to convert the public and private key to a string</p>
  
  <pre><code>KeyPairGenerator keyPairGen = KeyPairGenerator.getInstance("RSA");
  keyPairGen.initialize(2048);
  KeyPair          keyPair    = keyPairGen.genKeyPair();
  PublicKey        publicKey  = keyPair.getPublic();
  PrivateKey       privateKey = keyPair.getPrivate();
  String publicK = Base64.encodeBase64String(publicKey.getEncoded());
  String privateK = Base64.encodeBase64String(privateKey.getEncoded());
  </code></pre>
  
  <p>Now I'm trying to convert it back to public ad private key</p>
  
  <pre><code>PublicKey publicDecoded = Base64.decodeBase64(publicK);
  </code></pre>
  
  <p>I'm getting error of cannot convert from byte[] to public key. So I tried like this</p>
  
  <pre><code>PublicKey publicDecoded = new SecretKeySpec(Base64.decodeBase64(publicK),"RSA");
  </code></pre>
  
  <p>This leads to error like below</p>
  
  <pre><code>java.security.spec.InvalidKeySpecException: java.security.InvalidKeyException: Neither a public nor a private key
  </code></pre>
  
  <p>Looks like I'm doing wrong key conversion here. Any help would be appreciated.</p>