1

I'm working into a project to encrypt/decrypt files in JavaScript. To save the encrypted/decrypted file in disk, I'm using blob. All the process is working, the files get encrypted (and some tests show-me that decrypted too) correctly. And I can save even large files with the blob method (I was using URI data before, and it was causing browser crash errors when files size more than 1MB). But for some reason, I can't save the decrypted blob content into a file correctly. When it's a TXT file, I get this in the beginning of the file content:

     data:text/plain;base64,

and it continues with the text content encoded in base64. I need it to be saved as original file, not in base64. When I decrypt an exe file, it's corrupted, so if I open it into some text editor, I get:

     data:application/x-msdownload;base64,

Again, looks like the file is getting saved in base64 and with this header attached. Here's my code to create/save the content of blob (on decrypt routine):

    reader.onload = function(e){

        var decrypted = CryptoJS.AES.decrypt(e.target.result, password)
                                .toString(CryptoJS.enc.Latin1);
        var blob = new Blob([decrypted]);
        var objectURL = window.URL.createObjectURL(blob);

        if(!/^data:/.test(decrypted)){
            alert("Invalid pass phrase or file! Please try again.");
            return false;
        }

        a.attr('href','' + objectURL);
        a.attr('download', file.name.replace('.encrypted',''));

        step(4);
    };

    reader.readAsText(file);
}

How can I save the files with the original content? And not header+base64 encode?

  • It looks like its base64 encoded before it was encrypted. Don't base64 encode it before encryption? – Musa Feb 04 '15 at 16:50
  • I believe to the encryption works, all the content needs to be encoded in base64. It's strange, because it was working when the link to download the file was something like: `data:text/plain;base64,77u/Q3JhY2tlcnMgZG...` but when I put it to be blob (this way I'm able to download bigger data size), and the link came to something like:`blob:null/666f4c47-0ef9-4315-9552-87a056d1813b` I get the file with MIME header and base64 encoded... What can be done here? – Petrus Ananias Feb 04 '15 at 17:16
  • base64 encoding is needed for data uri(and are automatically decoded by the browser) but not for blobs. – Musa Feb 04 '15 at 17:21
  • Yes, it looks exactly that what's happening here. And I don't know how to fix this problem... The blob is downloaded in base64 content, I tried to use atob/btoa, but it corrupted the content (specially when it was binary raw data)... I removed the header location the indexOf(',') and atob everything that left. But as I said, corrupted the content. – Petrus Ananias Feb 04 '15 at 17:23
  • Why do you base64 encode the file before you encrypt it? – Musa Feb 04 '15 at 17:24
  • It's a library I got for encryption that does this. I believe that the encryption do not work well if the data doesn't get base64 encoded... Not sure. But if the only way to make it work is not encoding base64, I'll try to make it work without base64... Maybe not possible. – Petrus Ananias Feb 04 '15 at 17:26
  • Take a look at http://stackoverflow.com/questions/16245767/creating-a-blob-from-a-base64-string-in-javascript – Musa Feb 04 '15 at 17:29

0 Answers0