0

i am trying to encrypt files with JS during upload and then decrypt them during download - all clientside, with javascript (like Mega / tresorit).

So far, basic principle of my app is:

-upload: read file with HTML5 FileReader as a base 64 string, encrypt it with CryptoJS, then send to PhP

reader = new FileReader(); 
    reader.onload = function(event) {
        var encrypted = CryptoJS.AES.encrypt(event.target.result, "Secret Passphrase");
       //send to PhP
    }
    reader.readAsDataURL(files[i]);

-download: get file as a string via XMLHTTP request, decrypt with CryptoJS and i am done.

var rawFile = new XMLHttpRequest();
rawFile.open("GET", file, true);
rawFile.onreadystatechange = function ()
{
    if(rawFile.readyState === 4)
    {
        if(rawFile.status === 200 || rawFile.status == 0)
        {
            var decryped= CryptoJS.AES.decrypt(rawFile.responseText, "Secret Passphrase");
            //process....

        }
    }
}

The principle above is working fine with small files (few MBs), but i need to be able to work with bigger files too (at least 100MB).

Is there any nice way of doing it? Uploading may be probably solved by uploading encrypted chunks with HTML5 FileReader, but what about downloading? I was thinking about HTML5 FileSystem API, but it is no longer developed.

I am not asking anyone to code that for me, I just want to know what is the good/best way of doing it and what should I be googling / looking for.

Thanks you.

user2054618
  • 55
  • 10
  • 1
    Basically the same thing: [JavaScript File Hash Value Generate with Part of the file](http://stackoverflow.com/a/28213834/1816580). You need to do this inside of a Web Worker otherwise the browser will freeze. CryptoJS provides progressive encryption, but I don't think it works with passphrases, but rather with key+IV. – Artjom B. Oct 06 '15 at 10:12
  • Thans you for interesting link, i think i can handle upload now. But what about download? How can i download my file by parts and then build them back to the whole file from decrypted parts? – user2054618 Oct 06 '15 at 19:19
  • I don't know. [This](http://www.html5rocks.com/en/tutorials/file/filesystem/#toc-file-creatingempty) might be a starting point. Don't forget to post your answer here when you get working code. – Artjom B. Oct 06 '15 at 19:22
  • I have thought of FileSystem API before, but currently it is supporter only by chrome, and its very unlikelly to be supported by other browsers in future. I think there must be a better way, the way mega/tresorit/wuala does it. – user2054618 Oct 06 '15 at 19:39
  • I see. I don't know how they do it, but you can always look into their code. – Artjom B. Oct 06 '15 at 19:52

0 Answers0