Click here to Skip to main content
15,887,214 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I am working on code to encrypts any file to a chosen password and will automatically encrypt any download. An encryption page where you can encrypt or decrypt any file which you need to download. The thing is I have just written encryption code and used "web workers" to perform the encryption. I created constructor "worker". I passed a parameter, that is a <worker_file>.js file. I mentioned some of the snippet here.

//main.js
JavaScript
<pre>req.open("GET", name, true);  
    req.responseType = "blob";
    req.onreadystatechange = function () 
    {
        if (req.readyState == req.DONE) 
        { 
            var blobResponse = req.response;
            var fileReader = new FileReader();
            fileReader.onload = function() {
            var worker = new Worker('worker_file.js');

             var binaryArrayOut = new Array();
                var arrayBuffer = this.result;;
                var fileNameOut;
                var currentCompletion = 0;
                var lengthLeft = getSize.size - 1;
                var perInterval = 100/(lengthLeft/5242880);
                var getSize = new Blob([arrayBuffer]);
                var currentStart = 0;
                
                worker.onmessage = function(e) {
                binaryArrayOut.push(e.data.fileObject);
                    fileNameOut = e.data.filenameToSaveAs;
                      console.log(fileNameOut); 
                         }
                    var postDataVar;
                    var postLengthVar;
                    var binaryArray;
                    
                    if(lengthLeft - (currentStart + 5242880) < 0)
                    {
                        postLengthVar = lengthLeft - currentStart;
                        binaryArray = arrayBuffer.slice(currentStart);
                        postDataVar = new Blob([binaryArray]);
                        currentStart = lengthLeft;
                    }
                    else
                    {
                        postLengthVar = lengthLeft - (currentStart + 5242880);
                        binaryArray = arrayBuffer.slice(currentStart, currentStart + 5242880);
                        postDataVar = new Blob([binaryArray]);
                        currentStart = currentStart + 5242880;
                    }
                    
                    worker.postMessage(
                    {'blobResponse': postDataVar3,
                    'fileName': fileName,
                    'password': passwd
                    });
                }
                fileReader.readAsArrayBuffer(new Blob([blobResponse]));
        }
    }
    req.send();
}

// worker_file.js

   self.onmessage = function(e) {

loadFile(e.data.blobResponse, e.data.fileName, e.data.password)

}
Got this file, fullfilename and password to call encrypt functions via loadFile function.

  function encrypt( file, fullfilename, passwordIn) {

// Init new instance of FileReader
var reader = new FileReader();
    
// Operate on data at load time
reader.onload = function(f) {
    
            // Encrypt with AES256 against password
           // use built in CrytoJS AES code to encrypt

                var encrypted = CryptoJS.AES.encrypt(f.target.result, passwordIn)
            
                // Create file for download. This is the encrypted file
            createFileDownload(encrypted);
                
            };
    reader.readAsDataURL(file);
}
Now, the issue is it does generate an encrypted file and downloads it. But, when I checked that encrypted file to decrypt using some other online decryption apps with the same passphrase. It does not give correct decryption file which means encryption file has some issue or maybe not encrypt in a correct way that is why cannot decrypt it.

But I do not understand where is the issue exactly.  Could any one help me with this?

What I have tried:

I wrote the code again 2 times and execute single commands, but nothing found.
Posted
Updated 15-Jul-22 11:31am
v4
Comments
Rida Kamran 18-Jul-22 8:43am    
I did try from the scratch. I used my code to encrypt and decrypt a single string of message, (not a file) that works fine. That means problem is in reading the file. Could anyone tell how can I read the contents of the file in Javascript before it downloads. Maybe I have something wrong with my reading the contents code.

1 solution

So basically, it doesn't work and you have no idea where the problem is: it could be the encryption or decryption section, or the file transfer, or the storage, or ... pretty much anything.

So you need to go back to basics and work out what does work.
Start by taking a known file and encrypting it with a "known working" program and the same password.
Then compare that file with the output of your app.
If it's same, check the stored file in the same way.

And so on, until you find a difference.

when you know where the problem is being caused, you can start looking at why - but until then you are just whistling in the dark.

Sorry, but we can't do any of that for you!
 
Share this answer
 
Comments
Rida Kamran 18-Jul-22 8:40am    
I did try from the scratch. I used my code to encrypt and decrypt a single string of message, (not a file) that works fine. That means problem is in reading the file. Could anyone tell how can I read the contents of the file in Javascript before it downloads. Maybe I have something wrong with my reading the contents code.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900