C#/vbscript/JS如何加密保护HTML/javascript源代码

  本文通过将源代码进行unicode转换后进行混淆加密,对HTML或者javascript源代码进行加密,将内容转换为一些看似乱码的内容,然后通过客户端的JS脚本反相解析回来。

  HTML/javascript源代码加密混淆算法

1)ASCII编码内可见字符【33~126】,33~79进行+47操作操作,80~126进行-47操作
2)ASCII编码内部可见字符【0~32,127】,【128~133】,不操作,但是在后面增加【,】,变2个字符
3)其他,-5操作,后接@变2字符

  客户端JS脚本解密函数

<script type="text/javascript">
    _$_ = ["", "\x77\x72\x69\x74\x65", "\x6c\x65\x6e\x67\x74\x68", "\x63\x68\x61\x72\x43\x6f\x64\x65\x41\x74", "\x66\x72\x6f\x6d\x43\x68\x61\x72\x43\x6f\x64\x65", "\x73\x75\x62\x73\x74\x72", "\x40", "\x65"];
    var a = { e: function (__0, __1) { var __2, __3, __4, __5 = _$_[0]; if (__1 == 0) { eval(__5) } else if (__1 == 1) { document[_$_[1]](__0) } else { for (__3 = 0; __3 < __0[_$_[2]]; __3++) { { __2 = __0[_$_[3]](__3); if (__2 >= 33 && __2 <= 79) { __5 = __5 + String[_$_[4]](__2 + 47) } else if (__2 >= 80 && __2 <= 126) { __5 = __5 + String[_$_[4]](__2 - 47) } else { __4 = __3 + 1; if (__0[_$_[5]](__4, 1) == _$_[6]) { __5 = __5 + String[_$_[4]](__2 + 5) } else { __5 = __5 + __0[_$_[5]](__3, 1) } __3++ } } } a[_$_[7]](__5, 1) } } };
   //使用方法
  //a.e("被加密混淆过的HTML或者javascript代码")
  a.e("k2 ,9C67lQ9EEAi^^HHH]4@5:?8`ab]?6EQm缑@稆@讹@讜@罌@k^2mkD4C:AE ,EJA6lQE6IE^;2G2D4C:AEQm2=6CEW`abXk^D4C:AEm");
</script>

  客户端JS脚本解密函数详解对照

    //_$_ = ["", "\x77\x72\x69\x74\x65", "\x6c\x65\x6e\x67\x74\x68", "\x63\x68\x61\x72\x43\x6f\x64\x65\x41\x74", "\x66\x72\x6f\x6d\x43\x68\x61\x72\x43\x6f\x64\x65", "\x73\x75\x62\x73\x74\x72", "\x40", "\x65"];
    _$_ = ["", "write", "length", "charCodeAt", "fromCharCode", "substr", "@", "e"]
    var a = { e: function (__0, __1) {
        var __2, __3, __4
        //, __5 = _$_[0];
        , __5 = ""; //用来保存实际解码成功后的内容
        if (__1 == 0) {
            //eval(__5) 
            eval("")
        }
        else if (__1 == 1) {
            //document[_$_[1]](__0)
            document.write(__0);
        }
        else {
            //for (__3 = 0; __3 < __0[_$_[2]]; __3++) {
            for (__3 = 0; __3 < __0.length; __3++) {
                {
                    //__2 = __0[_$_[3]](__3);
                    __2 = __0.charCodeAt(__3); //获取当前字符数字编码
                    //当编码大于等于33且小于79的时候,对当前编码执行+47操作然后生成字符
                    if (__2 >= 33 && __2 <= 79) {//80~126为实际编码
                        //__5 = __5 + String[_$_[4]](__2 + 47)
                        __5 = __5 + String.fromCharCode(__2 + 47)
                    }
                    //当编码大于等于80且小于126的时候,对当前编码执行+47操作然后生成字符
                    else if (__2 >= 80 && __2 <= 126) {//33~79为实际编码
                        //__5 = __5 + String[_$_[4]](__2 - 47)
                        __5 = __5 + String.fromCharCode(__2 - 47)
                    }
                    /*33~79:PQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~
                    80~126:!"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNO*/
                    else {//编码不在33~126之间
                        __4 = __3 + 1; //预读取
                        //if (__0[_$_[5]](__4, 1) == _$_[6]) { __5 = __5 + String[_$_[4]](__2 + 5) }
                        if (__0.substr(__4, 1) == "@") {//编码大于133的字符
                            //__5 = __5 + String[_$_[4]](__2 + 5)
                            __5 = __5 + String.fromCharCode(__2 + 5)
                        }
                        else {//ASCII表示范围内的不可见字符
                            // __5 = __5 + __0[_$_[5]](__3, 1)
                            __5 = __5 + __0.substr(__3, 1)
                        }
                        __3++;//通过算法2),3)知道这些内容占用2个字符,所以循环变量++到下一个字符
                    }
                }
            }
            //a[_$_[7]](__5, 1)
            //==>等价于
            // a.e(__5, 1);
            //==>等价于
            document.write(__5);
        }
    }
};

  下面贴出算法加密保护HTML/javascript实现源代码

  javascript加密保护HTML/javascript实现源代码,一般很少用,因为是客户端的,不好直接读取文件进行加密

