title: Public/private key encryption Android-PHP
body: <p>I'm trying to encrypt data in android side and decrypt it in php side
  i'm using <b>phpseclib</b> in php to generate public/private keys</p>
  
  <p>after i generate keys this public key i got in <b>PHP side</b> : </p>
  
  <pre><code>-----BEGIN RSA PUBLIC KEY-----".
              "MIGJAoGBAKks62Itns2uU/dVZJ4kCkMinHgyeh/rdMD53a4Zu2a76OIJvdSZ8q4c".
              "YTWvPj0giefVtMc7tV4c6AAw04jyIfmCTvcQUlHI+sspHxXDlQTagNoxCuA29b5L".
              "9MKO6Ok0LwF9rGgTywC1heNEulZz9ISn9FQDazJT+Bd9cnNOrJRdAgMBAAE=".
              "-----END RSA PUBLIC KEY-----
  </code></pre>
  
  <p>and then i encoded it to base64 and got this <b>base64 encoded key</b> </p>
  
  <pre><code>LS0tLS1CRUdJTiBSU0EgUFVCTElDIEtFWS0tLS0tTUlHSkFvR0JBS2tzNjJJdG5zMnVVL2RWWko0a0NrTWluSGd5ZWgvcmRNRDUzYTRadTJhNzZPSUp2ZFNaOHE0Y1lUV3ZQajBnaWVmVnRNYzd0VjRjNkFBdzA0anlJZm1DVHZjUVVsSEkrc3NwSHhYRGxRVGFnTm94Q3VBMjliNUw5TUtPNk9rMEx3RjlyR2dUeXdDMWhlTkV1bFp6OUlTbjlGUURhekpUK0JkOWNuTk9ySlJkQWdNQkFBRT0tLS0tLUVORCBSU0EgUFVCTElDIEtFWS0tLS0t
  </code></pre>
  
  <p>i copied it to android side to encrypt data using it but i got <b>InvalidKeySpecException</b></p>
  
  <p><b>Android Side Code: </b></p>
  
  <pre><code>public static byte[] encrypt(String text) {
  
          byte[] encodedPublicKey= Base64.decode("LS0tLS1CRUdJTiBSU0EgUFVCTElDIEtFWS0tLS0tTUlHSkFvR0JBS2tzNjJJdG5zMnVVL2RWWko0a0NrTWluSGd5ZWgvcmRNRDUzYTRadTJhNzZPSUp2ZFNaOHE0Y1lUV3ZQajBnaWVmVnRNYzd0VjRjNkFBdzA0anlJZm1DVHZjUVVsSEkrc3NwSHhYRGxRVGFnTm94Q3VBMjliNUw5TUtPNk9rMEx3RjlyR2dUeXdDMWhlTkV1bFp6OUlTbjlGUURhekpUK0JkOWNuTk9ySlJkQWdNQkFBRT0tLS0tLUVORCBSU0EgUFVCTElDIEtFWS0tLS0t", Base64.DEFAULT);
  
  
  
     PublicKey publicKey=null;
      KeyFactory keyFactory = null;
      try {
          keyFactory = KeyFactory.getInstance("RSA");
          X509EncodedKeySpec publicKeySpec = new X509EncodedKeySpec(encodedPublicKey);
          publicKey = keyFactory.generatePublic(publicKeySpec);
  
      } catch (NoSuchAlgorithmException e) {
          e.printStackTrace();
      }catch (InvalidKeySpecException e) {
          e.printStackTrace();
      }
  
  
      byte[] cipherText = null;
      try {
          // get an RSA cipher object and print the provider
          final Cipher cipher = Cipher.getInstance("RSA");
          // encrypt the plain text using the public key
          cipher.init(Cipher.ENCRYPT_MODE, publicKey);
          cipherText = cipher.doFinal(text.getBytes());
      } catch (Exception e) {
          e.printStackTrace();
      }
      return cipherText;
  }
  </code></pre>