Hide JS code

22

I've been trying to figure out if there is any way to hide the direct access JavaScript source code. Is there any way to do this?

    
asked by anonymous 11.07.2014 / 09:11

4 answers

23

Diogo, the most you can do is disrupt anyone who wants to see the source code but will never be able to hide it in its entirety.

You can make this code:

var a = "Hello World!";
function MsgBox(msg){
    alert(msg+"\n"+a);
}
MsgBox("OK");

appear like this:

var _0x8e48 = ["\x48\x65\x6C\x6C\x6F\x20\x57\x6F\x72\x6C\x64\x21", "\x0A", "\x4F\x4B"];
var a = _0x8e48[0];

function MsgBox(_0xab5dx3) {
    alert(_0xab5dx3 + _0x8e48[1] + a);
};
MsgBox(_0x8e48[2]);

or only

var a = "Hello World!";
function B(c){
    alert(c+"\n"+a);
}
B("OK");

But the user can always find the code and decipher it. If the browser can use it, the user can see and try to decipher.

    
11.07.2014 / 10:08
7

I searched for various solutions on the internet in English using the term "javascript obfuscator" and found some excellent, but very expensive and expensive solutions (even more for Brazilians who pay the service in dollars).

However the company did not want to pay for the tool, and I ended up finding one in github link

You can use the online version as well: link and the best: it's free and open source.

Edit: For example, if we get the code that @ Seriously posted on the other answer:

    var a = "Hello World!";
    function MsgBox(msg){
        alert(msg+"\n"+a);
    }
    MsgBox("OK");

And let's use the tool above , it's completely unreadable and also protected against modification. If someone tries to format it (put line breaks, rename variables or functions, remove codes, etc.) using a javascript beautifier of life, it stops to work.

