Rumah > hujung hadapan web > tutorial js > CLASS_CONFUSION JS混淆 全源码_js面向对象

CLASS_CONFUSION JS混淆 全源码_js面向对象

WBOY
Lepaskan: 2016-05-16 19:07:08
asal
1085 orang telah melayarinya

利用随机字符串代替变量等,混淆原代码,保护自己写的代码,不利于别人直接剽窃

复制代码 代码如下:

<script> <BR><!-- <BR>/**//**//**//** <BR>** <BR>============================================================== <BR>==================================== <BR>** 类名:CLASS_CONFUSION <BR>** 功能:JS混淆 <BR>** 示例: <BR> --------------------------------------------------------- <BR>------------------------------------------ <br><br> var xx = new CLASS_CONFUSION(code); <br><br> document.getElementById("display").innerHTML = <BR>xx.confusion(); <br><br> --------------------------------------------------------- <BR>------------------------------------------ <BR>** 作者:ttyp <BR>** 邮件:ttyp@21cn.com <BR>** 日期:2006-3-20 <BR>** 版本:0.12 <BR>** <BR>============================================================== <BR>==================================== <BR>**/ <br><br>function CLASS_CONFUSION(code){ <BR> //哈希表类 <BR> function Hashtable(){ <BR> this._hash = new Object(); <BR> this.add = function(key,value){ <BR> if(typeof(key)!="undefined"){ <BR> if(this.contains(key)==false){ <BR> this._hash[key]=typeof <BR>(value)=="undefined"?null:value; <BR> return true; <BR> } else { <BR> return false; <BR> } <BR> } else { <BR> return false; <BR> } <BR> } <BR> this.remove = function(key){delete this._hash <BR>[key];} <BR> this.count = function(){var i=0;for(var k in <BR>this._hash){i++;} return i;} <BR> this.items = function(key){return this._hash <BR>[key];} <BR> this.contains = function(key){return typeof <BR>(this._hash[key])!="undefined";} <BR> this.clear = function(){for(var k in <BR>this._hash){delete this._hash[k];}} <br><br> } <br><br> function VariableMap(parent){ <BR> this.table = new Hashtable(); <BR> this.level = parent?parent.level+1:0; <BR> this.parent= parent; <BR> this.add = function(key,value){this.table.add <BR>(key,value)}; <BR> this.items = function(key){return this.table.items <BR>(key)}; <BR> this.count = function(){return this.table.count()}; <BR> this.contains = function(key){return <BR>this.table.contains(key);} <BR> this.isParameter = false; <BR> } <br><br> this._caseSensitive = true; <br><br> //字符串转换为哈希表 <BR> this.str2hashtable = function(key,cs){ <br><br> var _key = key.split(/,/g); <BR> var _hash = new Hashtable(); <BR> var _cs = true; <br><br><BR> if(typeof(cs)=="undefined"||cs==null){ <BR> _cs = this._caseSensitive; <BR> } else { <BR> _cs = cs; <BR> } <br><br> for(var i in _key){ <BR> if(_cs){ <BR> _hash.add(_key[i]); <BR> } else { <BR> _hash.add((_key[i]+"").toLowerCase()); <BR> } <br><br> } <BR> return _hash; <BR> } <br><br> //获得需要转换的代码 <BR> this._codetxt = code; <br><br> if(typeof(syntax)=="undefined"){ <BR> syntax = ""; <BR> } <br><br> this._deleteComment = false; <BR> //是否大小写敏感 <BR> this._caseSensitive = true; <BR> //得到关键字哈希表 <BR> this._keywords = this.str2hashtable <BR>("switch,case,delete,default,typeof,for,in,function,void,this, <BR>boolean,while,if,return,new,true,false,try,catch,throw,null,el <BR>se,do,var"); <BR> this._function = this.str2hashtable("function"); <BR> this._var = "var"; <BR> this._beginBlock = "{"; <BR> this._endBlock = "}"; <br><br> this._window = this.str2hashtable <BR>("alert,escape,unescape,document,parseInt,parseFloat"); <BR> //得到内建对象哈希表 <BR> this._commonObjects = this.str2hashtable <BR>("String,Number,Boolean,RegExp,Error,Math,Date,Object,Array,Gl <BR>obal"); <BR> //得到分割字符 <BR> this._wordDelimiters= "  ,.?!;:\\/<>(){}[]\"'\r\n\t=+- <BR>|*%@#$^&"; <BR> //引用字符 <BR> this._quotation = this.str2hashtable("\",'"); <BR> //行注释字符 <BR> this._lineComment = "//"; <BR> //转义字符 <BR> this._escape = "\\"; <BR> //多行引用开始 <BR> this._commentOn = "/*"; <BR> //多行引用结束 <BR> this._commentOff = "*/"; <BR> this._execute = "eval"; <BR> //引用调用字符 <BR> this._call = "."; <BR> this._varPause = "="; <BR> this._varContinue = ","; <BR> //变量个数 <BR> this._varNum = 0; <br><br> this.confusion = function() { <BR> var codeArr = new Array(); <BR> var word_index = 0; <BR> var htmlTxt = new Array(); <br><br><BR> //得到分割字符数组(分词) <BR> for (var i = 0; i < this._codetxt.length; i++) { <br><br> if (this._wordDelimiters.indexOf <BR>(this._codetxt.charAt(i)) == -1) { //找不到关键字 <BR> if (codeArr[word_index] == null || typeof <BR>(codeArr[word_index]) == 'undefined') { <BR> codeArr[word_index] = ""; <BR> } <BR> codeArr[word_index] += this._codetxt.charAt <BR>(i); <BR> } else { <BR> if (typeof(codeArr[word_index]) != 'undefined' <BR>&& codeArr[word_index].length > 0) <BR> word_index++; <BR> codeArr[word_index++] = this._codetxt.charAt <BR>(i); <BR> } <BR> } <br><br><BR> var quote_opened = false; //引用标记 <BR> var slash_star_comment_opened = false; //多行注 <BR>释标记 <BR> var slash_slash_comment_opened = false; //单行注 <BR>释标记 <BR> var line_num = 1; //行号 <BR> var quote_char = ""; //引用 <BR>标记类型 <BR> var call_opened = false; <BR> var call_string = ""; <BR> var var_opened = false; <BR> var var_pause = false; <BR> var function_opened = false; <BR> var parameter_opened = false; <br><br> var var_map = new VariableMap <BR>(); <BR> var cur_var_map = var_map; <BR> var execute_opened = false; <br><br> //按分割字,分块显示 <BR> for (var i=0; i <=word_index; i++){ <br><br> //单独处理指针引用 <BR> if(call_opened&&typeof(codeArr[i])!="undefined"){ <BR> if(call_string.length==0){ <BR> if(this.isVar(codeArr[i])){ <BR> call_string +=codeArr[i]; <BR> }else{ <BR> htmlTxt[htmlTxt.length] = "[\"" + <BR>this.toHex(call_string) + "\"]"; <BR> if(codeArr[i]!=this._call){ <BR> htmlTxt[htmlTxt.length] = codeArr <BR>[i]; <BR> call_opened = false; <BR> } <BR> call_string = ""; <BR> } <BR> } else { <BR> if(!this.isVar(codeArr[i])){ <BR> htmlTxt[htmlTxt.length] = "[\"" + <BR>this.toHex(call_string) + "\"]"; <BR> if(codeArr[i]!=this._call){ <BR> htmlTxt[htmlTxt.length] = codeArr <BR>[i]; <BR> call_opened = false; <BR> } <BR> call_string = ""; <BR> }else{ <BR> htmlTxt[htmlTxt.length] = "[\"" + <BR>this.toHex(call_string) + "\"]"; <BR> } <BR> } <BR> continue; <BR> } <br><br> //处理空行(由于转义带来) <BR> if(typeof(codeArr[i])=="undefined"||codeArr <BR>[i].length==0){ <BR> continue; <BR> } else if(codeArr[i]==" "){ <BR> htmlTxt[htmlTxt.length] = " "; <BR> } else if(codeArr[i]=="\n"){ <BR> //处理换行 <BR> } else if (codeArr[i] == "\r"){ <BR> slash_slash_comment_opened = false; <BR> quote_opened = false; <BR> var_opened = false; <BR> htmlTxt[htmlTxt.length] = "\r\n"; <BR> line_num++; <BR> //处理function里的参数标记 <BR> } else if (!slash_slash_comment_opened&&! <BR>slash_star_comment_opened && !quote_opened &&this.isFunction <BR>(codeArr[i])){ <BR> htmlTxt[htmlTxt.length] = codeArr[i]; <BR> function_opened = true; <BR> } else if (!slash_slash_comment_opened&&! <BR>slash_star_comment_opened && !quote_opened &&codeArr[i]=="("){ <BR> htmlTxt[htmlTxt.length] = codeArr[i]; <BR> if(function_opened == true){ <BR> function_opened =false; <BR> var_opened = true; <BR> cur_var_map = new VariableMap <BR>(cur_var_map); <BR> cur_var_map.isParameter = true; <BR> } <BR> } else if (!slash_slash_comment_opened&&! <BR>slash_star_comment_opened && !quote_opened &&codeArr[i]==")"){ <BR> htmlTxt[htmlTxt.length] = codeArr[i]; <BR> //处理var a = new Class(),b=new Date();类似的 <BR>问题 <BR> if(cur_var_map.isParameter){ <BR> var_opened = false; <BR> var_pause = false; <BR> } <BR> } else if (!slash_slash_comment_opened&&! <BR>slash_star_comment_opened && !quote_opened &&codeArr[i]==";"){ <BR> htmlTxt[htmlTxt.length] = codeArr[i]; <BR> var_opened = false; <BR> var_pause = false; <BR> if(execute_opened){ <BR> execute_opened = false; <BR> } <BR> } else if (!slash_slash_comment_opened&&! <BR>slash_star_comment_opened && !quote_opened &&codeArr[i] <BR>==this._var){ <BR> htmlTxt[htmlTxt.length] = codeArr[i]; <BR> var_opened = true; <BR> var_pause = false; <BR> } else if(!slash_slash_comment_opened&&! <BR>slash_star_comment_opened && !quote_opened &&codeArr[i] <BR>==this._varPause){ <BR> htmlTxt[htmlTxt.length] = codeArr[i]; <BR> var_pause = true; <BR> } else if(!slash_slash_comment_opened&&! <BR>slash_star_comment_opened && !quote_opened &&codeArr[i] <BR>==this._varContinue){ <BR> htmlTxt[htmlTxt.length] = codeArr[i]; <BR> var_pause = false; <BR> } else if(!slash_slash_comment_opened&&! <BR>slash_star_comment_opened && !quote_opened &&codeArr[i] <BR>==this._beginBlock){ <BR> cur_var_map = new VariableMap(cur_var_map); <BR> var_opened = false; <BR> htmlTxt[htmlTxt.length] = codeArr[i]; <BR> } else if(!slash_slash_comment_opened&&! <BR>slash_star_comment_opened && !quote_opened &&codeArr[i] <BR>==this._endBlock){ <BR> cur_var_map = cur_var_map.parent; <BR> if(cur_var_map.isParameter){ <BR> cur_var_map = cur_var_map.parent; <BR> } <BR> htmlTxt[htmlTxt.length] = codeArr[i]; <BR> //处理引用调用 <BR> } else if (!slash_slash_comment_opened&&! <BR>slash_star_comment_opened && !quote_opened &&codeArr[i] <BR>==this._call){ <BR> //判断引用(.)后面第一个是否为字母货_$ <BR> if(i<word_index-1){ <BR> if(this.isVar(codeArr[i+1])){ <BR> if(call_opened){ <BR> htmlTxt[htmlTxt.length] = <BR>this.toHex(call_string); <BR> } <BR> call_opened = true; <BR> }else{ <BR> htmlTxt[htmlTxt.length] = this._call; <BR> } <BR> }else{ <BR> htmlTxt[htmlTxt.length] = this._call; <BR> } <BR> //处理关键字 <BR> } else if (!slash_slash_comment_opened&&! <BR>slash_star_comment_opened && !quote_opened && this.isKeyword <BR>(codeArr[i])){ <BR> htmlTxt[htmlTxt.length] = codeArr[i]; <BR> //处理eval后的字符串 <BR> } else if (!slash_slash_comment_opened&&! <BR>slash_star_comment_opened && !quote_opened && codeArr[i] <BR>==this._execute){ <BR> htmlTxt[htmlTxt.length] = "window[\"" + <BR>this.toHex(codeArr[i]) + "\"]"; <BR> execute_opened = true; <BR> //window内置对象 <BR> } else if (!slash_slash_comment_opened&&! <BR>slash_star_comment_opened && !quote_opened && this.isWindow <BR>(codeArr[i])){ <BR> htmlTxt[htmlTxt.length] = "window[\"" + <BR>this.toHex(codeArr[i]) + "\"]"; <BR> //处理普通对象 <BR> } else if (!slash_slash_comment_opened&&! <BR>slash_star_comment_opened && !quote_opened && <BR>this.isCommonObject(codeArr[i])){ <BR> htmlTxt[htmlTxt.length] = "window[\"" + <BR>this.toHex(codeArr[i]) + "\"]"; <BR> //处理双引号(引号前不能为转义字符) <BR> } else if (!slash_star_comment_opened&&! <BR>slash_slash_comment_opened&&this._quotation.contains(codeArr <BR>[i])){ <BR> if (quote_opened){ <BR> //是相应的引号 <BR> if(quote_char==codeArr[i]){ <BR> htmlTxt[htmlTxt.length] = codeArr[i]; <BR> quote_opened = false; <BR> quote_char = ""; <BR> } else { <BR> htmlTxt[htmlTxt.length] = this.toHex <BR>(codeArr[i]); <BR> } <BR> } else { <BR> htmlTxt[htmlTxt.length] = codeArr[i]; <BR> quote_opened = true; <BR> quote_char = codeArr[i]; <BR> } <BR> //处理转义字符 <BR> } else if(codeArr[i] == this._escape){ <BR> htmlTxt[htmlTxt.length] = codeArr[i]; <BR> if(i<word_index-1){ <BR> if(codeArr[i+1].charCodeAt(0) <BR>>=32&&codeArr[i+1].charCodeAt(0)<=127){ <BR> htmlTxt[htmlTxt.length] = codeArr <BR>[i+1].substr(0,1); <BR> htmlTxt[htmlTxt.length] = this.toHex <BR>(codeArr[i+1].substr(1)); <BR> i=i+1; <BR> } <BR> } <BR> //处理多行注释的开始 <BR> } else if (!slash_slash_comment_opened && ! <BR>slash_star_comment_opened&&!quote_opened&&this.isStartWith <BR>(this._commentOn,codeArr,i)){ <BR> slash_star_comment_opened = true; <BR> htmlTxt[htmlTxt.length] = this._commentOn; <BR> i = i + this.getSkipLength(this._commentOn); <BR> //处理单行注释 <BR> } else if (!slash_slash_comment_opened && ! <BR>slash_star_comment_opened&&!quote_opened&&this.isStartWith <BR>(this._lineComment,codeArr,i)){ <BR> slash_slash_comment_opened = true; <BR> if(!this._deleteComment){ <BR> htmlTxt[htmlTxt.length] = <BR>this._lineComment; <BR> } <BR> i = i + this.getSkipLength(this._lineComment); <BR> //处理忽略词 <BR> } else if (!slash_slash_comment_opened && ! <BR>slash_star_comment_opened&&!quote_opened&&this.isStartWith <BR>(this._ignore,codeArr,i)){ <BR> slash_slash_comment_opened = true; <BR> htmlTxt[htmlTxt.length] = this._ignore; <BR> i = i + this.getSkipLength(this._ignore); <BR> //处理多行注释结束 <BR> } else if (!quote_opened&&! <BR>slash_slash_comment_opened&&this.isStartWith <BR>(this._commentOff,codeArr,i)){ <BR> if (slash_star_comment_opened) { <BR> slash_star_comment_opened = false; <BR> if(!this._deleteComment){ <BR> htmlTxt[htmlTxt.length] = <BR>this._commentOff; <BR> } <BR> i = i + this.getSkipLength <BR>(this._commentOff); <BR> } <BR> } else { <BR> //不是在字符串中 <BR> if(!quote_opened){ <BR> //如果不是在注释重 <BR> if(!slash_slash_comment_opened && ! <BR>slash_star_comment_opened){ <BR> //不是在定义变量时 <BR> if(!var_opened){ <BR> if(this.translateVar <BR>(cur_var_map,codeArr[i])==""){ <BR> htmlTxt[htmlTxt.length] = <BR>codeArr[i]; <BR> }else{ <BR> htmlTxt[htmlTxt.length] = <BR>this.translateVar(cur_var_map,codeArr[i]); <BR> } <BR> }else{ <BR> //不是在暂停变量定义时 <BR> if(var_pause){ <BR> if(this.translateVar <BR>(cur_var_map,codeArr[i])==""){ <BR> htmlTxt[htmlTxt.length] = <BR>codeArr[i]; <BR> }else{ <BR> htmlTxt[htmlTxt.length] = <BR>this.translateVar(cur_var_map,codeArr[i]); <BR> } <BR> }else{ <BR> //变量符合命名规则,并且(变量 <BR>前为空格或制表符或逗号如:var a;var a;var a,b;,还有如果是 <BR>函数参数,如:function(a,b,c) <BR> if(this.isVar(codeArr[i])&& <BR>(i>0&&codeArr[i-1]==" "||codeArr[i-1]=="\t"||codeArr[i-1] <BR>==this._varContinue||cur_var_map.isParameter)){ <BR> var name = <BR>this.getRandName(); <BR> cur_var_map.add(codeArr <BR>[i],name); <BR> htmlTxt[htmlTxt.length] = <BR>this.translateVar(cur_var_map,codeArr[i]); <BR> }else{ <BR> htmlTxt[htmlTxt.length] = <BR>codeArr[i]; <BR> } <BR> } <BR> } <BR> //注释中 <BR> }else{ <BR> if(!this._deleteComment){ <BR> htmlTxt[htmlTxt.length] = codeArr <BR>[i]; <BR> } <BR> } <BR> }else{ <BR> if(execute_opened){ <BR> if(this.translateVar <BR>(cur_var_map,codeArr[i])==""){ <BR> htmlTxt[htmlTxt.length] = codeArr <BR>[i]; <BR> }else{ <BR> htmlTxt[htmlTxt.length] = <BR>this.translateVar(cur_var_map,codeArr[i]); <BR> } <BR> }else{ <BR> htmlTxt[htmlTxt.length] = this.toHex <BR>(codeArr[i]); <BR> } <BR> } <BR> } <br><br> } <br><br> return htmlTxt.join(""); <BR> } <BR>this.isStartWith = function(str,code,index){ <br><br> if(typeof(str)!="undefined"&&str.length>0){ <BR> var cc = new Array(); <BR> for(var i=index;i<index+str.length;i++){ <BR> cc[cc.length] = code[i]; <BR> } <BR> var c = cc.join(""); <BR> if(this._caseSensitive){ <BR> if(str.length>=code[index].length&&c.indexOf <BR>(str)==0){ <BR> return true; <BR> } <BR> }else{ <BR> if(str.length>=code <BR>[index].length&&c.toLowerCase().indexOf(str.toLowerCase()) <BR>==0){ <BR> return true; <BR> } <BR> } <BR> return false; <br><br> } else { <BR> return false; <BR> } <BR> } <br><br> this.isFunction = function(val){ <BR> return this._function.contains(this._caseSensitive? <BR>val:val.toLowerCase()); <BR> } <br><br> this.isKeyword = function(val) { <BR> return this._keywords.contains(this._caseSensitive? <BR>val:val.toLowerCase()); <BR> } <br><br> this.isWindow = function(val) { <BR> return this._window.contains(this._caseSensitive? <BR>val:val.toLowerCase()); <BR> } <br><br> this.isCommonObject = function(val) { <BR> return this._commonObjects.contains <BR>(this._caseSensitive?val:val.toLowerCase()); <BR> } <br><br> this.getSkipLength = function(val){ <BR> var count = 0; <BR> for(var i=0;i<val.length;i++){ <BR> if(this._wordDelimiters.indexOf(val.charAt(i)) <BR>>=0){ <BR> count++; <BR> } <BR> } <BR> if(count>0){ <BR> count=count-1; <BR> } <BR> return count; <BR> } <br><br> //字符串转换为16进制形式 <BR> this.toHex = function(val){ <BR> var str = new Array(); <BR> for(var i=0;i<val.length;i++){ <BR> var c = val.charCodeAt(i); <BR> if(c>=0&&c<256){ <BR> str[str.length] = "\\x" + val.charCodeAt <BR>(i).toString(16); <BR> }else{ <BR> str[str.length] = "\\u" + val.charCodeAt <BR>(i).toString(16); <BR> } <BR> } <BR> return str.join(""); <BR> } <br><br> //获得变量随机名 <BR> this.getRandName = function(){ <BR> var style = parseInt(Math.random()*4); <BR> var len = parseInt(Math.random()*9)+1; <BR> var n = []; <br><br> this._varNum++; <BR> var c = <BR>"abcdefghijklmnopqrstuvwsyzABCDEFGHIJKLMNOPQRSTUVWXYZ_$"; <br><br> for(var i=0;i<len;i++){ <BR> n[n.length] = c.charAt(parseInt(Math.random() <BR>*54)); <BR> } <br><br> return n.join("")+this._varNum; <br><br> } <br><br> //是否符合变量命名字首规则 <BR> this.isVar = function(val){ <BR> return /^[a-zA-Z_\$].*$/.test(val); <BR> } <br><br> //翻译变量,如果返回为空则不存在此变量 <BR> this.translateVar = function(node,key){ <BR> if(node.contains(key)){ <BR> return node.items(key); <BR> } <br><br> var cn = node.parent; <BR> while(cn!=null){ <BR> if(cn.contains(key)){ <BR> return cn.items(key); <BR> } <BR> cn = cn.parent; <BR> } <BR> return ""; <BR> } <br><br><BR>} <br><br>function doConfusion(o){ <BR> var htmltxt = ""; <br><br> if (o == null){ <BR> alert("domNode is null!"); <BR> return; <BR> } <br><br> var _codetxt = ""; <br><br> if(typeof(o)=="object"){ <BR> switch(o.tagName){ <BR> case "TEXTAREA": <BR> case "INPUT": <BR> _codetxt = o.value; <BR> break; <BR> case "DIV": <BR> case "SPAN": <BR> _codetxt = o.innerText; <BR> break; <BR> default: <BR> _codetxt = o.innerHTML; <BR> break; <BR> } <BR> }else{ <BR> _codetxt = o; <BR> } <br><br> var _syn = new CLASS_CONFUSION(_codetxt); <BR> htmltxt = _syn.confusion(); <BR> return htmltxt; <BR>} <br><br><BR>function go() <BR>{ <BR> var code = document.getElementById("code").value; <BR> var xx = new CLASS_CONFUSION(code); <BR> var a = new Date(); <BR> document.getElementById("display").value = xx.confusion(); <BR> alert("共花:" + (new Date().getTime()-a.getTime()) + <BR>"ms"); <BR>} <BR>//--> <BR></script>
Label berkaitan:
sumber:php.cn
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan