title: Multi-platform Base64 Encoding and decoding problems(Android to PC)
body: <p>Okay so here is the situation I am facing. I am building an android app with a login screen and encrypting the information to send to my server. After encrypting I am encoding the information using Base64 on android to send it to the server on my pc which is decoding in Base64 but it isn't doing it correctly. My server reports a encryption padding error. </p>
  
  <p>This is the code for the encryption on Android:</p>
  
  <pre><code>import android.util.Base64;
  
      try {
          plainText = user.getBytes("UTF-8");
          user = EncryptInfo(plainText, publicKey);
      } catch (UnsupportedEncodingException ex) {
          ex.getStackTrace();
      }
  
  private static String EncryptInfo(byte[] data,PublicKey key){
      Cipher cipher;
      byte[] cipherText = null;
      try {
          cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
          cipher.init(Cipher.ENCRYPT_MODE, key);
          cipherText = cipher.doFinal(data);
      } catch (NoSuchAlgorithmException ex) {
          ex.getStackTrace();
          System.exit(1);
      } catch (NoSuchPaddingException ex) {
          ex.getStackTrace();
          System.exit(1);
      } catch (InvalidKeyException ex) {
          ex.getStackTrace();
          System.exit(1);
      } catch (IllegalBlockSizeException ex) {
          ex.getStackTrace();
          System.exit(1);
      } catch (BadPaddingException ex) {
          ex.getStackTrace();
          System.exit(1);
      }
  
      return Base64.encodeToString(cipherText, Base64.DEFAULT);
  }
  </code></pre>
  
  <p>This is the code running on my pc on the server decoding the data:</p>
  
  <pre><code>import java.util.Base64;
  
  //Decrypt the userpassword
  byte[] plainText = Base64.getDecoder().decode(encryptedData);
  inputLine = decryptData(plainText);
  
  private String decryptData(byte[] cipherText) throws UnsupportedEncodingException{  
      // decrypt the ciphertext using the private key
      Cipher cipher;
      byte[] newPlainText = null;
      try {
          cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
          cipher.init(Cipher.DECRYPT_MODE, privateKey);
          newPlainText = cipher.doFinal(cipherText);
          //System.out.println( "Finish decryption: " );
          //System.out.println( new String(newPlainText, "UTF8") );
      } catch (NoSuchAlgorithmException ex) {
          Logger.getLogger(ServerThread.class.getName()).log(Level.SEVERE, null, ex);
      } catch (NoSuchPaddingException ex) {
          Logger.getLogger(ServerThread.class.getName()).log(Level.SEVERE, null, ex);
      } catch (InvalidKeyException ex) {
          Logger.getLogger(ServerThread.class.getName()).log(Level.SEVERE, null, ex);
      } catch (IllegalBlockSizeException ex) {
          Logger.getLogger(ServerThread.class.getName()).log(Level.SEVERE, null, ex);
      } catch (BadPaddingException ex) {
          Logger.getLogger(ServerThread.class.getName()).log(Level.SEVERE, null, ex);
      }
      return new String(newPlainText, "UTF8");
  }  
  </code></pre>
  
  <p>I've included the imports I'm using for the base64. Also this isn't the whole code but just the relevant bits. I don't fully understand base64 and all my searching hasn't lead me to an answer so any help would be very appreciated!</p>
  
  <p>Edit: The reason I'm doing all this is to safely transport login credentials to a server. This works on my desktop app just fine. I copied the code from the desktop app but the encoding was the only thing that needed changing. I believe base 64 is part of the problem. Can't debug the app because I run amd so I have to run it right on my phone.</p>