Hi,
I am using AES encryption algorithm to encrypt the string on android device and then decrypting it on the server in order to fetch the data.
The problem I am facing is that the encrypted string that I am getting is giving "badPaddingException" while decrypting.
Below is the code for encryption and decryption.
For decryption(On server) ->
public static String decrypt(String message, String encryptionKey) throws Exception {
try {
setIvPair(encryptionKey.getBytes("UTF-8"), ivString.getBytes("UTF-8"));
Cipher cipher = getCipher(Cipher.DECRYPT_MODE);
byte[] encryptedTextBytes = BaseEncoding.base64().decode(message);
byte[] decryptedTextBytes = cipher.doFinal(encryptedTextBytes);
return new String(decryptedTextBytes);
} catch (IllegalBlockSizeException | BadPaddingException | InvalidKeyException
| InvalidAlgorithmParameterException | UnsupportedEncodingException e) {
log.debug("Unable to decrypt message :: " + e.getMessage());
throw new Exception(e);
}
For encryption(On MobileApp) ->
public static String encrypt(String message, String encryptionKey) throws Exception {
try {
setIvPair(encryptionKey.getBytes("UTF-8"), ivString.getBytes("UTF-8"));
Cipher cipher = getCipher(Cipher.ENCRYPT_MODE);
byte[] encryptedTextBytes = cipher.doFinal(message.getBytes("UTF-8"));
return BaseEncoding.base64().encode(encryptedTextBytes);
} catch (IllegalBlockSizeException | BadPaddingException | UnsupportedEncodingException | InvalidKeyException
| InvalidAlgorithmParameterException e) {
log.info("Unable to encrypt message :: ", e);
//e.printStackTrace();
throw new Exception("Unable to encrypt message");
}
}
The string being encrypted is of length 10, and the token used to encrypt it is "f15c3316a539466c924f35215b806989".
The encrypted value which I am getting from the app is "t3XMYQ2NZ62Yg7KO1/smrQ==", however, the value which I should get is "Vjs6tvfz+JqNalf3s9mWaA==".
I have debugged the code and checked the values of the parameters and matched the same with the one in the app, everything seems to be in sync.
Can somebody help me out here, any help is appreciated.
Thanks in advance.