var _0x3102=['\x48\x65\x6c\x6c\x6f\x20\x57\x6f\x72\x6c\x64\x21'];(function(_0x4d8cba,_0x14ad3c){var _0x52efb3=function(_0x1380af){while(--_0x1380af){_0x4d8cba['\x70\x75\x73\x68'](_0x4d8cba['\x73\x68\x69\x66\x74']());}};var _0x4fa633=function(){var _0x189275={'\x64\x61\x74\x61':{'\x6b\x65\x79':'\x63\x6f\x6f\x6b\x69\x65','\x76\x61\x6c\x75\x65':'\x74\x69\x6d\x65\x6f\x75\x74'},'\x73\x65\x74\x43\x6f\x6f\x6b\x69\x65':function(_0x11387e,_0x173a51,_0x6a0123,_0xea9d6f){_0xea9d6f=_0xea9d6f||{};var _0xb46af2=_0x173a51+'\x3d'+_0x6a0123;var _0x5b1c00=0x0;for(var _0x5b1c00=0x0,_0x24424e=_0x11387e['\x6c\x65\x6e\x67\x74\x68'];_0x5b1c00<_0x24424e;_0x5b1c00++){var _0x4668d5=_0x11387e[_0x5b1c00];_0xb46af2+='\x3b\x20'+_0x4668d5;var _0x1001b3=_0x11387e[_0x4668d5];_0x11387e['\x70\x75\x73\x68'](_0x1001b3);_0x24424e=_0x11387e['\x6c\x65\x6e\x67\x74\x68'];if(_0x1001b3!==!![]){_0xb46af2+='\x3d'+_0x1001b3;}}_0xea9d6f['\x63\x6f\x6f\x6b\x69\x65']=_0xb46af2;},'\x72\x65\x6d\x6f\x76\x65\x43\x6f\x6f\x6b\x69\x65':function(){return'\x64\x65\x76';},'\x67\x65\x74\x43\x6f\x6f\x6b\x69\x65':function(_0x386eb1,_0x29953b){_0x386eb1=_0x386eb1||function(_0x57ca6e){return _0x57ca6e;};var _0x32368d=_0x386eb1(new RegExp('\x28\x3f\x3a\x5e\x7c\x3b\x20\x29'+_0x29953b['\x72\x65\x70\x6c\x61\x63\x65'](/([.$?*|{}()[]\/+^])/g,'\x24\x31')+'\x3d\x28\x5b\x5e\x3b\x5d\x2a\x29'));var _0x5c72b4=function(_0x258f31,_0x5426ba){_0x258f31(++_0x5426ba);};_0x5c72b4(_0x52efb3,_0x14ad3c);return _0x32368d?decodeURIComponent(_0x32368d[0x1]):undefined;}};var _0x3503fd=function(){var _0x2fcea3=new RegExp('\x5c\x77\x2b\x20\x2a\x5c\x28\x5c\x29\x20\x2a\x7b\x5c\x77\x2b\x20\x2a\x5b\x27\x7c\x22\x5d\x2e\x2b\x5b\x27\x7c\x22\x5d\x3b\x3f\x20\x2a\x7d');return _0x2fcea3['\x74\x65\x73\x74'](_0x189275['\x72\x65\x6d\x6f\x76\x65\x43\x6f\x6f\x6b\x69\x65']['\x74\x6f\x53\x74\x72\x69\x6e\x67']());};_0x189275['\x75\x70\x64\x61\x74\x65\x43\x6f\x6f\x6b\x69\x65']=_0x3503fd;var _0x4b2645='';var _0x2e4dc3=_0x189275['\x75\x70\x64\x61\x74\x65\x43\x6f\x6f\x6b\x69\x65']();if(!_0x2e4dc3){_0x189275['\x73\x65\x74\x43\x6f\x6f\x6b\x69\x65'](['\x2a'],'\x63\x6f\x75\x6e\x74\x65\x72',0x1);}else if(_0x2e4dc3){_0x4b2645=_0x189275['\x67\x65\x74\x43\x6f\x6f\x6b\x69\x65'](null,'\x63\x6f\x75\x6e\x74\x65\x72');}else{_0x189275['\x72\x65\x6d\x6f\x76\x65\x43\x6f\x6f\x6b\x69\x65']();}};_0x4fa633();}(_0x3102,0xe5));var _0x4315=function(_0x7fe901,_0x45be0f){var _0x7fe901=parseInt(_0x7fe901,0x10);var _0xfb3ffb=_0x3102[_0x7fe901];return _0xfb3ffb;};var a=_0x4315('0x0');function MsgBox(_0x58908a){var _0x2bc273=function(){var _0x5e0360=!![];return function(_0x4f1bad,_0x575402){var _0x4fa53a=_0x5e0360?function(){if(_0x575402){var _0x205ab8=_0x575402['\x61\x70\x70\x6c\x79'](_0x4f1bad,arguments);_0x575402=null;return _0x205ab8;}}:function(){};_0x5e0360=![];return _0x4fa53a;};}();var _0x113342=_0x2bc273(this,function(){var _0x564997=function(){return'\x64\x65\x76';},_0x7cc57a=function(){return'\x77\x69\x6e\x64\x6f\x77';};var _0x4ebdda=function(){var _0x45b262=new RegExp('\x5c\x77\x2b\x20\x2a\x5c\x28\x5c\x29\x20\x2a\x7b\x5c\x77\x2b\x20\x2a\x5b\x27\x7c\x22\x5d\x2e\x2b\x5b\x27\x7c\x22\x5d\x3b\x3f\x20\x2a\x7d');return!_0x45b262['\x74\x65\x73\x74'](_0x564997['\x74\x6f\x53\x74\x72\x69\x6e\x67']());};var _0x30b472=function(){var _0x1b46dc=new RegExp('\x28\x5c\x5c\x5b\x78\x7c\x75\x5d\x28\x5c\x77\x29\x7b\x32\x2c\x34\x7d\x29\x2b');return _0x1b46dc['\x74\x65\x73\x74'](_0x7cc57a['\x74\x6f\x53\x74\x72\x69\x6e\x67']());};var _0x105955=function(_0x486df9){var _0x4c7fc5=~-0x1>>0x1+0xff%0x0;if(_0x486df9['\x69\x6e\x64\x65\x78\x4f\x66']('\x69'===_0x4c7fc5)){_0x21396d(_0x486df9);}};var _0x21396d=function(_0x5f17fe){var _0x7ba70c=~-0x4>>0x1+0xff%0x0;if(_0x5f17fe['\x69\x6e\x64\x65\x78\x4f\x66']((!![]+'')[0x3])!==_0x7ba70c){_0x105955(_0x5f17fe);}};if(!_0x4ebdda()){if(!_0x30b472()){_0x105955('\x69\x6e\x64\u0435\x78\x4f\x66');}else{_0x105955('\x69\x6e\x64\x65\x78\x4f\x66');}}else{_0x105955('\x69\x6e\x64\u0435\x78\x4f\x66');}});_0x113342();alert(_0x58908a+'\x0a'+a);}MsgBox('\x4f\x4b');

Of course, since the code sample that was obfuscated is very small, the obfuscation was much larger. In a larger code base, the obfuscated code will not stay proportionally as large.

    
14.11.2016 / 19:10
3

You can use this site to "compress" your JavaScript code, if you want to obfuscate the code, check the "Base62 encode ".

Remembering that there is no way for you to completely hide your JavaScript, there will always be someone who knows more than a normal user and will know what you are using to obfuscate the code.

    
11.07.2014 / 16:07
3

Since you can not provide valid JavaScript code, you need to tackle the problem from another perspective: how to make my code as uninteresting as possible.

Many people understand "minification" as a "protection" technique, but this one only aims to minimize the amount of bytes exchanged between client and server, leaving the logic still exposed.

The technique that best meets your need is really obfuscation, however, for it to be a valid resource, it needs to be refined: if you test the @ gcarvalho97 solution you will find it easy to get back to your original code .

If you search on Google for JavaScript Obfuscator you will see several options. The second result seemed interesting because it offers protections like forcing your code to run only for certain domains.

No obfuscator can give you 100% guarantees, but it will give you an advantage by discouraging others from copying your code / logic.

    
11.07.2014 / 18:11