Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/rest/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript中的CryptoJS WordArray和驱动API补丁更新_Javascript_Rest_Google Chrome Extension_Cryptojs - Fatal编程技术网

Javascript中的CryptoJS WordArray和驱动API补丁更新

Javascript中的CryptoJS WordArray和驱动API补丁更新,javascript,rest,google-chrome-extension,cryptojs,Javascript,Rest,Google Chrome Extension,Cryptojs,我正在编写Chrome扩展,它需要驱动API和。它已经可以以arrayBuffer()形式获取字节,解密并读取json: fetch( 'https://www.googleapis.com/drive/v3/files/' + fileId + '?alt=media', { method: 'GET', headers: { Authorization: 'Bearer ' + token,

我正在编写Chrome扩展,它需要驱动API和。它已经可以以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 Drive:

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()过程中发生错误:

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

  • newWordArray和dcWordArray真的是WordArray吗?如何验证
  • 对于补丁更新,如何将WordArray作为ArrayBuffer发送
  • 解决方案

    • 一旦我知道如何检查数据类型+值,事情就变得显而易见了。例如,一些名为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
      
      最后,通过驱动器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);
      );
      
      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);
      );