老掉牙的ASP文件的加密与解密

using System;
using System.IO ;
using System.Text ;

namespace SuperData.ASP
{
///

 1<summary>   
 2/// ASP破解程序   
 3/// 功能:   
 4/// 1.加密一个文件:EncodeFile(源文件,目标文件),EncodeFile(源目录,目标目录,文件名)   
 5/// 2.解密一个文件:DecodeFile(源文件,目标文件),DecodeFile(源目录,目标目录,文件名)   
 6/// 3.加密一个目录:EncodeFolder(源目录,目标目录[,递归子目录])   
 7/// 4.解密一个目录:DecodeFolder(源目录,目标目录[,递归子目录])   
 8/// 原作者:wanghr100(灰豆宝宝.net)   
 9/// 修改者:satchmo   
10/// 更新日期:2005-5-24   
11/// </summary>

public class ASPDecode
{

private enum enumState
{
stateNull = 0 ,
stateCopyInput = 100,
stateReadLen = 101,
stateDecode = 102,
stateUnescape = 103
};

int[] pick_encoding = {
1, 2, 0, 1, 2, 0, 2, 0, 0, 2, 0, 2, 1, 0, 2, 0,
1, 0, 2, 0, 1, 1, 2, 0, 0, 2, 1, 0, 2, 0, 0, 2,
1, 1, 0, 2, 0, 2, 0, 1, 0, 1, 1, 2, 0, 1, 0, 2,
1, 0, 2, 0, 1, 1, 2, 0, 0, 1, 1, 2, 0, 1, 0, 2
};

int[] rawData = {
0x64,0x37,0x69, 0x50,0x7E,0x2C, 0x22,0x5A,0x65, 0x4A,0x45,0x72,
0x61,0x3A,0x5B, 0x5E,0x79,0x66, 0x5D,0x59,0x75, 0x5B,0x27,0x4C,
0x42,0x76,0x45, 0x60,0x63,0x76, 0x23,0x62,0x2A, 0x65,0x4D,0x43,
0x5F,0x51,0x33, 0x7E,0x53,0x42, 0x4F,0x52,0x20, 0x52,0x20,0x63,
0x7A,0x26,0x4A, 0x21,0x54,0x5A, 0x46,0x71,0x38, 0x20,0x2B,0x79,
0x26,0x66,0x32, 0x63,0x2A,0x57, 0x2A,0x58,0x6C, 0x76,0x7F,0x2B,
0x47,0x7B,0x46, 0x25,0x30,0x52, 0x2C,0x31,0x4F, 0x29,0x6C,0x3D,
0x69,0x49,0x70, 0x3F,0x3F,0x3F, 0x27,0x78,0x7B, 0x3F,0x3F,0x3F,
0x67,0x5F,0x51, 0x3F,0x3F,0x3F, 0x62,0x29,0x7A, 0x41,0x24,0x7E,
0x5A,0x2F,0x3B, 0x66,0x39,0x47, 0x32,0x33,0x41, 0x73,0x6F,0x77,
0x4D,0x21,0x56, 0x43,0x75,0x5F, 0x71,0x28,0x26, 0x39,0x42,0x78,
0x7C,0x46,0x6E, 0x53,0x4A,0x64, 0x48,0x5C,0x74, 0x31,0x48,0x67,
0x72,0x36,0x7D, 0x6E,0x4B,0x68, 0x70,0x7D,0x35, 0x49,0x5D,0x22,
0x3F,0x6A,0x55, 0x4B,0x50,0x3A, 0x6A,0x69,0x60, 0x2E,0x23,0x6A,
0x7F,0x09,0x71, 0x28,0x70,0x6F, 0x35,0x65,0x49, 0x7D,0x74,0x5C,
0x24,0x2C,0x5D, 0x2D,0x77,0x27, 0x54,0x44,0x59, 0x37,0x3F,0x25,
0x7B,0x6D,0x7C, 0x3D,0x7C,0x23, 0x6C,0x43,0x6D, 0x34,0x38,0x28,
0x6D,0x5E,0x31, 0x4E,0x5B,0x39, 0x2B,0x6E,0x7F, 0x30,0x57,0x36,
0x6F,0x4C,0x54, 0x74,0x34,0x34, 0x6B,0x72,0x62, 0x4C,0x25,0x4E,
0x33,0x56,0x30, 0x56,0x73,0x5E, 0x3A,0x68,0x73, 0x78,0x55,0x09,
0x57,0x47,0x4B, 0x77,0x32,0x61, 0x3B,0x35,0x24, 0x44,0x2E,0x4D,
0x2F,0x64,0x6B, 0x59,0x4F,0x44, 0x45,0x3B,0x21, 0x5C,0x2D,0x37,
0x68,0x41,0x53, 0x36,0x61,0x58, 0x58,0x7A,0x48, 0x79,0x22,0x2E,
0x09,0x60,0x50, 0x75,0x6B,0x2D, 0x38,0x4E,0x29, 0x55,0x3D,0x3F
};

private char[,] transformed;
private int[] digits;
string undigits = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";

///

1<summary>   
2/// ASP DECODE构造函数   
3/// </summary>

public ASPDecode()
{
//
// TODO: 在此处添加构造函数逻辑
//
transformed = new char[3,0x80];
for(int i=0; i<96; i++)
{
for(int j=0; j<3; j++)
{
transformed[j,rawData[i * 3 + j]] = (char)((i==0) ? 9 : i + 31);
}
}

Console.WriteLine ("============================MapTable============================");
for(int i = 0;i < 3;i ++)
{
for(int j = 0;j < 0x80;j ++)
{
if(transformed[i,j] != '\0')
{
if(transformed[i,j] == '\t')
Console.Write (" ");
else
Console.Write (transformed[i,j]);
}
if(j == 0x60)
{
Console.WriteLine (" ->");
}
}
Console.WriteLine ();
}
Console.WriteLine ("================================================================");

digits = new int[0x80];
for (int i=0; i<26; i++)
{
digits[0x41+i] = i;
digits[0x61+i] = i+26;
}
for (int i=0; i<10; i++)
{
digits[0x30+i] = i+52;
}

digits[0x2b] = 62;
digits[0x2f] = 63;
}

#region public bool DecodeFile(string source , string dest[ , string file])
///

1<summary>   
2/// 打开文件source,Encode解密,重写该文件   
3/// </summary>

///

1<param name="source"/>

源文件路径
///

1<param name="dest"/>

目标文件路径
/// eg.Response.Write(DncodeFile(Server.MapPath("conn.asp")));
/// au.wanghr100(灰豆宝宝.net)
/// md.satchmo
/// up.2005-7-23 12:48
///

1<returns>是否成功</returns>

public bool DecodeFile(string source , string dest)
{
if(System.IO .File .Exists (source))
{
try
{
StreamReader reader = new StreamReader(source , Encoding.Default );
string sFileText = reader.ReadToEnd ();
reader.Close ();
StreamWriter write = new StreamWriter (dest , false , Encoding.Default );
write.Write (strdec(sFileText));
write.Close ();
return true;
}
catch
{
return false;
}
}
else
{
return false;
}
}

///

1<summary>   
2/// 解密ASP文件   
3/// </summary>

///

1<param name="sourcePath"/>

文件源路径
///

1<param name="destPath"/>

文件目标路径
///

1<param name="file"/>

文件名,源,目标文件名一致
///

1<returns>解密成功返回TRUE</returns>

public bool DecodeFile(string sourcePath , string destPath , string file)
{
return DecodeFile(string.Format ("{0}\\{1}" , sourcePath , file) ,
string.Format ("{0}\\{1}" , destPath , file));
}
#endregion

#region public bool EncodeFile(string source , string dest[ , string file])
///

1<summary>   
2/// 加密ASP文件   
3/// </summary>

///

1<param name="source"/>

源文件
///

1<param name="dest"/>

目标文件
///

1<returns>加密成功否</returns>

public bool EncodeFile(string source , string dest)
{
if(System.IO .File .Exists (source))
{
try
{
StreamReader reader = new StreamReader(source , Encoding.Default );
string sFileText = reader.ReadToEnd ();
reader.Close ();
StreamWriter write = new StreamWriter (dest , false , Encoding.Default );
write.Write (strenc(sFileText));
write.Close ();
return true;
}
catch
{
return false;
}
}
else
{
return false;
}
}

///

1<summary>   
2/// 加密ASP文件   
3/// </summary>

///

1<param name="sourcePath"/>

文件源路径
///

1<param name="destPath"/>

文件目标路径
///

1<param name="file"/>

文件名,源,目标文件名一致
///

1<returns>加密成功返回TRUE</returns>

public bool EncodeFile(string sourcePath , string destPath , string file)
{
return EncodeFile(string.Format ("{0}\\{1}" , sourcePath , file) ,
string.Format ("{0}\\{1}" , destPath , file));
}
#endregion

#region public bool DecodeFolder(string source , string dest[ , bool bRecur = true])
///

1<summary>   
2/// 将指定目录下的所有ASP进行解密   
3/// </summary>

///

1<param name="source"/>

源目录
///

1<param name="dest"/>

目标目录
///

1<param name="bRecur"/>

递归子目录
///

1<returns>解密成功返回TRUE</returns>

public bool DecodeFolder(string source , string dest , bool bRecur)
{
if(Directory .Exists (source) && Directory.Exists (dest))
{
string [] allFile = Directory.GetFiles (source , "*.asp");
foreach(string file in allFile)
{
if(!DecodeFile(source , dest , file.Substring (file.LastIndexOf ('\') + 1)))
return false;
}
if(bRecur)
{
string [] allDir = Directory.GetDirectories (source);
foreach(string dir in allDir)
{
string dsubpath = string.Format ("{0}\\{1}" , dest , dir.Substring (dir.LastIndexOf ('\') + 1));
Directory.CreateDirectory (dsubpath);
if(!DecodeFolder(dir , dsubpath))
return false;
}
}
return true;
}
else
{
return false;
}
}

///

1<summary>   
2/// 将指定目录下的所有ASP进行解密   
3/// </summary>

///

1<param name="source"/>

源目录
///

1<param name="dest"/>

目标目录
///

1<returns>解密成功返回TRUE</returns>

public bool DecodeFolder(string source , string dest)
{
return DecodeFolder(source , dest , true);
}
#endregion

#region public bool EncodeFolder(string source , string dest[ , bool bRecur = true])
///

1<summary>   
2/// 将指定目标下的所有ASP进行加密   
3/// </summary>

///

1<param name="source"/>

源目录
///

1<param name="dest"/>

目标目录
///

1<param name="bRecur"/>

递归子目录
///

1<returns>解密成功返回TRUE</returns>

public bool EncodeFolder(string source , string dest , bool bRecur)
{
if(Directory .Exists (source) && Directory.Exists (dest))
{
string [] allFile = Directory.GetFiles (source , "*.asp");
foreach(string file in allFile)
{
if(!EncodeFile(source , dest , file.Substring (file.LastIndexOf ('\') + 1)))
return false;
}
if(bRecur)
{
string [] allDir = Directory.GetDirectories (source);
foreach(string dir in allDir)
{
string dsubpath = string.Format ("{0}\\{1}" , dest , dir.Substring (dir.LastIndexOf ('\') + 1));
Directory.CreateDirectory (dsubpath);
if(!EncodeFolder(dir , dsubpath))
return false;
}
}
return true;
}
else
{
return false;
}
}

///

1<summary>   
2/// 将指定目标下的所有ASP进行加密   
3/// </summary>

///

1<param name="source"/>

源目录
///

1<param name="dest"/>

目标目录
///

1<returns>解密成功返回TRUE</returns>

public bool EncodeFolder(string source , string dest)
{
return EncodeFolder(source , dest , true);
}
#endregion

#region private string unescape(string onechar)
///

1<summary>   
2/// 解密特殊字符回车、换行、左,右括号及@   
3/// </summary>

///

1<param name="onechar"/>

特殊字符
///

1<returns>解密后的特殊字符</returns>

private string unescape(string onechar)
{
string escapes = "#&!*$";
string escaped = "\r\n<>@";

if (onechar[0] > 126)
return onechar;
if (escapes.IndexOf (onechar) != -1)
return escaped.Substring (escapes.IndexOf (onechar), 1);
else
return "?";
}
#endregion

#region private bool escape(char onechar , out char outchar)
///

1<summary>   
2/// 加密特殊字符回车、换行、左,右括号及@   
3/// </summary>

///

1<param name="onechar"/>

特殊字符
///

1<param name="outchar"/>

返回加密后的特殊字符
///

1<returns>是特殊字符返回TRUE</returns>

private bool escape(char onechar , out char outchar)
{
string escapes = "#&!*$";
string escaped = "\r\n<>@";
int nPos = escaped.IndexOf (onechar);
if(nPos >= 0)
{
outchar = escapes[nPos];
return true;
}
else
{
outchar = '\0';
return false;
}
}
#endregion

#region private int decodeBase64(string str)
///

1<summary>   
2/// 解密BASE64串   
3/// </summary>

///

1<param name="str"/>

BASE64串
///

1<returns>解密值</returns>

private int decodeBase64(string str)
{
int val = 0;
val += ((int)digits[str[0]] << 2);
val += ((int)digits[str[1]] >> 4);
val += ((int)digits[str[1]] & 0xf) << 12;
val += (((int)digits[str[2]] >> 2) << 8);
val += (((int)digits[str[2]] & 0x3) << 22);
val += ((int)digits[str[3]] << 16);
return val;
}
#endregion

#region private string encodeBase64(int nSum)
///

1<summary>   
2/// 用BASE64加密数字   
3/// </summary>

///

1<param name="nSum"/>

校验和
///

1<returns>返回加密后的校验和</returns>

private string encodeBase64(int nSum)
{
string strRet = "";
strRet += undigits[(nSum >> 2) & 0x3f];
strRet += undigits[((nSum & 0x3) << 4) + ((nSum >> 12) & 0xf)];
strRet += undigits[((nSum >> 6) & 0x3c) + ((nSum >> 22) & 3)];
strRet += undigits[(nSum >> 16) & 0x3f];
return strRet;
}
#endregion

#region private int GetSum(string str)
///

1<summary>   
2/// 针对VBScript.Encode特点生成校验和,只对小于0x80的字符进行累加   
3/// </summary>

///

1<param name="str"/>

源串
///

1<returns>返回校验和</returns>

private int GetSum(string str)
{
int nRet = 0;
for(int i = 0;i < str.Length ;i ++)
{
int nOne = (int)str[i];
if(nOne <= 0x7f)
{
nRet += nOne;
}
}
return nRet;
}
#endregion

#region private string strdec(string encodingString)
///

1<summary>   
2/// 解密ASP文本   
3/// </summary>

///

1<param name="encodingString"/>

源串
///

1<returns>加密串</returns>

private string strdec(string encodingString)
{
encodingString = scriptdec(headdec(encodingString));
string marker = "#@~^";
int stringIndex = 0;
int scriptIndex = -1;
int unEncodingIndex = 0;
int unEncodinglength = 0;
enumState state = enumState.stateCopyInput ;
string unEncodingString = "";

while(state != enumState.stateNull )
{
switch(state)
{
case (enumState.stateCopyInput ) :
scriptIndex = encodingString.IndexOf (marker , stringIndex);
if (scriptIndex != -1)
{
unEncodingString += encodingString.Substring (stringIndex, scriptIndex - stringIndex);
scriptIndex += marker.Length;
state = enumState.stateReadLen ;
}
else
{
unEncodingString += encodingString.Substring (stringIndex);
state = enumState.stateNull ;
}
break;

case (enumState.stateReadLen ) :
unEncodinglength = decodeBase64(encodingString.Substring (scriptIndex, 6));
scriptIndex += (6 + "==".Length);
state = enumState.stateDecode ;
break;

case (enumState.stateDecode ) :
if (unEncodinglength == 0)
{
stringIndex = scriptIndex + " DQgAAA==^#~@".Length ;
unEncodingIndex = 0;
state = enumState.stateCopyInput ;
break;
}
if (encodingString[scriptIndex] == '@' )
{
state = enumState.stateUnescape ;
unEncodingString += unescape(encodingString.Substring (++scriptIndex, 1));
scriptIndex++;
unEncodinglength -=2;
unEncodingIndex++;
state = enumState.stateDecode ;
break;
}
else
{
if (encodingString[scriptIndex] <= 0x7f)
{
unEncodingString += transformed[pick_encoding[unEncodingIndex%64],encodingString[scriptIndex]];
unEncodingIndex++;
}
else
{
unEncodingString += encodingString[scriptIndex];
}
scriptIndex++;
unEncodinglength--;
break;
}
case enumState.stateUnescape :
unEncodingString += unescape(encodingString.Substring (++scriptIndex, 1));
scriptIndex++;
unEncodinglength -=2;
unEncodingIndex++;
state = enumState.stateDecode ;
break;
}
}

// 下面代码的用意何在?
// int re, arr;
// re = new RegExp("(JScript|VBscript).encode", "gmi");
// while(arr = re.exec(unEncodingString)) unEncodingString = RegExp.leftContext + RegExp.$1 + RegExp.rightContext;
return unEncodingString;
}
#endregion

#region private string strenc(string encodingString)
///

1<summary>   
2/// 加密ASP文本   
3/// </summary>

///

1<param name="encodingString"/>

源串
///

1<returns>返回加密串</returns>

private string strenc(string encodingString)
{
int nPos = encodingString.IndexOf ("``` ");
if(nPos < 0)
return encodingString;
else
{
string unEncodingString = "";
unEncodingString += encodingString.Substring (0 , nPos);
while(nPos >= 0)
{
int nPos2 = encodingString.IndexOf ("

 1if(nPos2 &lt; 0)   
 2{   
 3unEncodingString += encodingString.Substring (nPos);   
 4break;   
 5}   
 6else   
 7{   
 8string strSection = encodingString.Substring (nPos + 2 , nPos2 - nPos - 2);   
 9//查找第一个字符是否为"@"或"="   
10//为"@"则忽略处理   
11//为"="表示该串为赋值,"="及之前的内容不做处理   
12int nSpecialPos ;   
13char chSpecial = FindSpecialChar(strSection , out nSpecialPos);   
14if(chSpecial != '@' )   
15{   
16if(chSpecial == '=')   
17strSection = strSection.Substring (nSpecialPos + 1);   
18string stren = EncodeSection(strSection); 
19
20if(chSpecial == '=')   
21unEncodingString += string.Format ("```
22{0}{1}
23```" , chSpecial , stren);   
24else   
25unEncodingString += string.Format ("```
26{0}
27```" , stren);   
28}   
29else   
30{   
31int nStart;   
32int nLen;   
33if(GetPropertyPos(strSection , "Language" , out nStart , out nLen))   
34unEncodingString += string.Format ("```
35{0}.Encode{1}
36```" ,   
37strSection.Substring (0 , nStart + nLen) ,   
38strSection.Substring (nStart + nLen));   
39else   
40unEncodingString += string.Format ("```
41{0}
42```" , strSection);   
43}   
44nPos2 += 2;   
45nPos = encodingString.IndexOf ("```
46" , nPos2);   
47if(nPos > 0)   
48{   
49unEncodingString += encodingString.Substring (nPos2 , nPos - nPos2);   
50}   
51else   
52{   
53unEncodingString += encodingString.Substring (nPos2);   
54}   
55}   
56}   
57return scriptenc(unEncodingString);   
58}   
59}   
60#endregion 
61
62#region private string headdec(string str)   
63///

<summary>
/// 对VBScript.Encode串进行解密
/// </summary>

1///
``` 加密的ASP串 /// ``` <returns>解密串</returns> ``` private string headdec(string str) { int nPos = str.IndexOf ("<%"); if(nPos < 0) return str; else { int nPos2 = str.IndexOf (" ```" , nPos); if(nPos2 < 0) return str; else { string strSection = str.Substring (nPos + 2 , nPos2 - nPos - 2); int nSpecialPos; char chSpecial = FindSpecialChar(strSection , out nSpecialPos); if(chSpecial != '@' ) return str; else { string strRet = str.Substring (0 , nPos + 2); int nStart; int nLen; if(GetPropertyPos(strSection , "Language" , out nStart , out nLen)) { int nPos3; if(nLen > 0 && (nPos3 = strSection.Substring (nStart , nLen).ToUpper ().IndexOf (".ENCODE")) > 0) { nStart += nPos3; strRet += strSection.Substring (0 , nStart); strRet += strSection.Substring (nStart + ".Encode".Length ); } else strRet += strSection;

}
else
strRet += strSection;
strRet += str.Substring (nPos2);
return strRet;
}

}
}
}
#endregion

#region private string scriptdec(string str)
///

1<summary>   
2/// 删除script标签中语言值,将后缀中的.Encode删除   
3/// </summary>

///

1<param name="str"/>

源串
///

1<returns>返回删除.Encode的串</returns>

private string scriptdec(string str)
{
string strRet;
string str2 = str.ToUpper ();
int nPos = str2.IndexOf ("

  1<script"); (0="" ,="" 0)="" <="" else="" if(npos="" npos);="" return="" str;="" strret="str.Substring" while(npos="" {="" }="">= 0)   
  2{   
  3int nPos2 = str.IndexOf ("&gt;" , nPos);   
  4if(nPos2 &gt; 0)   
  5{   
  6string strScriptHead = str.Substring (nPos + 1 , nPos2 - nPos - 1);   
  7int nStart;   
  8int nLen;   
  9if(GetPropertyPos(strScriptHead , "Language" , out nStart , out nLen))   
 10{   
 11int nPos3;   
 12if(nLen &gt; 0 &amp;&amp; (nPos3 = strScriptHead.Substring (nStart , nLen).ToUpper ().IndexOf (".ENCODE")) &gt; 0)   
 13{   
 14//Language中存在.Encode标识   
 15nPos3 += nStart + 1; //多一个"&lt;"符号   
 16strRet += str.Substring (nPos , nPos3 );   
 17nPos += nPos3 + ".Encode".Length ;   
 18strRet += str.Substring (nPos , nPos2 - nPos );   
 19nPos = nPos2;   
 20}   
 21}   
 22if(nPos != nPos2)   
 23{   
 24strRet += str.Substring (nPos , nPos2 - nPos);   
 25nPos = nPos2;   
 26} 
 27
 28nPos = str2.IndexOf ("<script" ,="" if(npos="" npos2);=""> 0)   
 29{   
 30strRet += str.Substring (nPos2 , nPos - nPos2);   
 31}   
 32else   
 33{   
 34strRet += str.Substring (nPos2);   
 35}   
 36}   
 37else   
 38{   
 39strRet += str.Substring (nPos);   
 40break;   
 41}   
 42}   
 43return strRet;   
 44}   
 45#endregion 
 46
 47#region private string scriptenc(string str)   
 48/// <summary>   
 49/// 查找服务器端脚本位置   
 50/// </summary>   
 51/// <param name="str"/>ASP文本   
 52/// <returns>服务器脚本的起始位置</returns>   
 53private string scriptenc(string str)   
 54{   
 55string strRet = "";   
 56string str2 = str.ToUpper ();   
 57int nPos = str2.IndexOf ("<script"); (0="" ,="" 0)="" <="" else="" if(npos="" npos);="" return="" str;="" strret="str.Substring" while(npos="" {="" }="">= 0)   
 58{   
 59int nPos2 = str.IndexOf ("&gt;" , nPos);   
 60if(nPos2 &gt; 0)   
 61{   
 62string strScriptHead = str.Substring (nPos + 1 , nPos2 - nPos - 1);   
 63int nStart;   
 64int nLen;   
 65if(GetPropertyPos(strScriptHead , "RunAt" , out nStart , out nLen) &amp;&amp;   
 66nLen &gt; 0 &amp;&amp;   
 67strScriptHead.Substring (nStart , nLen).ToUpper ().Equals ("SERVER"))   
 68{   
 69//加密内容   
 70if(GetPropertyPos(strScriptHead , "Language" , out nStart , out nLen))   
 71{   
 72if(nLen &gt; 0 &amp;&amp; strScriptHead.Substring (nStart , nLen).ToUpper ().IndexOf (".ENCODE") &lt; 0)   
 73{   
 74//Language中不存在.Encode标识   
 75strRet += str.Substring (nPos , nStart + nLen + 1);   
 76strRet += ".Encode";   
 77nPos += nStart + nLen + 1;   
 78strRet += str.Substring (nPos , nPos2 - nPos + 1);   
 79nPos = nPos2 + 1;   
 80}   
 81else   
 82{   
 83strRet += str.Substring (nPos , nPos2 - nPos + 1);   
 84nPos = nPos2 + 1;   
 85}   
 86nPos2 = str2.IndexOf ("" , nPos);   
 87if(nPos2 &gt; 0)   
 88{   
 89strRet += EncodeSection(str.Substring (nPos , nPos2 - nPos));   
 90nPos = nPos2;   
 91}   
 92else   
 93{   
 94strRet += EncodeSection(str.Substring (nPos));   
 95break;   
 96}   
 97}   
 98}   
 99//复制内容   
100nPos2 = str2.IndexOf ("<script" ,="" if(npos2="" npos2);=""> 0)   
101{   
102strRet += str.Substring (nPos , nPos2 - nPos);   
103nPos = nPos2;   
104}   
105else   
106{   
107strRet += str.Substring (nPos);   
108break;   
109}   
110}   
111else   
112{   
113strRet += str.Substring (nPos);   
114break;   
115}   
116}   
117return strRet;   
118}   
119#endregion 
120
121#region private bool GetPropertyPos(string str , string strProperty , out int nStart , out int nLen)   
122/// <summary>   
123/// 获得标签中的属性值位置   
124/// </summary>   
125/// <param name="str"/>ASP标签   
126/// <param name="strProperty"/>属性ID   
127/// <param name="nStart"/>属性值起始位置   
128/// <param name="nLen"/>属性值长度   
129/// <returns>获得属性成功返回TRUE</returns>   
130private bool GetPropertyPos(string str , string strProperty , out int nStart , out int nLen)   
131{   
132nStart = -1;   
133nLen = 0;   
134int nPos = str.ToUpper ().IndexOf (strProperty.ToUpper ());   
135if(nPos &gt;= 0)   
136{   
137nPos += strProperty.Length ;   
138while(nPos &lt; str.Length )   
139{   
140if(str[nPos] == ' ' || str[nPos] == '=' || str[nPos] == '\t' || str[nPos] == '\r' || str[nPos] == '\n')   
141{   
142nPos ++;   
143}   
144else if(str[nPos] == '\"')   
145{   
146nStart = nPos + 1;   
147nLen = str.IndexOf ('\"' , nStart) - nStart;   
148return true;   
149}   
150else   
151{   
152nStart = nPos;   
153while(nPos &lt; str.Length )   
154{   
155if(str[nPos] == ' ' || str[nPos] == '\t' || str[nPos] == '\r' || str[nPos] == '\n')   
156{   
157nLen = nPos - nStart;   
158return true;   
159}   
160else   
161{   
162nPos ++;   
163continue;   
164}   
165}   
166nLen = str.Length - nStart;   
167return true;   
168}   
169}   
170}   
171return false;   
172}   
173#endregion 
174
175#region private char FindSpecialChar(string str , out int nPos)   
176/// <summary>   
177/// 查找第一个特殊字符, '@' 或'='   
178/// </summary>   
179/// <param name="str"/>字符串   
180/// <param name="nPos"/>返回找到字符的位置   
181/// <returns>返回找到的字符,否则返回0</returns>   
182private char FindSpecialChar(string str , out int nPos)   
183{   
184char chRet = '\0';   
185nPos = 0;   
186while(nPos &lt; str.Length )   
187{   
188if(str[nPos] == ' ' || str[nPos] == '\t' || str[nPos] == '\r' || str[nPos] == '\n')   
189{   
190nPos ++;   
191continue;   
192}   
193else if(str[nPos] == '@' )   
194{   
195chRet = '@' ;   
196}   
197else if(str[nPos] == '=')   
198{   
199chRet = '=';   
200}   
201break;   
202}   
203return chRet;   
204}   
205#endregion 
206
207#region private string EncodeSection(string str)   
208/// <summary>   
209/// 对ASP服务端脚本进行加密   
210/// </summary>   
211/// <param name="str"/>脚本片段   
212/// <returns>加密后的服务器代码片段</returns>   
213private string EncodeSection(string str)   
214{   
215int nSum = GetSum(str);   
216string strTail = encodeBase64(nSum);   
217string strData = "";   
218int nCount = 0;   
219for(int i = 0;i &lt; str.Length ;i ++)   
220{   
221char chOne;   
222if(str[i] &gt; 0x7f)   
223{   
224strData += str[i];   
225}   
226else if(escape(str[i] , out chOne))   
227{   
228strData += "@" + chOne;   
229nCount ++;   
230}   
231else   
232{   
233int nRow = pick_encoding[nCount % 64];   
234int nCol = str[i] - 31;   
235if(nCol &lt; 0)   
236nCol = 0;   
237strData += (char)rawData[nCol * 3 + nRow]; 
238
239nCount ++;   
240}   
241}   
242//增加头与尾   
243string strHead = encodeBase64(strData.Length );   
244return string.Format (" #@~^{1}AA=={0}{2}AA ==^#~@" , strData , strHead , strTail);   
245}   
246#endregion   
247}   
248}</script"></script");></script"></script");>
Published At
Categories with Web编程
Tagged with
comments powered by Disqus