Cifrado de Android cifrar / descifrar
Estoy usando cifrado para cifrar y descifrar mensajes:
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");
}
Por alguna razón, aunque estoy usando Base64 para codificar y decodificar la cadena, todavía recibo este error:
javax.crypto.IllegalBlockSizeException: último bloque incompleto en descifrado
¿Qué estoy haciendo mal?
Editar:
Este es mi JSONObject. Estoy tratando de descifrar la "m":
{"m":"Cu7FR2be0E6ZP2BrZaLU2ZWQSfycNg0-fPibphTIZno\r\n"}
Lo extraño es que el error solo aparece en Android. Mi servidor está escrito en Java y estoy usando el codificador Apache Base64 y funciona muy bien.