我用nwjs(nodejs / html / css)开发了一个桌面应用程序,现在我想把这个应用程序用于生产,所以我需要防止窃取我的资产(我的图像非常有价值),nwjs 提供了一个编译(加密)的工具js 文件而不是资产所以我想用 js 加密我的资产然后用 nwjs 工具加密 js,我对节点模块和处理 js 中的文件不是很熟悉,所以我在这个任务中挣扎!这段代码是我试图做的,但我没有达到我的目标?
加密
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); } } });
解密
'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);
感谢您对代码或其他 IDEAS 发表评论