Splitting CryptoJS word array to extract the IV before decryption

  Kiến thức lập trình

I receive a base64 encoded string. The first 16 bytes are the IV. I need to extract them before I decrypt with CryptoJS.

I am failing to split data from CryptoJS.enc.Base64.parse

In a nutshell, please help me fill this:

/*
 * ivAndcipheredDataBase64: first 16 bytes are iv the rest is the cipheredData
 */
function decipherData(ivAndcipheredDataBase64, key) {

    var ivAndCipheredDataBytes = CryptoJS.enc.Base64.parse(ivAndcipheredDataBase64);
    var iv = // What do I put here to get cipheredIVAndDataBytes[0:16]
    var cipheredData = // What do I put here to get cipheredIVAndDataBytes[16:len(cipheredIVAndDataBytes)]
   

    var plainDataBytes = CryptoJS.AES.decrypt({ ciphertext: cipheredData }, key, {
        iv: iv
    });
    return plainDataBytes.toString(CryptoJS.enc.Utf8)

}

Thanks, I am having trouble to know the type returned by CryptoJS.enc.Base64.parse, the documentation says WordArray object but i am not sure how to deal with that

Theme wordpress giá rẻ Theme wordpress giá rẻ Thiết kế website

LEAVE A COMMENT