title: Android: InvalidKeySpecException: encoded key spec not recognised
body: <p>When I try to make RSA public key from string. I get exception mentioned below. I am trying to verify generated keys (string, using Swift) in Android. How to convert the string to private/public key?</p>
  
  <pre><code>static PublicKey getRSAPublicKeyFromString (String publicKeyPEM){
      try {
          KeyFactory keyFactory = KeyFactory.getInstance("RSA", "SC");
          byte[] publicKeyBytes = Base64.decode(publicKeyPEM.getBytes("UTF-8"), Base64.DEFAULT);
          X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(publicKeyBytes);
          return keyFactory.generatePublic(x509KeySpec);
      }catch (Exception e){
          e.printStackTrace ();
      }
  
      return null;
  }
  </code></pre>
  
  <p>Exception</p>
  
  <pre><code>java.security.spec.InvalidKeySpecException: encoded key spec not recognised
  at org.spongycastle.jcajce.provider.asymmetric.util.BaseKeyFactorySpi.engineGeneratePublic(BaseKeyFactorySpi.java:52)
  at org.spongycastle.jcajce.provider.asymmetric.rsa.KeyFactorySpi.engineGeneratePublic(KeyFactorySpi.java:130)
  at java.security.KeyFactory.generatePublic(KeyFactory.java:172)
  </code></pre>
  
  <p>public key in ios:</p>
  
  <pre><code>MIGJAoGBAOibWF15OhLJQAWpQN++UFqyG+2uLEG8PPvCajwwh6FWi6jLiZ9VN+l3JpiJTQwF/yxlSJCZCoYnljKeRBnNxpzBnuTJsyuZ4hI8ehLzDvh9nYzX/TlcZYVshJzyIVRZAbuFOCLNIltjB+Qmw8YGlxzeDSFaB6eEC1OP49hDakURAgMBAAE=
  </code></pre>