Cifra Android criptografar / descriptografar
Eu estou usando cifra para criptografar e descriptografar mensagens:
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 algum motivo, embora eu esteja usando o Base64 para codificar e decodificar a string, ainda recebo este erro:
javax.crypto.IllegalBlockSizeException: último bloco incompleto na descriptografia
O que estou fazendo de errado?
Editar:
Este é o meu JSONObject - estou tentando descriptografar o "m":
{"m":"Cu7FR2be0E6ZP2BrZaLU2ZWQSfycNg0-fPibphTIZno\r\n"}
O estranho é que o erro só aparece no Android. Meu servidor está escrito em Java e estou usando o codificador Apache Base64 e funciona muito bem.