How to decrypt a cryptojs AES encrypted message at the java server side?

后端 未结 2 589
失恋的感觉
失恋的感觉 2021-02-08 12:02

I have the following cryptojs based javascript encryption/decryption functions which works perfectly fine.

I use a random salt, random iv value and a specific password

2条回答
  •  南方客
    南方客 (楼主)
    2021-02-08 12:42

    This part of your code is wrong:

    KeySpec keySpec = new PBEKeySpec(password.toCharArray(), saltBytes, 100, 128/32);
    //->---------------------------------------------------------------------^^^^^^^
    

    The 128/32 value is erroneous. You need either 128, 192 or 256. Currently you have the equivalent of 4, which seems to result in no output at all from the PBKDF2 function.

    Also, in Java you should use DatatypeConverter.parseHexBinary(), or similar, to convert hex into bytes. Currently you are just calling getBytes() which isn't right.

    Finally, you need to specify CBC mode and PKCS#5 padding in order to match your Javascript code. So change the line to:

    Cipher c = Cipher.getInstance("AES/CBC/PKCS5Padding");
    

提交回复
热议问题