<script type="text/javascript">
function encrypt(v) {
    var s = '',charCode;
    for (var i = 0, len = v.length; i < len; i++) {
        charCode = v.charCodeAt(i);
        if (charCode >= 33 && charCode <= 79) s += String.fromCharCode(charCode + 47);
        else if (charCode >= 80 && charCode <= 126) s += String.fromCharCode(charCode - 47);
        else if ((charCode >= 0 && charCode <= 32) || (charCode >= 127 && charCode <= 133)) s += String.fromCharCode(charCode) + ',';
        else s += String.fromCharCode(charCode - 5)+'@';
    }
    return s;
}
var s = '<a href="/">Web开发网</a><script type="text/javascript">alert(123)</'+'script>';
alert('“'+s+'”加密后内容为:“'+encrypt(s)+'”')
 </script>

C#加密保护HTML/javascript实现源代码

    public string encrypt(string v)
    {
        if (string.IsNullOrEmpty(v)) return "";
        string s = "";
        long charCode;
        System.Text.Encoding unicode = System.Text.Encoding.Unicode;
        for (int i = 0, len = v.Length; i < len; i++)
        {
            charCode = (long)v[i];
            if (charCode >= 33 && charCode <= 79) s += ((char)(charCode + 47)).ToString();
            else if (charCode >= 80 && charCode <= 126) s += ((char)(charCode - 47)).ToString();
            else if ((charCode >= 0 && charCode <= 32) || (charCode >= 127 && charCode <= 133)) s += ((char)(charCode)).ToString() + ",";
            else s += ((char)(charCode - 5)).ToString() + "@";
        }
        return s;
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        string s = "<a href=\"/\">Web开发网</a>";
        Response.Write("“" + s + "”加密后内容为:“" + encrypt(s) + "”"); 
    }

vbscript加密保护HTML/javascript实现源代码

function encrypt(v) 
  dim s,charcode,vlen
  vlen=len(v)
  for i=1 to vlen
    charcode=clng("&H"&(hex(AscW(mid(v,i,1)))))
    if charCode >= 33 and charCode <= 79 then
      s=s&chrw(charcode+47)
    elseif charCode >= 80 and charCode <= 126 then
      s=s&chrw(charcode-47)
    elseif (charCode >= 0 and charCode <= 32) or (charCode >= 127 and charCode <= 133) then
      s=s&chrw(charcode)&","
    else
      s=s&chrw(charcode-5)&"@"
    end if
  next
 encrypt=s
end function
dim v
v = "<a href=""/"">Web开发网</a>"
response.Write "“"&v&"”加密后内容为:“"&encrypt(v)&"”"

综合示例,JavaScript实现HTML/javascript源代码加密和解密

<form method="post" onsubmit="return _de_encrypt(this)">
<b>要加密/解密的代码</b><br />
<textarea name="source" rows="6" cols="80"></textarea>
<br /><b>类型:</b><input type="radio" name="type"/>加密 <input type="radio" name="type"/>解密<br />
<b>加密/解密结果</b><br />
<textarea name="target" rows="6" cols="80" readonly="readonly"></textarea><br />
<input type="submit" value="开始"/></form>
<script type="text/javascript">
function _de_encrypt(f){
  if(f.source.value==''){alert('请输入“要加密/解密的代码”!');f.source.focus();return false;}
  if(!f.type[0].checked&&!f.type[1].checked){alert('请选择“类型”!');f.type[0].focus();return false;}
  f.target.value=window[f.type[0].checked?'encrypt':'decrypt'](f.source.value);
  return false;
}
function encrypt(v) {
  var s = '',charCode;
  for (var i = 0, len = v.length; i < len; i++) {
    charCode = v.charCodeAt(i);
    if (charCode >= 33 && charCode <= 79) s += String.fromCharCode(charCode + 47);
    else if (charCode >= 80 && charCode <= 126) s += String.fromCharCode(charCode - 47);
    else if ((charCode >= 0 && charCode <= 32) || (charCode >= 127 && charCode <= 133)) s += String.fromCharCode(charCode) + ',';
    else s += String.fromCharCode(charCode - 5)+'@';
  }
  return s;
}
function decrypt(v){
  var i,charCode,s='',preIndex;
  for (i = 0; i < v.length; i++) {
    charCode = v.charCodeAt(i); 
    if (charCode >= 33 && charCode <= 79)s = s + String.fromCharCode(charCode + 47);
    else if (charCode >= 80 && charCode <= 126)s = s + String.fromCharCode(charCode - 47);
	else{
      preIndex = i + 1; 
      if (v.substr(preIndex, 1) == "@") s = s + String.fromCharCode(charCode + 5)
      else s = s + v.substr(i, 1);
      i++;
    }
  }
  return s;
}
 </script>

 

加支付宝好友偷能量挖...


原创文章,转载请注明出处:C#/vbscript/JS如何加密保护HTML/javascript源代码

评论(1)Web开发网
阅读(961)喜欢(1)JavaScript/Ajax开发技巧