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

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

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

  /// <summary>
  /// ASP DECODE构造函数
  /// </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])
  /// <summary>
  /// 打开文件source,Encode解密,重写该文件
  /// </summary>
  /// <param name="source">源文件路径</param>
  /// <param name="dest" >目标文件路径</param>
  /// eg.Response.Write(DncodeFile(Server.MapPath("conn.asp")));
  /// au.wanghr100(灰豆宝宝.net)
  /// md.satchmo
  /// up.2005-7-23 12:48
  /// <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;
   }
  }

  /// <summary>
  /// 解密ASP文件
  /// </summary>
  /// <param name="sourcePath">文件源路径</param>
  /// <param name="destPath">文件目标路径</param>
  /// <param name="file">文件名,源,目标文件名一致</param>
  /// <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])
  /// <summary>
  /// 加密ASP文件
  /// </summary>
  /// <param name="source">源文件</param>
  /// <param name="dest">目标文件</param>
  /// <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;
   }
  }

  /// <summary>
  /// 加密ASP文件
  /// </summary>
  /// <param name="sourcePath">文件源路径</param>
  /// <param name="destPath">文件目标路径</param>
  /// <param name="file">文件名,源,目标文件名一致</param>
  /// <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])
  /// <summary>
  /// 将指定目录下的所有ASP进行解密
  /// </summary>
  /// <param name="source">源目录</param>
  /// <param name="dest">目标目录</param>
  /// <param name="bRecur" >递归子目录</param>
  /// <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;
   }
  }

  /// <summary>
  /// 将指定目录下的所有ASP进行解密
  /// </summary>
  /// <param name="source">源目录</param>
  /// <param name="dest">目标目录</param>
  /// <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])
  /// <summary>
  /// 将指定目标下的所有ASP进行加密
  /// </summary>
  /// <param name="source">源目录</param>
  /// <param name="dest">目标目录</param>
  /// <param name="bRecur" >递归子目录</param>
  /// <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;
   }
  }

  /// <summary>
  /// 将指定目标下的所有ASP进行加密
  /// </summary>
  /// <param name="source">源目录</param>
  /// <param name="dest">目标目录</param>
  /// <returns>解密成功返回TRUE</returns>
  public bool EncodeFolder(string source , string dest)
  {
   return EncodeFolder(source , dest , true);
  }
  #endregion

  #region private string unescape(string onechar)
  /// <summary>
  /// 解密特殊字符回车、换行、左,右括号及@
  /// </summary>
  /// <param name="onechar">特殊字符</param>
  /// <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)
  /// <summary>
  /// 加密特殊字符回车、换行、左,右括号及@
  /// </summary>
  /// <param name="onechar">特殊字符</param>
  /// <param name="outchar">返回加密后的特殊字符</param>
  /// <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)
  /// <summary>
  /// 解密BASE64串
  /// </summary>
  /// <param name="str">BASE64串</param>
  /// <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)
  /// <summary>
  /// 用BASE64加密数字
  /// </summary>
  /// <param name="nSum">校验和</param>
  /// <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)
  /// <summary>
  /// 针对VBScript.Encode特点生成校验和,只对小于0x80的字符进行累加
  /// </summary>
  /// <param name="str">源串</param>
  /// <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)
  /// <summary>
  /// 解密ASP文本
  /// </summary>
  /// <param name="encodingString">源串</param>
  /// <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)
  /// <summary>
  /// 加密ASP文本
  /// </summary>
  /// <param name="encodingString">源串</param>
  /// <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 ("%>" , nPos);
     if(nPos2 < 0)
     {
      unEncodingString += encodingString.Substring (nPos);
      break;
     }
     else
     {
      string strSection = encodingString.Substring (nPos + 2 , nPos2 - nPos - 2);
      //查找第一个字符是否为"@"或"="
      //为"@"则忽略处理
      //为"="表示该串为赋值,"="及之前的内容不做处理
      int nSpecialPos ;
      char chSpecial = FindSpecialChar(strSection , out nSpecialPos);
      if(chSpecial != '@')
      {
       if(chSpecial == '=')
        strSection = strSection.Substring (nSpecialPos + 1);
       string stren = EncodeSection(strSection);

       if(chSpecial == '=')
        unEncodingString += string.Format ("<%{0}{1}%>" , chSpecial , stren);
       else
        unEncodingString += string.Format ("<%{0}%>" , stren);
      }
      else
      {
       int nStart;
       int nLen;
       if(GetPropertyPos(strSection , "Language" , out nStart , out nLen))
        unEncodingString += string.Format ("<%{0}.Encode{1}%>" ,
         strSection.Substring (0 , nStart + nLen) ,
         strSection.Substring (nStart + nLen));
       else
        unEncodingString += string.Format ("<%{0}%>" , strSection);
      }
      nPos2 += 2;
      nPos = encodingString.IndexOf ("<%" , nPos2);
      if(nPos > 0)
      {
       unEncodingString += encodingString.Substring (nPos2 , nPos - nPos2);
      }
      else
      {
       unEncodingString += encodingString.Substring (nPos2);
      }
     }
    }
    return scriptenc(unEncodingString);
   }
  }
  #endregion

  #region private string headdec(string str)
  /// <summary>
  /// 对VBScript.Encode串进行解密
  /// </summary>
  /// <param name="str">加密的ASP串</param>
  /// <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)
  /// <summary>
  /// 删除script标签中语言值,将后缀中的.Encode删除
  /// </summary>
  /// <param name="str">源串</param>
  /// <returns>返回删除.Encode的串</returns>
  private string scriptdec(string str)
  {
   string strRet;
   string str2 = str.ToUpper ();
   int nPos = str2.IndexOf ("<SCRIPT");
   if(nPos < 0)
   {
    return str;
   }
   else
   {
    strRet = str.Substring (0 , nPos);
   }

   while(nPos >= 0)
   {
    int nPos2 = str.IndexOf (">" , nPos);
    if(nPos2 > 0)
    {
     string strScriptHead = str.Substring (nPos + 1 , nPos2 - nPos - 1);
     int nStart;
     int nLen;
     if(GetPropertyPos(strScriptHead , "Language" , out nStart , out nLen))
     {
      int nPos3;
      if(nLen > 0 && (nPos3 = strScriptHead.Substring (nStart , nLen).ToUpper ().IndexOf (".ENCODE")) > 0)
      {
       //Language中存在.Encode标识
       nPos3 += nStart + 1; //多一个"<"符号
       strRet += str.Substring (nPos , nPos3 );
       nPos += nPos3 + ".Encode".Length ;
       strRet += str.Substring (nPos , nPos2 - nPos );
       nPos = nPos2;
      }
     }
     if(nPos != nPos2)
     {
      strRet += str.Substring (nPos , nPos2 - nPos);
      nPos = nPos2;
     }

     nPos = str2.IndexOf ("<SCRIPT" , nPos2);
     if(nPos > 0)
     {
      strRet += str.Substring (nPos2 , nPos - nPos2);
     }
     else
     {
      strRet += str.Substring (nPos2);
     }
    }
    else
    {
     strRet += str.Substring (nPos);
     break;
    }
   }
   return strRet;
  }
  #endregion

  #region private string scriptenc(string str)
  /// <summary>
  /// 查找服务器端脚本位置
  /// </summary>
  /// <param name="str">ASP文本</param>
  /// <returns>服务器脚本的起始位置</returns>
  private string scriptenc(string str)
  {
   string strRet = "";
   string str2 = str.ToUpper ();
   int nPos = str2.IndexOf ("<SCRIPT");
   if(nPos < 0)
   {
    return str;
   }
   else
   {
    strRet = str.Substring (0 , nPos);
   }

   while(nPos >= 0)
   {
    int nPos2 = str.IndexOf (">" , nPos);
    if(nPos2 > 0)
    {
     string strScriptHead = str.Substring (nPos + 1 , nPos2 - nPos - 1);
     int nStart;
     int nLen;
     if(GetPropertyPos(strScriptHead , "RunAt" , out nStart , out nLen) &&
      nLen > 0 &&
      strScriptHead.Substring (nStart , nLen).ToUpper ().Equals ("SERVER"))
     {
      //加密内容
      if(GetPropertyPos(strScriptHead , "Language" , out nStart , out nLen))
      {
       if(nLen > 0 && strScriptHead.Substring (nStart , nLen).ToUpper ().IndexOf (".ENCODE") < 0)
       {
        //Language中不存在.Encode标识
        strRet += str.Substring (nPos , nStart + nLen + 1);
        strRet += ".Encode";
        nPos += nStart + nLen + 1;
        strRet += str.Substring (nPos , nPos2 - nPos + 1);
        nPos = nPos2 + 1;
       }
       else
       {
        strRet += str.Substring (nPos , nPos2 - nPos + 1);
        nPos = nPos2 + 1;
       }
       nPos2 = str2.IndexOf ("</SCRIPT>" , nPos);
       if(nPos2 > 0)
       {
        strRet += EncodeSection(str.Substring (nPos , nPos2 - nPos));
        nPos = nPos2;
       }
       else
       {
        strRet += EncodeSection(str.Substring (nPos));
        break;
       }
      }
     }
     //复制内容
     nPos2 = str2.IndexOf ("<SCRIPT" , nPos2);
     if(nPos2 > 0)
     {
      strRet += str.Substring (nPos , nPos2 - nPos);
      nPos = nPos2;
     }
     else
     {
      strRet += str.Substring (nPos);
      break;
     }
    }
    else
    {
     strRet += str.Substring (nPos);
     break;
    }
   }
   return strRet;
  }
  #endregion

  #region private bool GetPropertyPos(string str , string strProperty , out int nStart , out int nLen)
  /// <summary>
  /// 获得标签中的属性值位置
  /// </summary>
  /// <param name="str">ASP标签</param>
  /// <param name="strProperty">属性ID</param>
  /// <param name="nStart">属性值起始位置</param>
  /// <param name="nLen">属性值长度</param>
  /// <returns>获得属性成功返回TRUE</returns>
  private bool GetPropertyPos(string str , string strProperty , out int nStart , out int nLen)
  {
   nStart = -1;
   nLen = 0;
   int nPos = str.ToUpper ().IndexOf (strProperty.ToUpper ());
   if(nPos >= 0)
   {
    nPos += strProperty.Length ;
    while(nPos < str.Length )
    {
     if(str[nPos] == ' ' || str[nPos] == '=' || str[nPos] == '/t' || str[nPos] == '/r' || str[nPos] == '/n')
     {
      nPos ++;
     }
     else if(str[nPos] == '/"')
     {
      nStart = nPos + 1;
      nLen = str.IndexOf ('/"' , nStart) - nStart;
      return true;
     }
     else
     {
      nStart = nPos;
      while(nPos < str.Length )
      {
       if(str[nPos] == ' ' || str[nPos] == '/t' || str[nPos] == '/r' || str[nPos] == '/n')
       {
        nLen = nPos - nStart;
        return true;
       }
       else
       {
        nPos ++;
        continue;
       }
      }
      nLen = str.Length - nStart;
      return true;
     }
    }
   }
   return false;
  }
  #endregion

  #region private char FindSpecialChar(string str , out int nPos)
  /// <summary>
  /// 查找第一个特殊字符,'@'或'='
  /// </summary>
  /// <param name="str">字符串</param>
  /// <param name="nPos">返回找到字符的位置</param>
  /// <returns>返回找到的字符,否则返回0</returns>
  private char FindSpecialChar(string str , out int nPos)
  {
   char chRet = '/0';
   nPos = 0;
   while(nPos < str.Length )
   {
    if(str[nPos] == ' ' || str[nPos] == '/t' || str[nPos] == '/r' || str[nPos] == '/n')
    {
     nPos ++;
     continue;
    }
    else if(str[nPos] == '@')
    {
     chRet = '@';
    }
    else if(str[nPos] == '=')
    {
     chRet = '=';
    }
    break;
   }
   return chRet;
  }
  #endregion

  #region private string EncodeSection(string str)
  /// <summary>
  /// 对ASP服务端脚本进行加密
  /// </summary>
  /// <param name="str">脚本片段</param>
  /// <returns>加密后的服务器代码片段</returns>
  private string EncodeSection(string str)
  {
   int nSum = GetSum(str);
   string strTail = encodeBase64(nSum);
   string strData = "";
   int nCount = 0;
   for(int i = 0;i < str.Length ;i ++)
   {
    char chOne;
    if(str[i] > 0x7f)
    {
     strData += str[i];
    }
    else if(escape(str[i] , out chOne))
    {
     strData += "@" + chOne;
     nCount ++;
    }
    else
    {
     int nRow = pick_encoding[nCount % 64];
     int nCol = str[i] - 31;
     if(nCol < 0)
      nCol = 0;
     strData += (char)rawData[nCol * 3 + nRow];

     nCount ++;
    }
   }
   //增加头与尾
   string strHead = encodeBase64(strData.Length );
   return string.Format ("#@~^{1}AA=={0}{2}AA==^#~@" , strData , strHead , strTail);
  }
  #endregion
 }
}

已标记关键词 清除标记
相关推荐
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页