1

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?

2 Answers 2

1

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 ;)

Sign up to request clarification or add additional context in comments.

6 Comments

Thanks. I saw this stackoverflow.com/a/9163557/458816 too. But I get SyntaxError: Unexpected token ILLEGAL. I thought it is encoding issue and converted to utf-8. but no use.
This code should work, because this code stolen from working library. Check your sources you are trying to _compile.
I am still struggling with the error. Do you think it can be encoding issues? Any other suggestions?.
What do you see on console.log(content);? Expected source code? What if replace real code with some simple example like module.exports = 2 + 2;
I see real code. And I tried as simple as var x = 10. Still same problem.
|
0

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

3 Comments

it didn't work. Because the string is plain and not JSON.stringify generated. Parse Error: SyntaxError: Unexpected token e
@mv945 - So you should make your encrypted file a JSON class OR check out this answer to a similar question stackoverflow.com/questions/5171213/…
you pointed me right direction. thanks.. But I cannot accept your answer unless you edit it :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.