Skip to content
Advertisement

Is there a behavioral equivalent to the AES256TextEncryptor Class of the Jasypt-Library in CryptoJS?

As a newbie to Cryptography, I’m trying to reproduce the same default behavior of the AES256TextEncryptor Class of the jasypt-library with the CrpytoJS library. This is my Java method, that basically takes in two arguments – the message that I want to encrypt as well as my secret paraphrase:

JavaScript

When encrypting the messageToBeEncrypted with this code, the resulting encrypted message is fine. What I found out is that the AES256TextEncryptor, which internally uses the StandardPBEStringEncryptor as a encryptor, seems to use the PBEWithHMACSHA512AndAES_256 algorithm as a default.

How can I reproduce the same encryption behavior with CrpytoJS? When I’m trying to encrypt the message with CryptoJS in the way it’s documented here, the result is totally different from what I expect it to be.

Based on Topaco’s comment, I came up with the following JavaScript Code to mimic the Java code:

JavaScript

The generated result still seems not be as expected though, as it’s length is 88 characters, whereas the Java code generates a 64 character long encrypted message.

Advertisement

Answer

The posted code is close to the required result. The following still needs to be corrected:

  • PBKDF2 applies SHA1 by default, which means SHA512 must be explicitly specified.
  • The concatenation must be done on a binary level and not with the hex and Base64 encoded data.

If this is fixed, a possible implementation is:

JavaScript
JavaScript

Since because of the random salt and IV always different data is generated, a test of the implementation is not possible by comparing the data. Instead, it must be checked whether the data generated with the CryptoJS code is decryptable with the Jasypt counterpart for decryption:

JavaScript

which is indeed the case with the above CryptoJS implementation.

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement