JS compression, obfuscation, and encryption

Source: Internet
Author: User

1. Definition and distinction of the three

compression : Delete all comments in Javascript code, tick mark, line break symbol and useless space, thus compressing JS file size, optimize page loading speed.

obfuscation : by encoding variable and function names into meaningless names (such as function (A,B,C,E,G), etc.), to prevent others from peeking and stealing Javascript source code, but also a certain compression effect.

Encryption : The general use of the Eval method of encryption, the effect and confusion similar, but also to achieve the effect of compression.

As can be seen from the definition, the main purpose of the compression is to eliminate the comments and other useless characters, to streamline the JS code, the purpose of reducing the size of JS file, which is a way of page optimization, and the purpose of confusion and encryption is close to prevent others from viewing the source directly, the code (such as important API) protection, But this only increases the cost of reading code, that is, the so-called anti-gentleman against the villain. But when obfuscation and encryption are used together, the crack time increases when the encryption is first confused (or encrypted and then confused). For the encryption of JS, you can refer to this article: http://www.cnblogs.com/top5/archive/2009/08/07/1540860.html

2.demo

Here's a more intuitive way to see the difference between these four ways (compression, obfuscation, encryption, obfuscation & encryption).

The source JS code is as follows:

View Code

(1) The code after compression is as follows:

1 function xx (num,str) {var a=num;this.aa=a;this.bb=function () {alert (str)};this.cc=function () {for (Var i=0;i<10;i + +) {document.title=i}};this.yy=new yy (), function xxf () {alert ("XXF"), if ((/\{\d+\}/). Test ("A\sdf{2}ab")) alert ("{\ \ \ D} is match! ")}}; Xx.prototype.dd=function () {alert ("dd"), A.yy.ll (), Var fnx=function (i) {this.ab=i;this.aa=function () {alert (This.ab )}};var f1=new FNX (1) f1.aa ()};function yy () {alert (' yy ')};yy.prototype.ll=function () {alert ("Yyll")};var a=new xx ( "Hello"), B=new xx (0, "Ttyp"), eval ("a.aa=20"); A.bb (); b.dd (); alert (A.AA); var k=9;function kk () {var k=0;alert (k)}; KK (); alert (k);

After compression, compared with the source code is only less comments, spaces, line breaks and so on.

(2) The code after the confusion is as follows:

1 function xx (d,e) {var f=d;this.aa=f;this.bb=function () {alert (e)};this.cc=function () {for (Var g=0;g<10;g++) { Document.title=g}};this.yy=new yy (), function xxf () {alert ("XXF"), if ((/\{\d+\}/). Test ("A\sdf{2}ab")) alert ("{\\d} is match! ")}}; Xx.prototype.dd=function () {alert ("dd"), A.yy.ll (), Var fnx=function (e) {this.ab=e;this.aa=function () {alert (This.ab )}};var d=new FNX (1);d. AA ()};function yy () {alert (' yy ')};yy.prototype.ll=function () {alert ("Yyll")};var a=new xx (100, "Hello"), B=new xx (0, "Ttyp"); Eval ("a.aa=20"); A.bb (); b.dd (); alert (A.AA); var c=9;function kk () {var d=0;alert (d)};kk () ; alert (c);

In addition to less comments, spaces and line breaks, the parameters are replaced by a,b,c,d,e and other characters, which improves the difficulty of reading.

(3) The code after the encryption is as follows:

