title: Asymmetric encryption between android and php
body: <p>The following asymmetric functions are used to encrypt and decrypt in android.. it work perfectly. However, when I encrypt using the function below and decrypt using php or another function(which is quite similar using eclipse).. I get null .. bad padding exception .. I cannot figure out the issue .. as i am encoding the result .. and why does it work only if I encrypt and decrypt in android or in eclipse .. but doesn't work between php and android or simply between two java programs .. but on eclipse and android..</p>
  
  <p>Android Program:</p>
  
  <pre><code>public  String encryptAsymmetric(String input, Key key) throws GeneralSecurityException, IOException {
      byte[] crypted = null;
      try{
  
          Cipher cipher = Cipher.getInstance("RSA");
          cipher.init(Cipher.ENCRYPT_MODE, key);
          crypted = cipher.doFinal(input.getBytes());
      }catch(Exception e){
          System.out.println(e.toString());
      }//Base64.encodeBase64(crypted)
  
      return new String(Base64.encode(crypted, Base64.DEFAULT));
  }
  public  String decryptAsymmetric(String input, Key key){
      byte[] output = null;
      try{
  
          Cipher cipher = Cipher.getInstance("RSA");
          cipher.init(Cipher.DECRYPT_MODE, key);//Base64.decodeBase64(input.getBytes())
          output = cipher.doFinal(Base64.decode(input.getBytes(), Base64.DEFAULT));
      }catch(Exception e){
          System.out.println(e.toString());
      }
      return new String(output);
  }
  </code></pre>
  
  <p>Eclipse (Java program as well):</p>
  
  <pre><code>public static String encryptAsymmetric(String input, Key key){
        byte[] crypted = null;
  
        try{
  
            Cipher cipher = Cipher.getInstance("RSA");
            cipher.init(Cipher.ENCRYPT_MODE, key);
            crypted = cipher.doFinal(input.getBytes());
          }catch(Exception e){
              System.out.println(e.toString());
          }//Base64.encodeBase64(crypted)
          return new String(Base64.getEncoder().encode(crypted));
      }
  public static String decryptAsymmetric(String input, Key key){
      byte[] output = null;
      try{
  
        Cipher cipher = Cipher.getInstance("RSA");
        cipher.init(Cipher.DECRYPT_MODE, key);//Base64.decodeBase64(input.getBytes())
        output = cipher.doFinal(Base64.getDecoder().decode(input.getBytes()));
      }catch(Exception e){
        System.out.println(e.toString());
      }
      return new String(output);
  }
  </code></pre>