title: How can convert android PublicKey to string and back?
body: <p>I am trying to create an app which will encrypt user messages. The user Public Key needs to be published to server as a string. I am generating Android KyeStore PublicKey like this:</p>
  
  <pre><code>    public static PublicKey getOrCreatePublicKey(String alias) throws GeneralSecurityException, IOException {
  
      KeyStore keyStore = KeyStore.getInstance(ANDROID_PROVIDER);
      keyStore.load(null);
      if (!keyStore.containsAlias(alias) || keyStore.getCertificate(alias) == null) {
  
          KeyGenParameterSpec spec = new KeyGenParameterSpec.Builder(
                  alias,
                  KeyProperties.PURPOSE_DECRYPT | KeyProperties.PURPOSE_ENCRYPT)
                  .setEncryptionPaddings(KeyProperties.ENCRYPTION_PADDING_RSA_OAEP)
                  .setDigests(KeyProperties.DIGEST_SHA256,
                          KeyProperties.DIGEST_SHA512)
                  .build();
          KeyPairGenerator generator = KeyPairGenerator.getInstance(KeyProperties.KEY_ALGORITHM_RSA, ANDROID_PROVIDER);
          generator.initialize(spec);
          generator.generateKeyPair();
      }
      return keyStore.getCertificate(alias).getPublicKey();
  }
  </code></pre>
  
  <p>Then I try to convert the PublicKey to string and back to PublicKey like this:</p>
  
  <pre><code>public static PublicKey stringToPublicKey(String publStr)  {
  
      PublicKey publicKey = null;
      try {
          byte[] data = Base64.decode(publStr, Base64.DEFAULT);
          X509EncodedKeySpec spec = new X509EncodedKeySpec(data);
          KeyFactory fact = KeyFactory.getInstance("RSA");
          publicKey = fact.generatePublic(spec);
      } catch (GeneralSecurityException e) {
          e.printStackTrace();
      }
      return publicKey;
  }
  
  
  public static String publicKeyToString(PublicKey publ) {
      String publicKeyString = null;
      try {
          KeyFactory fact = KeyFactory.getInstance("RSA");
          X509EncodedKeySpec spec = fact.getKeySpec(publ,
                  X509EncodedKeySpec.class);
          publicKeyString = Base64.encodeToString(spec.getEncoded(), Base64.DEFAULT);
      } catch (GeneralSecurityException e) {
          e.printStackTrace();
      }
      return publicKeyString;
  }
  </code></pre>
  
  <p>Then I try to encrypt and decrypt user messages with the Public and Private keys.</p>
  
  <p>If I don't convert Public Key the encryption works fine. But if I convert Public Key to string and back to Public Key the encryption doesn't work anymore.
  What am I doing wrong? Thank you.</p>
  
  <pre><code>   PublicKey publicKey1 = getOrCreatePublicKey("alias");
          String publicKeyStr = publicKeyToString(publicKey1);
          PublicKey publicKey2 = stringToPublicKey(publicKeyStr);
  
      //this one works            
          String message = encrypt(str, publicKey1);
          String decryptm = decrypt(message, privateKey);
  
      //this one doesn't work
      String message = encrypt(str, publicKey2);
          String decryptm = decrypt(message, privateKey);
  </code></pre>