一般情况下,Asp.Net程序中,数据库连接字符串大多存储在配置文件中。如果程序不是手工发布,而是通过安装程序部署,那么通常在安装过程中需要用户输入数据库管理员帐号和密码,以便安装数据库。同时将管理员帐号和密码以及数据库服务器地址(或者数据库连接字符串)写入配置文件中。但是如果不加密,尤其是数据库管理员密码,若以明码形式存储,极容易被窃取。 
  
  
这就需要对字符串进行加密处理。但是在安装制作软件(我用的是InstallShield 12)中没有提供专用的加密方法,而且在InstallShield中加密过的字符串在Asp.Net(我用的是C#编写Asp.Net)中要能够被解密,则必须用两者通用的加密解密方法。因此我想到,每个字符的ASCII码都是通用的,只要在InstallShield中对字符的ASCII码进行处理,比如加一或移位,得到一个新的字符串,再到C# 中进行逆向操作,就可以还原为原来的字符串。
在寻思用什么方法加密的时候,偶然想到曾经在一外网站上下载的一段代码,赶紧找将出来(后来还幸运地回忆起那个网站,那就是非常有名的 InstallSite ,大家可以上去逛逛,那里有很多InstallShield的脚本示例代码,可以下载参考参考) ,他的源码如下:
  
     
     /**/
    
    
      
        //////////////////////////////////////////////////////////////////////////////
      
      
        /
    
      /**/
    
    
      
        //////////////////////////////////////////////////////////////////////////////
      
      
        /
      
      
      
    
    
     //
    
      //
    
    
      
       //
    
    
      //
    
    
      
       //
    
    
       Function: String2Password
    
    
      //
    
    
       Function: String2Password
      
       //
    
    
      //
    
    
      
       //
    
    
      //
    
    
      
       //
    
    
      //
    
    
      
       //
    
    
        Purpose: This function takes a string and returns a numeric equivalent   
    
    
      //
    
    
      //
    
    
        Purpose: This function takes a string and returns a numeric equivalent   
    
    
      //
    
    
      
       //
    
    
                 of all the character's ASCII values added together.
    
    
      //
    
    
                 of all the character's ASCII values added together.
      
       //
    
    
      //
    
    
      
       //
    
    
                 (Note: InstallShield may use different values than the ASCII    
    
    
      //
    
    
      //
    
    
                 (Note: InstallShield may use different values than the ASCII    
    
    
      //
    
    
      
       //
    
    
                 table
    
    
      //
    
    
                 table
       I did not verify this.)
      I did not verify this.)
      
       //
    
    
      //
    
    
      
       //
    
    
      //
    
    
      
       //
    
    
      //
      
       
       /**/
    
    
      
        //////////////////////////////////////////////////////////////////////////////
      
      
        /
    
    
      /**/
    
    
      
        //////////////////////////////////////////////////////////////////////////////
      
      
        /
      
      
      
    
    
     function String2Password( nAnswer, szName )
    
      function String2Password( nAnswer, szName )
      
       STRING szRight, szLeft;
           STRING szRight, szLeft;
      
       CHAR   cChar;
           CHAR   cChar;
      
       NUMBER nLength;
           NUMBER nLength;
      
       
      
       begin
       begin
      
       
      
       nAnswer 
    
    
      =
    
    
       
    
    
      0
    
    
      ;
             nAnswer 
    
    
      =
    
    
       
    
    
      0
    
    
      ;
      
       nLength 
    
    
      =
    
    
       StrLength(szName);
             nLength 
    
    
      =
    
    
       StrLength(szName);
      
       while
    
    
       (nLength 
    
    
      >
    
    
       
    
    
      0
    
    
      )
         
    
    
          while
    
    
       (nLength 
    
    
      >
    
    
       
    
    
      0
    
    
      )
      
       StrSub(szRight, szName, 
    
    
      1
    
    
      , (nLength
    
    
      -
    
    
      1
    
    
      ));
              StrSub(szRight, szName, 
    
    
      1
    
    
      , (nLength
    
    
      -
    
    
      1
    
    
      ));
      
       StrSub(szLeft, szName, 
    
    
      0
    
    
      , 
    
    
      1
    
    
      );
              StrSub(szLeft, szName, 
    
    
      0
    
    
      , 
    
    
      1
    
    
      );
      
       cChar 
    
    
      =
    
    
       szLeft[
    
    
      0
    
    
      ];
              cChar 
    
    
      =
    
    
       szLeft[
    
    
      0
    
    
      ];
      
       if
    
    
       (cChar 
    
    
      <
    
    
       
    
    
      0
    
    
      ) then
              
    
    
      if
    
    
       (cChar 
    
    
      <
    
    
       
    
    
      0
    
    
      ) then
      
       cChar 
    
    
      =
    
    
       cChar 
    
    
      &
    
    
       
    
    
      255
    
    
      ;
                   cChar 
    
    
      =
    
    
       cChar 
    
    
      &
    
    
       
    
    
      255
    
    
      ;
      
       endif;
              endif;
      
       szName 
    
    
      =
    
    
       szRight;
              szName 
    
    
      =
    
    
       szRight;
      
       nAnswer 
    
    
      =
    
    
       nAnswer 
    
    
      +
    
    
       cChar;
              nAnswer 
    
    
      =
    
    
       nAnswer 
    
    
      +
    
    
       cChar;
      
       nLength 
    
    
      =
    
    
       nLength 
    
    
      -
    
    
       
    
    
      1
    
    
      ;
              nLength 
    
    
      =
    
    
       nLength 
    
    
      -
    
    
       
    
    
      1
    
    
      ;
      
       endwhile;
         endwhile;
      
       
      
       end;
       end;
    
  
  
这段代码返回的是一个求和的数字,并不完全符合我们的要求,而且我发现cChar=szLeft[0]的取值是有问题的.大家注意到没有,代码作者也在注释中有说明.也不知道 InstallSite 为什么把这种有问题的代码作放在网站上,还居然作为Sample供下载.
于是将其改造了一番:
          
  
  
至于C#的解码就很简单了,这里也一并贴出来,凑个数吧:
  
小结:这里为了说明问题只是用了加1减1的方法,非常简单.我正在研究基于XXTea的加密方法,等有了结果在公布出来.
 这就需要对字符串进行加密处理。但是在安装制作软件(我用的是InstallShield 12)中没有提供专用的加密方法,而且在InstallShield中加密过的字符串在Asp.Net(我用的是C#编写Asp.Net)中要能够被解密,则必须用两者通用的加密解密方法。因此我想到,每个字符的ASCII码都是通用的,只要在InstallShield中对字符的ASCII码进行处理,比如加一或移位,得到一个新的字符串,再到C# 中进行逆向操作,就可以还原为原来的字符串。
在寻思用什么方法加密的时候,偶然想到曾经在一外网站上下载的一段代码,赶紧找将出来(后来还幸运地回忆起那个网站,那就是非常有名的 InstallSite ,大家可以上去逛逛,那里有很多InstallShield的脚本示例代码,可以下载参考参考) ,他的源码如下:
 
     /**/
    
    
      
        //////////////////////////////////////////////////////////////////////////////
      
      
        /
    
      /**/
    
    
      
        //////////////////////////////////////////////////////////////////////////////
      
      
        /
      
      
      
    
     //
    
      //
    
    
       //
    
    
      //
    
    
       //
    
    
       Function: String2Password
    
    
      //
    
    
       Function: String2Password
       //
    
    
      //
    
    
       //
    
    
      //
    
    
       //
    
    
      //
    
    
       //
    
    
        Purpose: This function takes a string and returns a numeric equivalent   
    
    
      //
    
    
      //
    
    
        Purpose: This function takes a string and returns a numeric equivalent   
    
    
      //
    
    
       //
    
    
                 of all the character's ASCII values added together.
    
    
      //
    
    
                 of all the character's ASCII values added together.
       //
    
    
      //
    
    
       //
    
    
                 (Note: InstallShield may use different values than the ASCII    
    
    
      //
    
    
      //
    
    
                 (Note: InstallShield may use different values than the ASCII    
    
    
      //
    
    
       //
    
    
                 table
    
    
      //
    
    
                 table
       I did not verify this.)
      I did not verify this.)
       //
    
    
      //
    
    
       //
    
    
      //
    
    
       //
    
    
      //
       
       /**/
    
    
      
        //////////////////////////////////////////////////////////////////////////////
      
      
        /
    
    
      /**/
    
    
      
        //////////////////////////////////////////////////////////////////////////////
      
      
        /
      
      
      
    
     function String2Password( nAnswer, szName )
    
      function String2Password( nAnswer, szName )
       STRING szRight, szLeft;
           STRING szRight, szLeft;
       CHAR   cChar;
           CHAR   cChar;
       NUMBER nLength;
           NUMBER nLength;
       
       begin
       begin
       
       nAnswer 
    
    
      =
    
    
       
    
    
      0
    
    
      ;
             nAnswer 
    
    
      =
    
    
       
    
    
      0
    
    
      ;
       nLength 
    
    
      =
    
    
       StrLength(szName);
             nLength 
    
    
      =
    
    
       StrLength(szName);
       while
    
    
       (nLength 
    
    
      >
    
    
       
    
    
      0
    
    
      )
         
    
    
          while
    
    
       (nLength 
    
    
      >
    
    
       
    
    
      0
    
    
      )
       StrSub(szRight, szName, 
    
    
      1
    
    
      , (nLength
    
    
      -
    
    
      1
    
    
      ));
              StrSub(szRight, szName, 
    
    
      1
    
    
      , (nLength
    
    
      -
    
    
      1
    
    
      ));
       StrSub(szLeft, szName, 
    
    
      0
    
    
      , 
    
    
      1
    
    
      );
              StrSub(szLeft, szName, 
    
    
      0
    
    
      , 
    
    
      1
    
    
      );
       cChar 
    
    
      =
    
    
       szLeft[
    
    
      0
    
    
      ];
              cChar 
    
    
      =
    
    
       szLeft[
    
    
      0
    
    
      ];
       if
    
    
       (cChar 
    
    
      <
    
    
       
    
    
      0
    
    
      ) then
              
    
    
      if
    
    
       (cChar 
    
    
      <
    
    
       
    
    
      0
    
    
      ) then
       cChar 
    
    
      =
    
    
       cChar 
    
    
      &
    
    
       
    
    
      255
    
    
      ;
                   cChar 
    
    
      =
    
    
       cChar 
    
    
      &
    
    
       
    
    
      255
    
    
      ;
       endif;
              endif;
       szName 
    
    
      =
    
    
       szRight;
              szName 
    
    
      =
    
    
       szRight;
       nAnswer 
    
    
      =
    
    
       nAnswer 
    
    
      +
    
    
       cChar;
              nAnswer 
    
    
      =
    
    
       nAnswer 
    
    
      +
    
    
       cChar;
       nLength 
    
    
      =
    
    
       nLength 
    
    
      -
    
    
       
    
    
      1
    
    
      ;
              nLength 
    
    
      =
    
    
       nLength 
    
    
      -
    
    
       
    
    
      1
    
    
      ;
       endwhile;
         endwhile;
       
       end;
       end;
    
  这段代码返回的是一个求和的数字,并不完全符合我们的要求,而且我发现cChar=szLeft[0]的取值是有问题的.大家注意到没有,代码作者也在注释中有说明.也不知道 InstallSite 为什么把这种有问题的代码作放在网站上,还居然作为Sample供下载.
于是将其改造了一番:
    
       /////////////////////////////////////////////////////////////////////////////////////////////////////////////////
      
//
// string2password.rul
//
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
1 function
    
    
       
    
    
      STRING
    
    
       String2Password(szName )
    
      function
    
    
       
    
    
      STRING
    
    
       String2Password(szName )
      
2 STRING
    
    
       szRight
    
    
      ,
    
    
       szLeft
    
    
      ,
    
    
      szAnswer;
       
    
    
      STRING
    
    
       szRight
    
    
      ,
    
    
       szLeft
    
    
      ,
    
    
      szAnswer;
      
3 CHAR   cChar;
       CHAR   cChar;
      
4 NUMBER
    
    
       nLength;
       
    
    
      NUMBER
    
    
       nLength;
      
5 
      
6 begin
       begin
      
7 
      
8 szAnswer 
    
    
      =
    
    
       
    
    
      ""
    
    
      ;
         szAnswer 
    
    
      =
    
    
       
    
    
      ""
    
    
      ;
      
9 nLength 
    
    
      =
    
    
       StrLength(szName);
         nLength 
    
    
      =
    
    
       StrLength(szName);
      
10 while
    
    
       (nLength 
    
    
      >
    
    
       
    
    
      0
    
    
      )
         
    
    
      while
    
    
       (nLength 
    
    
      >
    
    
       
    
    
      0
    
    
      )
      
11 StrSub(szRight
    
    
      ,
    
    
       szName
    
    
      ,
    
    
       
    
    
      1
    
    
      ,
    
    
       (nLength
    
    
      -
    
    
      1
    
    
      ));
            StrSub(szRight
    
    
      ,
    
    
       szName
    
    
      ,
    
    
       
    
    
      1
    
    
      ,
    
    
       (nLength
    
    
      -
    
    
      1
    
    
      ));
      
12 StrSub(szLeft
    
    
      ,
    
    
       szName
    
    
      ,
    
    
       
    
    
      0
    
    
      ,
    
    
       
    
    
      1
    
    
      );
            StrSub(szLeft
    
    
      ,
    
    
       szName
    
    
      ,
    
    
       
    
    
      0
    
    
      ,
    
    
       
    
    
      1
    
    
      );
      
13 cChar 
    
    
      =
    
    
       STRTOCHAR(szLeft);
            cChar 
    
    
      =
    
    
       STRTOCHAR(szLeft);
      
14 cChar 
    
    
      =
    
    
       cChar 
    
    
      +
    
    
      1
    
    
      ;
            cChar 
    
    
      =
    
    
       cChar 
    
    
      +
    
    
      1
    
    
      ;
      
15 szName 
    
    
      =
    
    
       szRight;
            szName 
    
    
      =
    
    
       szRight;
      
16 CharReplace(szLeft
    
    
      ,
    
    
      STRTOCHAR(szLeft)
    
    
      ,
    
    
      cChar
    
    
      ,
    
    
      0
    
    
      )
            CharReplace(szLeft
    
    
      ,
    
    
      STRTOCHAR(szLeft)
    
    
      ,
    
    
      cChar
    
    
      ,
    
    
      0
    
    
      )
      
17 szAnswer 
    
    
      =
    
    
       szAnswer 
    
    
      +
    
    
       szLeft;
            szAnswer 
    
    
      =
    
    
       szAnswer 
    
    
      +
    
    
       szLeft;
      
18 nLength 
    
    
      =
    
    
       nLength 
    
    
      -
    
    
       
    
    
      1
    
    
      ;
            nLength 
    
    
      =
    
    
       nLength 
    
    
      -
    
    
       
    
    
      1
    
    
      ;
      
19 endwhile
    
    
      ;
         
    
    
      endwhile
    
    
      ;
      
20 return
    
    
       szAnswer;
         
    
    
      return
    
    
       szAnswer;
      
21 end
    
    
      ;
       
    
    
      end
    
    
      ;
    
  
  //
// string2password.rul
//
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
1
 function
    
    
       
    
    
      STRING
    
    
       String2Password(szName )
    
      function
    
    
       
    
    
      STRING
    
    
       String2Password(szName )
      2
 STRING
    
    
       szRight
    
    
      ,
    
    
       szLeft
    
    
      ,
    
    
      szAnswer;
       
    
    
      STRING
    
    
       szRight
    
    
      ,
    
    
       szLeft
    
    
      ,
    
    
      szAnswer;
      3
 CHAR   cChar;
       CHAR   cChar;
      4
 NUMBER
    
    
       nLength;
       
    
    
      NUMBER
    
    
       nLength;
      5
 
      6
 begin
       begin
      7
 
      8
 szAnswer 
    
    
      =
    
    
       
    
    
      ""
    
    
      ;
         szAnswer 
    
    
      =
    
    
       
    
    
      ""
    
    
      ;
      9
 nLength 
    
    
      =
    
    
       StrLength(szName);
         nLength 
    
    
      =
    
    
       StrLength(szName);
      10
 while
    
    
       (nLength 
    
    
      >
    
    
       
    
    
      0
    
    
      )
         
    
    
      while
    
    
       (nLength 
    
    
      >
    
    
       
    
    
      0
    
    
      )
      11
 StrSub(szRight
    
    
      ,
    
    
       szName
    
    
      ,
    
    
       
    
    
      1
    
    
      ,
    
    
       (nLength
    
    
      -
    
    
      1
    
    
      ));
            StrSub(szRight
    
    
      ,
    
    
       szName
    
    
      ,
    
    
       
    
    
      1
    
    
      ,
    
    
       (nLength
    
    
      -
    
    
      1
    
    
      ));
      12
 StrSub(szLeft
    
    
      ,
    
    
       szName
    
    
      ,
    
    
       
    
    
      0
    
    
      ,
    
    
       
    
    
      1
    
    
      );
            StrSub(szLeft
    
    
      ,
    
    
       szName
    
    
      ,
    
    
       
    
    
      0
    
    
      ,
    
    
       
    
    
      1
    
    
      );
      13
 cChar 
    
    
      =
    
    
       STRTOCHAR(szLeft);
            cChar 
    
    
      =
    
    
       STRTOCHAR(szLeft);
      14
 cChar 
    
    
      =
    
    
       cChar 
    
    
      +
    
    
      1
    
    
      ;
            cChar 
    
    
      =
    
    
       cChar 
    
    
      +
    
    
      1
    
    
      ;
      15
 szName 
    
    
      =
    
    
       szRight;
            szName 
    
    
      =
    
    
       szRight;
      16
 CharReplace(szLeft
    
    
      ,
    
    
      STRTOCHAR(szLeft)
    
    
      ,
    
    
      cChar
    
    
      ,
    
    
      0
    
    
      )
            CharReplace(szLeft
    
    
      ,
    
    
      STRTOCHAR(szLeft)
    
    
      ,
    
    
      cChar
    
    
      ,
    
    
      0
    
    
      )
      17
 szAnswer 
    
    
      =
    
    
       szAnswer 
    
    
      +
    
    
       szLeft;
            szAnswer 
    
    
      =
    
    
       szAnswer 
    
    
      +
    
    
       szLeft;
      18
 nLength 
    
    
      =
    
    
       nLength 
    
    
      -
    
    
       
    
    
      1
    
    
      ;
            nLength 
    
    
      =
    
    
       nLength 
    
    
      -
    
    
       
    
    
      1
    
    
      ;
      19
 endwhile
    
    
      ;
         
    
    
      endwhile
    
    
      ;
      20
 return
    
    
       szAnswer;
         
    
    
      return
    
    
       szAnswer;
      21
 end
    
    
      ;
       
    
    
      end
    
    
      ;
    
  至于C#的解码就很简单了,这里也一并贴出来,凑个数吧:
    
       1
    
     public
    
    
       
    
    
      string
    
    
       password2string(
    
    
      string
    
    
       sPass)
    
       
    
    
      public
    
    
       
    
    
      string
    
    
       password2string(
    
    
      string
    
    
       sPass)
      
2 
       
          
    
    
       {
    
    
      
        {
        
3 string
      
      
         sResult 
      
      
        =
      
      
         
      
      
        string
      
      
        .Empty;
                
      
      
        string
      
      
         sResult 
      
      
        =
      
      
         
      
      
        string
      
      
        .Empty;
        
4 for
      
      
         (
      
      
        int
      
      
         i 
      
      
        =
      
      
         
      
      
        0
      
      
        ; i 
      
      
        <
      
      
         sPass.Length; i
      
      
        ++
      
      
        )
                
      
      
        for
      
      
         (
      
      
        int
      
      
         i 
      
      
        =
      
      
         
      
      
        0
      
      
        ; i 
      
      
        <
      
      
         sPass.Length; i
      
      
        ++
      
      
        )
        
5 
         
                
      
      
         {
      
      
        
          {
          
6 int
        
        
           num 
        
        
          =
        
        
           (
        
        
          int
        
        
          )
        
        
          char
        
        
          .Parse(sPass.Substring(i,
        
        
          1
        
        
          )) 
        
        
          -
        
        
           
        
        
          1
        
        
          ;
                      
        
        
          int
        
        
           num 
        
        
          =
        
        
           (
        
        
          int
        
        
          )
        
        
          char
        
        
          .Parse(sPass.Substring(i,
        
        
          1
        
        
          )) 
        
        
          -
        
        
           
        
        
          1
        
        
          ;
          
7 sResult
        
        
          +=
        
        
          ((
        
        
          char
        
        
          )num).ToString();
                      sResult
        
        
          +=
        
        
          ((
        
        
          char
        
        
          )num).ToString();
          
8 }
                  }
        
      
      
        
9 return
      
      
         sResult;
                
      
      
        return
      
      
         sResult;
        
10 }
            }
      
    
  
   public
    
    
       
    
    
      string
    
    
       password2string(
    
    
      string
    
    
       sPass)
    
       
    
    
      public
    
    
       
    
    
      string
    
    
       password2string(
    
    
      string
    
    
       sPass)
      2
 
       
          
    
    
       {
    
    
      
        {
        3
 string
      
      
         sResult 
      
      
        =
      
      
         
      
      
        string
      
      
        .Empty;
                
      
      
        string
      
      
         sResult 
      
      
        =
      
      
         
      
      
        string
      
      
        .Empty;
        4
 for
      
      
         (
      
      
        int
      
      
         i 
      
      
        =
      
      
         
      
      
        0
      
      
        ; i 
      
      
        <
      
      
         sPass.Length; i
      
      
        ++
      
      
        )
                
      
      
        for
      
      
         (
      
      
        int
      
      
         i 
      
      
        =
      
      
         
      
      
        0
      
      
        ; i 
      
      
        <
      
      
         sPass.Length; i
      
      
        ++
      
      
        )
        5
 
         
                
      
      
         {
      
      
        
          {
          6
 int
        
        
           num 
        
        
          =
        
        
           (
        
        
          int
        
        
          )
        
        
          char
        
        
          .Parse(sPass.Substring(i,
        
        
          1
        
        
          )) 
        
        
          -
        
        
           
        
        
          1
        
        
          ;
                      
        
        
          int
        
        
           num 
        
        
          =
        
        
           (
        
        
          int
        
        
          )
        
        
          char
        
        
          .Parse(sPass.Substring(i,
        
        
          1
        
        
          )) 
        
        
          -
        
        
           
        
        
          1
        
        
          ;
          7
 sResult
        
        
          +=
        
        
          ((
        
        
          char
        
        
          )num).ToString();
                      sResult
        
        
          +=
        
        
          ((
        
        
          char
        
        
          )num).ToString();
          8
 }
                  }
        
      
      
        9
 return
      
      
         sResult;
                
      
      
        return
      
      
         sResult;
        10
 }
            }
      
    
  小结:这里为了说明问题只是用了加1减1的方法,非常简单.我正在研究基于XXTea的加密方法,等有了结果在公布出来.


 
					 
					