title: Android Cipher encrypt/decrypt
body: <p>I'm using cipher to encrypt and decrypt messages:</p>
  
  <pre><code>public String encrypt(String string) throws InvalidKeyException, IllegalBlockSizeException, BadPaddingException, UnsupportedEncodingException {
    cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec);
    byte[] stringBytes = string.getBytes("UTF-8");
    byte[] encryptedBytes = cipher.doFinal(stringBytes);
    return android.util.Base64.encodeToString(encryptedBytes, android.util.Base64.DEFAULT);
  }
  
  public String decrypt(String string) throws InvalidKeyException, IllegalBlockSizeException, BadPaddingException, UnsupportedEncodingException {
    cipher.init(Cipher.DECRYPT_MODE, secretKeySpec);
    byte[] stringBytes = android.util.Base64.decode(string.getBytes(), android.util.Base64.DEFAULT);
    byte[] decryptedBytes = cipher.doFinal(stringBytes);
    return new String(decryptedBytes,"UTF-8");
  }
  </code></pre>
  
  <p>For some reason although I'm using Base64 to encode and decode the string, I still get this error:</p>
  
  <blockquote>
    <p>javax.crypto.IllegalBlockSizeException: last block incomplete in decryption</p>
  </blockquote>
  
  <p>What am I doing wrong?</p>
  
  <p><strong>Edit:</strong></p>
  
  <p>This is my JSONObject - I'm trying to decrypt the "m":</p>
  
  <pre><code>{"m":"Cu7FR2be0E6ZP2BrZaLU2ZWQSfycNg0-fPibphTIZno\r\n"}
  </code></pre>
  
  <p>The weird thing is that the error only appears in Android. My server is written in Java and I'm using Apache Base64 encoder and it works great.</p>