首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >CryptoJS在wordArray到ByteArray转换中超过最大调用堆栈大小

CryptoJS在wordArray到ByteArray转换中超过最大调用堆栈大小
EN

Stack Overflow用户
提问于 2019-11-19 15:25:25
回答 2查看 1.1K关注 0票数 2

我正在尝试使用CryptoJS在react客户端应用程序中实现加密和解密。加密和解密可以在没有错误的情况下进行,但只适用于文件大小较小的情况。在对视频文件(9MB)进行解密时,最大调用堆栈大小在将wordArray转换为ByteArray时会发生错误。处理此转换的代码来自:GitHub

错误:最大呼叫堆栈尺寸超过

代码:

代码语言:javascript
运行
复制
 wordArrayToByteArray = (wordArray,length) => {

  if(wordArray.hasOwnProperty("sigBytes") && wordArray.hasOwnProperty("words")){
    length = wordArray.sigBytes;
    wordArray = wordArray.words;
  }

  let result = [];
  let bytes;
  let i = 0;

  while(length > 0){
    bytes = this.wordToByteArray(wordArray[i],Math.min(4,length));
    length -= bytes.length;
    result.push(bytes);
    i++;
  }

  return [].concat.apply([],result);
}

 wordToByteArray = (word, length) => {

  var ba = [],
    i,
    xFF = 0xFF;
  if (length > 0)
    ba.push(word >>> 24);
  if (length > 1)
    ba.push((word >>> 16) & xFF);
  if (length > 2)
    ba.push((word >>> 8) & xFF);
  if (length > 3)
    ba.push(word & xFF);

  return ba;
}

decryptFile = (downloaded) =>{ 
  //convert unit array(encrypted file) to string
  let encryptedString = this.uintToString(downloaded);

  //decrypt and get wordArray
  const decrypted = cryptojs.AES.decrypt(encryptedString,"123");
  console.log(decrypted);

  //convert wordArray to string
  let decryptedString = decrypted.toString(cryptojs.enc.Utf8);


  // form a new word array
  const wordArray = cryptojs.enc.Hex.parse(decryptedString);


  //convert new wordArray to byteArray
  return this.wordArrayToByteArray(wordArray,Object.keys(wordArray).length);
}

uintToString = (uintArray) => {
  const decodedStr = new TextDecoder("utf-8").decode(uintArray);
  return decodedStr;
}

 onDownload = () => {
  ipfs.get(this.state.ipfsHash,(error,files)=>{
    files.forEach((file)=>{
      console.log(file.path);
      const decryptedFile = this.decryptFile(file.content);

      const arrayBufferView = new Uint8Array(decryptedFile);

      const blob = new Blob([arrayBufferView],{type: "video/mp4"});
      console.log(blob);
      fileSaver.saveAs(blob,"curseTheInternet.mp4");
    })
  })
}

或者为客户提供任何其他加密库的建议?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2019-11-19 15:37:12

问题来自您使用的[].concat.apply([], result),如果结果太大,这将导致您正在看到的错误。据我所知,你是用这个来平复词组的。若要避免此错误,请使用.flat

票数 2
EN

Stack Overflow用户

发布于 2021-03-04 14:58:13

使用此改进版本

代码语言:javascript
运行
复制
static wordArrayToByteArray(wordArray: CryptoJS.lib.WordArray, length: number): Uint8Array {
    let words: any = wordArray;
    if (wordArray.hasOwnProperty("sigBytes") && wordArray.hasOwnProperty("words")) {
      length = wordArray.sigBytes;
      words = wordArray.words;
    }

    let result: number[] = [];
    let bytesAdded;
    let i = 0;
    while (length > 0) {
      bytesAdded = this.wordToByteArray(result, words[i], Math.min(4, length));
      length -= bytesAdded.length;
      i++;
    }
    return new Uint8Array(result);
  }

  private static wordToByteArray(result, word, length): number {
    const lengthBefore = result.length;
    const xff = 0xFF;
    if (length > 0)
      result.push(word >>> 24);
    if (length > 1)
      result.push((word >>> 16) & xff);
    if (length > 2)
      result.push((word >>> 8) & xff);
    if (length > 3)
      result.push(word & xff);
    return result.length - lengthBefore;
  }
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58937464

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档