how can I achieve the same result from the bellow JS to apex? I already have tried:

blob encryptedData = Crypto.encrypt('AES128', AesKey, AesIv, json); //apex

but it says 'Invalid initialization vector. Must be 16 bytes'.

var key = CryptoJS.enc.Base64.parse(AesKey);
    var iv = CryptoJS.enc.Base64.parse(AesIv);
    var date = new Date();
    var json = "{'key':'" + apiKey + "','time':'" + date.toISOString() + "'}";
    var authTokenEncrypted = CryptoJS.AES.encrypt(CryptoJS.enc.Utf8.parse(json), key,
        keySize: 128 / 8,
        iv: iv,
        mode: CryptoJS.mode.CBC,
        padding: CryptoJS.pad.Pkcs7

The key and IV, are exactly the same from the JS. Sadly, I can't provide the exactly values of key, so that you can reproduce, but yes, the AesIv has more than 16 bits and the AesKey is exactly 16 bits. Whit the JS code, I don't receive this error and encrypt normally.

  • Can you post bit of more code on those parameters which are passing through Crypto.encrypt() – Santanu Boral Aug 21 '18 at 12:20
  • hello @SantanuBoral, thank you, I added more comments, please, let me know if helps. – Thiago Balancin Aug 21 '18 at 12:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.