Reputation: 1719
My Goal: I am trying to encrypt .js files into .jse and decrypt only when it is running (obfuscate nodejs code).
var ffi = require('ffi'); //libpcrypt.so is a library to encrypt and decrypt files var crypt = ffi.Library('./libpcrypt', { 'decrypt' : [ 'string', ['string', 'string']] }); require.extensions[".jse"] = function (module) { module.exports = (crypt.decrypt(module.filename, 'out')); }; console.log(require('./routes.jse'));
I know, with cosole.log() source code can be printed out.
Problem: Decrypted code is a plain string, I am not able to convert it into a valid javascript object for exports. Is there a way to export the code string I decrypted?
Upvotes: 1
Views: 448
Reputation: 30103
Here's your solution (not tested):
require.extensions['.jse'] = function(module, filename) {
var content = crypt.decrypt(fs.readFileSync(filename), 'out')
return module._compile(content, filename);
};
Happy debugging encrypted modules ;)
Upvotes: 1
Reputation: 21106
module.exports is an object you can assign to (ie: module.exports.newFunc = someFunction;)
JSON.parse(crypt.decrypt(module.filename, 'out'));
EDIT So you should make your encrypted file a JSON class OR check out this answer to a similar question Load "Vanilla" Javascript Libraries into Node.js
Upvotes: 0