Context
The first half of the project: Encrypt an image through The front-end using CryptoJS, then ZIP the encryption results and push to the server.
The other half: Allow users download the image they uploaded unzipped and decrypted from the server. Zipping happens using the back-end, but Encryption, Decryption, and Unzipping are done on the front-end.
This is the encryption code:
// Upload File upon clicking on "Upload" Button
var upload_button = document.getElementById('upload-button');
upload_button.addEventListener('click', uploadFile);
function uploadFile() {
var file = fileupload.files[0];
var reader = new FileReader();
var key = document.getElementById("enc_key").value;
var fileEnc = null;
reader.onload = function() {
var wordArray = CryptoJS.lib.WordArray.create(reader.result);
var encrypted = CryptoJS.AES.encrypt(wordArray, key).toString();
var fileEnc = new Blob([encrypted]);
encryptAndUpload(fileEnc);
}
reader.readAsArrayBuffer(file);
}
async function encryptAndUpload(file) {
let formData = new FormData();
formData.append("file", file);
let response = await fetch('/upload-form.php', {
method: "POST",
body: formData
});
console.log("Upload, Encryption, and Compression Completed Successfully.")
}
And This is the decryption Code:
async function decryptAndDownload_Unzipped() {
var key = document.getElementById("dec_key").value;
var file_url = window.location.href + '/uploads/test_new.zip' // the uploaded zip file path and name is hardcoded for now
// var file_url = 'https://url.com/uploads/test_new.zip'; // This is how the front-end fetches the encrypted zip file
https.get(file_url, function(res) {
var data = [], dataLen = 0;
res.on('data', function(chunk) {
data.push(chunk);
dataLen += chunk.length;
}).on('end', function() {
var buf = Buffer.alloc(dataLen);
for (var i = 0, len = data.length, pos = 0; i < len; i++) {
data[i].copy(buf, pos);
pos += data[i].length;
}
var zip = new AdmZip(buf);
var zipEntries = zip.getEntries();
var decrypted = hex2ascii(CryptoJS.AES.decrypt(zip.readAsText(zipEntries[0]), key));
downloadStringAsFile("decrypted_img.png", decrypted);
});
});
function hex2ascii(hexx){
var hex = hexx.toString(); //force conversion
var str = '';
for (var i = 0; i < hex.length; i += 2){
str += String.fromCharCode(parseInt(hex.substr(i, 2), 16));
}
return str;
}
function downloadStringAsFile(filename, text) {
var element = document.createElement('a');
element.setAttribute('href', 'data:text/plain,' + encodeURIComponent(text));
element.setAttribute('download', filename);
element.style.display = 'none';
document.body.appendChild(element);
element.click();
document.body.removeChild(element);
}
I console logged the input from the encryption function wordArray and the input of the decryption function zip.readAsText(zipEntries[0]) and both have the same identical HEX values. There seem to be no issue originated from the encryption/decryption process.
The Problem
I tested this with image black.png, but the downloaded file from the front-end is corrupted. I opened the downloaded PNG file (that was encrypted then decrypted) using text editor, and compared its binary data with the original and found differences between them around the backslashes (image attached). They are different from the original binary values but I’m not sure why this is happening and that’s why I assume what is causing the png file to be corrupted and can’t be read from image viewers.
Here’s an image shows the difference between the 2 images:

I’m trying to troubleshoot the corruption issue and make the downloaded image viewable.


