Skip to Main Content

Java Security

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Interested in getting your voice heard by members of the Developer Marketing team at Oracle? Check out this post for AppDev or this post for AI focus group information.

AES Encryption producing different values on different platforms

ak47Dec 13 2018 — edited Dec 13 2018

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.

Comments
Post Details
Added on Dec 13 2018
0 comments
274 views