1 eval (function (p,a,c,k,e,d) {e=function (c) {return (c<a? "": E (parseint (C/A))) + ((c=c%a) >35?) String.fromCharCode (c+29): c.tostring (+))};if (! ". Replace (/^/,string)) {while (c--) d[e (c)]=k[c]| | E (c); K=[function (e) {return d[e]}];e=function () {return ' \\w+ '};c=1;}; while (c--) if (K[c]) p=p.replace (New RegExp (' \\b ' +e (c) + ' \\b ', ' G '), k[c]); return p;} (' 3 E (o,l) {5 a=o;6.8=a;6.p=3 () {4 (L)};6.b=3 () {A (5 i=0;i<y;i++) {X.z=i}};6.7=c 7 (); 3 J () {4 ("J"); t ((/\\{\\d+\\}/). S ( "A\\u{2}g")) 4 ("{\\\\d} w v!")}}; E.r.f=3 () {4 ("F"); A.7.h (); 5 n=3 (i) {6.g=i;6.8=3 () {4 (6.G)}};5 m=c N (1); m.8 ()};3 7 () {4 (\ ' 7\ ')};7.r.h=3 () {4 ("D")};5 a=c E (F, "H"), B=c E (0, "G"); E ("A.8=c"); A.P (); B.f (); 4 (a.8); 5 k=9;3 Q () {5 k=0;4 (k)};q (); 4 (k); ', 44, 44, ' | | | function|alert|var|this|yy|aa| | | | new| | xx|dd|ab|ll| | xxf| | Str|f1|fnx|num|bb|kk|prototype|test|if|sdf|match|is|document|10|title|for|cc|20|yyll|eval|100|ttyp|hello '. Split (' | '), 0,{}))

After encryption, in addition to the role of confusion, some code is encrypted, further improve the difficulty of reading.

(4) The code after obfuscation & encryption is as follows

1 eval (function (p,a,c,k,e,d) {e=function (c) {return (c<a? "": E (parseint (C/A))) + ((c=c%a) >35?) String.fromCharCode (c+29): c.tostring (+))};if (! ". Replace (/^/,string)) {while (c--) d[e (c)]=k[c]| | E (c); K=[function (e) {return d[e]}];e=function () {return ' \\w+ '};c=1;}; while (c--) if (K[c]) p=p.replace (New RegExp (' \\b ' +e (c) + ' \\b ', ' G '), k[c]); return p;} (' 4 I (d,e) {5 f=d;6.8=f;6.p=4 () {3 (E)};6.s=4 () {T (5 g=0;g<y;g++) {g.f=g}};6.7=h 7 (); 4 L () {3 ("L"); X ((/\\{\\d+\\}/). V ( "A\\u{2}j")) 3 ("{\\\\d} w r!")}}; I.o.k=4 () {3 ("K"); a.7.m (); 5 n=4 (e) {6.j=e;6.8=4 () {3 (6.J)}};5 d=h N (1);d. 8 ()};4 7 () {3 (\ ' 7\ ')};7.o.m=4 () {3 ("E")};5 a=h I (A, "Z"), b=h I (0, "B");D("A.8=c"); A.P (); B.k (); 3 (a.8); 5 c=9;4 Q () {5 d=0;3 (D)};q (); 3 (C); ', 43, 43, ' | | | alert|function|var|this|yy|aa| | | | | | | | | new|xx|ab|dd|xxf|ll|fnx|prototype|bb|kk|match|cc|for|sdf|test|is|if|10|hello|100|ttyp|20|eval|yyll|title| Document '. Split (' | '), 0,{}))

3. How the browser resolves the confusion and encryption after the JS code

In fact, the variable name as long as the Unicode character on the line, for the JS engine is the same, but people think they are different.

4.js Compression Tool

In fact, JS compression tools Online There are many, online and installed version of all, the article is using Hitachi Jsobfuscator v2.13 green version.

On-line JS compression tool: Http://tool.chinaz.com/js.aspx

5. Precautions

The source JS file must be written according to the specifications, semicolons, annotations, Var and so on should be standardized, otherwise it is prone to error.

6. Reference Articles

(1) http://m.blog.csdn.net/blog/Shb_derek/25032169

(2) http://blog.csdn.net/kof820/article/details/19345929

(3) http://www.zhihu.com/question/29169806

Source: https://www.cnblogs.com/flyrui316/p/5002320.html

JS compression, obfuscation, and encryption

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.