benzen
benzen

Reputation: 6444

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.

Upvotes: 3

Views: 4443

Answers (2)

Ilmari Karonen
Ilmari Karonen

Reputation: 50328

What are you doing with the encrypted chunks? If you're, say, uploading them over the network, you don't need to store them in an array first. Instead, you can upload the encrypted file chunk by chunk, either writing your own chunked upload implementation (it's not terribly hard) or by using an existing library.

Ditto for the input: you can encrypt it as you read it. You can use the JS File API to read the file in chunks, using the .slice() method.

Other than that, your code looks just like the recommended way to progressively encrypt a file.

Upvotes: 1

Craig Bruce
Craig Bruce

Reputation: 674

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')

Upvotes: 1

Related Questions