JS代码混淆初步
一直以来,JS前端代码因为必须经过IE明文解析,某些加密的JS如:JScript.Encode也因为树大招风,早就被人破解了。还有些加密的手段,通过复杂的变换,改变源码,但最终都逃不脱最后的审判,像unescape,document.write,eval语句来还原。对于JS代码的保护,最好的手段就是混淆,混淆的目的就是让读懂代码的成本比直接写代码的成本高,混淆不是不可破解的,只是增加破解成本,JAVA,C#的加密都是采用混淆。这样对于非常核心的代码,混淆起不到保护代码的作用,不过JS有这样的代码么?本程序没有用到编译原理,其分词解析思想是基于mechiland(http://www.jzchen.net)的代码高亮程序,参考了蓝色经典的加密混淆专题讨论。
Cunfusion
(){}[]\"'\r\n\t=+-|*%@#$^&";
//引用字符
this._quotation = this.str2hashtable("\",'");
//行注释字符
this._lineComment = "//";
//转义字符
this._escape = "\\";
//多行引用开始
this._commentOn = "/*";
//多行引用结束
this._commentOff = "*/";
this._execute = "eval";
//引用调用字符
this._call = ".";
this._varPause = "=";
this._varContinue = ",";
//变量个数
this._varNum = 0;
this.confusion = function() {
var codeArr = new Array();
var word_index = 0;
var htmlTxt = new Array();
//得到分割字符数组(分词)
for (var i = 0; i 0)
word_index++;
codeArr[word_index++] = this._codetxt.charAt(i);
}
}
var quote_opened = false; //引用标记
var slash_star_comment_opened = false; //多行注释标记
var slash_slash_comment_opened = false; //单行注释标记
var line_num = 1; //行号
var quote_char = ""; //引用标记类型
var call_opened = false;
var call_string = "";
var var_opened = false;
var var_pause = false;
var function_opened = false;
var parameter_opened = false;
var var_map = new VariableMap();
var cur_var_map = var_map;
var execute_opened = false;
//按分割字,分块显示
for (var i=0; i =32&&codeArr[i+1].charCodeAt(0)0&&codeArr[i-1]==" "||codeArr[i-1]=="\t"||codeArr[i-1]==this._varContinue||cur_var_map.isParameter)){
var name = this.getRandName();
cur_var_map.add(codeArr[i],name);
htmlTxt[htmlTxt.length] = this.translateVar(cur_var_map,codeArr[i]);
}else{
htmlTxt[htmlTxt.length] = codeArr[i];
}
}
}
//注释中
}else{
if(!this._deleteComment){
htmlTxt[htmlTxt.length] = codeArr[i];
}
}
}else{
if(execute_opened){
if(this.translateVar(cur_var_map,codeArr[i])==""){
htmlTxt[htmlTxt.length] = codeArr[i];
}else{
htmlTxt[htmlTxt.length] = this.translateVar(cur_var_map,codeArr[i]);
}
}else{
htmlTxt[htmlTxt.length] = this.toHex(codeArr[i]);
}
}
}
}
return htmlTxt.join("");
}
this.isStartWith = function(str,code,index){
if(typeof(str)!="undefined"&&str.length>0){
var cc = new Array();
for(var i=index;i=code[index].length&&c.indexOf(str)==0){
return true;
}
}else{
if(str.length>=code[index].length&&c.toLowerCase().indexOf(str.toLowerCase())==0){
return true;
}
}
return false;
} else {
return false;
}
}
this.isFunction = function(val){
return this._function.contains(this._caseSensitive?val:val.toLowerCase());
}
this.isKeyword = function(val) {
return this._keywords.contains(this._caseSensitive?val:val.toLowerCase());
}
this.isWindow = function(val) {
return this._window.contains(this._caseSensitive?val:val.toLowerCase());
}
this.isCommonObject = function(val) {
return this._commonObjects.contains(this._caseSensitive?val:val.toLowerCase());
}
this.getSkipLength = function(val){
var count = 0;
for(var i=0;i=0){
count++;
}
}
if(count>0){
count=count-1;
}
return count;
}
//字符串转换为16进制形式
this.toHex = function(val){
var str = new Array();
for(var i=0;i=0&&c
/*
这个是一个类
*/
function xx(num,str)
{
//说明
var a = num;
this.aa = a;
this.bb = function(){alert(str);}
this.cc = function(){for(var i=0;i
[Ctrl+A 全选 注:如需引入外部Js需刷新才能执行]
最后,本人原则上不赞成代码混淆加密,写本程序是出于研究目的,本程序本人原创,后续的代码压缩等功能陆续加上,程序中有任何问题请指正,任何转载请写明出处。