CryptoJS WordArray 和驱动器 API Javascript 中的 PATCH 更新

CryptoJS WordArray and Drive API PATCH update in Javascript

我正在编写 Chrome 扩展程序,它需要驱动器 API 和 CryptoJS。已经可以获取arrayBuffer()形式的字节,解密读取json:

fetch(
    'https://www.googleapis.com/drive/v3/files/' + fileId + '?alt=media',
        {
        method: 'GET',
        headers: {
            Authorization: 'Bearer ' + token,
            'Accept': 'application/json'
            }
        }
).then(response => response.arrayBuffer()
).then(function(respRaw) {
    let respWordArray = CryptoJS.lib.WordArray.create(respRaw);
    let dcWordArray = CryptoJS.AES.decrypt(
        {
            ciphertext: respWordArray
        }, 
        key,
        {   // "AES/CBC/PKCS5Padding"
            iv: iv,
            mode: CryptoJS.mode.CBC,
            padding: CryptoJS.pad.Pkcs7
        }
    );
    let resptext = dcWordArray.toString(CryptoJS.enc.Utf8);
    let respJson = JSON.parse(resptext);
}

现在,我想反转每个并将加密的内容更新回 Google 驱动器:

let newtext = JSON.stringify(respJson);
let newWordArray = CryptoJS.enc.Utf8.parse(newtext);
let ecWordArray = CryptoJS.AES.encrypt( // RangeError: Invalid array length
    {
        ciphertext: newWordArray
    }, 
    key,
    {   // "AES/CBC/PKCS5Padding"
        iv: iv,
        mode: CryptoJS.mode.CBC,
        padding: CryptoJS.pad.Pkcs7
    }
);

//let dcBase64String = ecWordArray.toString(CryptoJS.enc.Base64);
let uint8array     = convertWordArrayToUint8Array(ecWordArray);
let binaryString   = convertUint8ArrayToBinaryString(uint8array);

fetch(
    'https://www.googleapis.com/upload/drive/v3/files/' + fileId,
    {
        method: 'PATCH',
        headers: {
            Authorization: 'Bearer ' + token,
            'Accept': 'text/plain',
            'Content-Type': 'text/plain'
        },
        body: binaryString
    }
).then(response => response.text()
).then(function(data) {
    console.log('data:' + data);
    }
);

遗憾的是,在 CryptoJS.AES.encrypt():

期间发生错误
RangeError: Invalid array length
at WordArray.init.clamp (crypto-js.js:272)
at WordArray.init.concat (crypto-js.js:237)
at Object.pad (crypto-js.js:3666)
at Object._doFinalize (crypto-js.js:3743)
at Object.finalize (crypto-js.js:3414)
at Object.encrypt (crypto-js.js:3926)
at Object.encrypt (crypto-js.js:3452)
at downup.js:2894

所以我不知道其余的类型转换是否正确。 如我所见,newWordArray 与 dcWordArray 相同。请清除我的想法:

一旦我知道如何检查数据类型和值,事情就变得显而易见了。例如,某些参数名为 ecResult:

console.log(ecResult);
> {$super: {…}, words: Array(28), sigBytes: 112, init: ƒ}

CryptoJS.AES.encrypt 不只是持有 WordArray。所以我将其重命名并将其转换为 ArrayBuffer:

let ecResult = CryptoJS.AES.encrypt(
    newWordArray,
    key,
    {   // "AES/CBC/PKCS7Padding"
        iv: iv,
        mode: CryptoJS.mode.CBC,
        padding: CryptoJS.pad.Pkcs7
        }
);
let ecWordArray = ecResult.ciphertext; // this is WordArray
let ecArrayBuf = CryptJsWordArrayToUint8Array( // github.com/brix/crypto-js/issues/274
    ecWordArray
).buffer; // now we have ArrayBuffer

最后,通过 Drive API:

用 ArrayBuffer 更新文件
fetch(
    'https://www.googleapis.com/upload/drive/v3/files/' + fileId,
    {
        method: 'PATCH',
        headers: {
            Authorization: 'Bearer ' + token,
            'Accept': 'text/plain',
            'Content-Type': 'text/plain'
            },
        body: ecArrayBuf                                
        }
).then(response => response.text()
).then(function(data) {
    console.log('data:' + data);
);