我想加密大字符串 (200 MB)。 字符串来自文件对应的dataUrl(base64)。
我正在浏览器中进行加密。
我的问题是,目前,我将字符串分成一小部分放入数组中。 然后我加密这个 block 。
此时加密字符串会占满内存。 这是我的做法。
var encryptChunk = function(chunk, index){
encryptedChunks.push( aesEncryptor.process( chunk ));
sendUpdateMessage( "encryption", index+1, numberOfChunks );
}
chunkedString.forEach(encryptChunk);
encryptedChunks.push( aesEncryptor.finalize() );
我认为,应该有更好的方法来做到这一点。但我找不到一种有效的方法来做到这一点。
最佳答案
我正在做和你类似的事情。直接回答你的问题“有没有更高效的内存方式?” .. 好吧,我使用网络 worker 来处理似乎有效的渐进式加密。
//pass in what you need here
var worker = new Worker("path/to/worker.js");
worker.postMessage({
key: getKeyAndIvSomehow(),
file: file,
chunkSize: MY_CHUNK_SIZE
});
worker.addEventListener('message', function (e) {
// create the blob from e.data.encrypted
});
您需要将 cryptoJS 脚本导入到您的 worker 中:importScripts('cryptoJS.all.min.js')
关于encryption - 使用 cryptojs 高效地加密/解密大文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20022772/