Search code examples
encryptionbrowserlarge-filescryptojs

Efficiently encrypt/decrypt large file with cryptojs


I want to encrypt large string (200 MB). The string come from dataUrl (base64) corresponding to file.

I'm doing my encryption in the browser.

My issue is that at the moment, i chunked string into small part into an array. Then i encrypt this chunks.

At the moment encrypting the string will full the memory. Here is how i'm doing it.

var encryptChunk = function(chunk, index){
  encryptedChunks.push( aesEncryptor.process( chunk ));
  sendUpdateMessage( "encryption", index+1, numberOfChunks );
}
chunkedString.forEach(encryptChunk);
encryptedChunks.push( aesEncryptor.finalize() );

I assume that, there should be a better way of doing this. But i can't find a memroy efficient way of doing this.


Solution

  • I am doing something similar to you. To directly answer your question of "is there a more memory efficient way?" .. well I use a web worker for processing progressive ciphering which seems to work.

      //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
                });
    

    You will need to import the cryptoJS script into your worker: importScripts('cryptoJS.all.min.js')