1

I'm trying to encrypt a message in javascript (using crypto-js library) and to decrypt it in java.

This is the javascript code:

var key = CryptoJS.enc.Utf8.parse(aesPassword);
var ive  = CryptoJS.enc.Utf8.parse(aesIv);
var encryptedData = CryptoJS.AES.encrypt(dataToEncrypt, key, {mode: CryptoJS.mode.CBC, padding: CryptoJS.pad.Pkcs7, iv: ive});

And this is the java code:

final Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
        final SecretKeySpec key = new SecretKeySpec(aesPassword().getBytes("UTF-8"), "AES");
        cipher.init(Cipher.DECRYPT_MODE, key, new IvParameterSpec(aesIv().getBytes("UTF-8")));
        byte[] decrypted = cipher.doFinal(DatatypeConverter.parseBase64Binary(message));

But when I try to decrypt in Java this exception is thrown: javax.crypto.BadPaddingException: Given final block not properly padded

password: 6h2faBePVxpgyFSN iv: NKOzRKrmEMKs1kE4 data to encrypt: "{token: cMGOIrYlJm9lPhPW}"

Any help?

Thanks in advance

6
  • 1
    Are you sure they're compatible? Commented Apr 20, 2017 at 11:36
  • Yes. In other cases I encrypt with java and decrypt in javascript and it works fine Commented Apr 20, 2017 at 12:16
  • Your code looks correct. Please edit your question to include your key and IV. You should encode the byte array of the key and IV as Hex to compare them between JavaScript and Java. Commented Apr 20, 2017 at 18:56
  • If you're using only symmetric encryption you need the exact same key at the server and the client. If you send the encryption key from the server to the client or the other way around you need to encrypt your symmetric encryption key. The easiest way to do this would be to use TLS. If you use TLS, then the data as well as key are encrypted, so you don't need to encrypt it yourself. This doesn't provide any security, just a little bit of obfuscation. You should read: nccgroup.trust/us/about-us/newsroom-and-events/blog/2011/august/… Commented Apr 20, 2017 at 18:56
  • I know tls is the best way but I was only playing with cryptography to improve the security of login method. Edited the original post with key and iv Commented Apr 21, 2017 at 6:03

1 Answer 1

0

I may be wrong, but I think BadPaddingException in this case means that you don't possess the correct key to successfully perform the decryption.The exception essentially means that the key is either too short or too long (I think).

Try{
    String decrypted = aes.decrypt(...);
    System.out.println(decryted);
}catch(Exception e){

}

Something like the code above may work, as System.out is only reached when the BadPaddingException isn't caught, this could be used in a loop when trying possible keys for decryption, for example, if you were trying to calculate all possible keys for the decryption.

Sign up to request clarification or add additional context in comments.

1 Comment

The key is correct and it is used to encrypt messages in java and decrypt messages in javascript. The key is always the same

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.