Search code examples
javascriptnode.jsencryptionfsnwjs

nwjs-nodejs- encrypt and decrypt img file (jpg) and use the decrypted data to an img element


I developed a desktop application with nwjs (nodejs / html / css ), now i want to put the app for the production so i need to prevent stealing my assets (my images are very valuables), nwjs provide a tool to compile (encrypt) the js files but not the asset so i thought about encrypting my assets with a js then encrypt the js with nwjs tool, i am not very familiare with node modules and dealing with files in js so i struggled with this task ! This code is what i tried to do but i did not reach my goal ?

  • encrypt

    let crypto;
    try {
        crypto = require('crypto');
    } catch (err) {
        console.log('crypto support is disabled!');
    }
    var algorithm = 'aes-256-ctr',
            password = 'secret';
    var fs = require('fs');
    var r;
    
    // encrypt content
    var encrypt = crypto.createCipher(algorithm, password);
    // decrypt content
    var decrypt = crypto.createDecipher(algorithm, password);
    
    // write file
    var w;
    var path = require('path');
    var dirPath = './Files/';  //directory path
    var fileType = '.' + 'jpg'; //file extension
    var files = [];
    fs2.readdir(dirPath, function (err, list) {
        if (err) throw err;
        for (var i = 0; i < list.length; i++) {
            if (path.extname(list[i]) === fileType) {
                r = fs.createReadStream('Files/' + list[i]);
                w = fs.createWriteStream('encFiles/' + list[i].replace('.jpg', ''));
                console.log(list[i]); //print the file
                // start pipe
                r.pipe(encrypt).pipe(w);
            }
        }
    });
    
  • decrypt

        'use strict';
    var crypt = require('crypto'),
            algorithm = 'aes-256-ctr',
            password = 'secret';
    
    var fs = require('fs');
    var zlib = require('zlib');
    var toArray = require('stream-to-array');
    
    // input file
    var r = fs.createReadStream('./encFiles/an example file'); 
    // decrypt content
    var decrypt = crypt.createDecipher(algorithm, password);
    
    //b64 module so i could put the base64 data to img html element
    const B64 = require('b64');
    
    const encoder = new B64.Encoder();
    
    // start pipe
    var stream = r.pipe(decrypt);
    
    var d = stream.pipe(encoder);
    d.pipe(process.stdout);
    
    var data;
    toArray(stream, function(err, arr) {
        console.log(err,arr);
        data = Buffer.concat(arr);
        console.log(data);
    });
    console.log(data);
    

thank you for giving me comments on the code or other IDEAS


Solution

  • So the solution was so simple, I used the nw-js code protection feature to protected the script in which I decrypt the assets (images in my case) (this script contains the key of decryption), so you could implement the encryption/decryption with any method you want and protect the decryption script which is going to be shipped with you product (in my case the desktop app).