2014-09-03 106 views
0


解密的SqlDataSource從代碼隱藏C#

我有以下的SqlDataSource:

<asp:SqlDataSource ID="myDataSource" runat="server" ConnectionString="<%$ ConnectionStrings:myConnString %>" SelectCommand="SELECT [ID], [Name] FROM [myTable] WHERE (([IsActive] = @IsActive))"> 
         <SelectParameters> 
          <asp:Parameter DefaultValue="True" Name="IsActive" Type="Boolean" /> 
         </SelectParameters> 
</asp:SqlDataSource> 

我加密,並使用以下方法解密我的ConnectionString:

 const string initVector = "4s}T*3Rka&5Z2qE_"; 
     const string saltValue = "Ly8$}7Qm9Fi*x2=D"; 
     const string passPhrase = "K!i3nL9_P=y5o6}Z"; 
     const int keySize = 256; 
     const int passwordIterations = 13; 
     public static string Decrypt(string cipherText) 
     { 
      string strReturn = string.Empty; 
      try 
      { 
       byte[] initVectorBytes = Encoding.ASCII.GetBytes(initVector); 
       byte[] saltValueBytes = Encoding.ASCII.GetBytes(saltValue); 
       byte[] cipherTextBytes = Convert.FromBase64String(cipherText); 
       Rfc2898DeriveBytes password = new Rfc2898DeriveBytes(passPhrase, saltValueBytes, passwordIterations); 
       byte[] keyBytes = password.GetBytes(keySize/8); 
       RijndaelManaged symmetricKey = default(RijndaelManaged); 
       symmetricKey = new RijndaelManaged(); 
       symmetricKey.Mode = CipherMode.CBC; 
       ICryptoTransform decryptor = symmetricKey.CreateDecryptor(keyBytes, initVectorBytes); 
       MemoryStream memoryStream = new MemoryStream(cipherTextBytes); 
       CryptoStream cryptoStream = new CryptoStream(memoryStream, decryptor, CryptoStreamMode.Read); 
       byte[] plainTextBytes = null; 
       plainTextBytes = new byte[cipherTextBytes.Length + 1]; 
       int decryptedByteCount = cryptoStream.Read(plainTextBytes, 0, plainTextBytes.Length); 
       memoryStream.Close(); 
       cryptoStream.Close(); 
       strReturn = Encoding.UTF8.GetString(plainTextBytes, 0, decryptedByteCount); 
      } 
      catch (Exception ex) 
      { 
       strReturn = null; 
      } 
      return strReturn; 
     } 

     public static string Encrypt(string plainText) 
     { 
      string strReturn = string.Empty; 

      try 
      { 
       byte[] initVectorBytes = null; 
       initVectorBytes = System.Text.Encoding.ASCII.GetBytes(initVector); 

       byte[] saltValueBytes = null; 
       saltValueBytes = System.Text.Encoding.ASCII.GetBytes(saltValue); 

       byte[] plainTextBytes = null; 
       plainTextBytes = System.Text.Encoding.UTF8.GetBytes(plainText); 

       Rfc2898DeriveBytes password = default(Rfc2898DeriveBytes); 

       password = new Rfc2898DeriveBytes(passPhrase, saltValueBytes, passwordIterations); 
       byte[] keyBytes = null; 
       int intKeySize = 0; 

       intKeySize = Convert.ToInt32((keySize/8)); 

       keyBytes = password.GetBytes(intKeySize); 

       System.Security.Cryptography.RijndaelManaged symmetricKey = default(System.Security.Cryptography.RijndaelManaged); 
       symmetricKey = new System.Security.Cryptography.RijndaelManaged(); 
       symmetricKey.Mode = System.Security.Cryptography.CipherMode.CBC; 
       System.Security.Cryptography.ICryptoTransform encryptor = default(System.Security.Cryptography.ICryptoTransform); 
       encryptor = symmetricKey.CreateEncryptor(keyBytes, initVectorBytes); 

       System.IO.MemoryStream memoryStream = default(System.IO.MemoryStream); 
       memoryStream = new System.IO.MemoryStream(); 

       System.Security.Cryptography.CryptoStream cryptoStream = default(System.Security.Cryptography.CryptoStream); 
       cryptoStream = new System.Security.Cryptography.CryptoStream(memoryStream, encryptor, System.Security.Cryptography.CryptoStreamMode.Write); 
       cryptoStream.Write(plainTextBytes, 0, plainTextBytes.Length); 

       cryptoStream.FlushFinalBlock(); 

       byte[] cipherTextBytes = null; 
       cipherTextBytes = memoryStream.ToArray(); 

       memoryStream.Close(); 
       cryptoStream.Close(); 

       string cipherText = null; 
       cipherText = Convert.ToBase64String(cipherTextBytes); 

       strReturn = cipherText; 
      } 
      catch (Exception ex) 
      { 
       strReturn = null; 
      } 
      return strReturn; 
     } 

我的問題是,我怎麼能在SQL數據源上放置Decrypt(connString)而在html源碼中使用SQL數據源?

謝謝。

+0

我可能是錯的,但我懷疑唯一的方法是使用ObjectDataSource。我認爲'SqlDataSource'對象的想法是快速簡單地實現標準案例,這是一個不適合的類別。 – 2014-09-03 03:19:22

回答

0

您需要使用自定義ExpressionBuilder來實現此目的。

步驟:

  1. 定義的自定義類:

    [ExpressionPrefix( 「MyConnectionExpression」)] [ExpressionEditor( 「MyConnectionExpressionEditor」)] 公共類MyConnectionStringExpressionBuilder:的ExpressionBuilder { }

參照th是:http://msdn.microsoft.com/en-us/library/system.web.compilation.expressionbuilder(v=vs.110).aspx

  1. 在該樣品中,而不是GetEvalData方法,使用方法,其執行以下操作:

讀取從配置文件中的加密的連接字符串。 使用您的Decrypt方法並將數據作爲對象返回。

  1. 確保您使用此自定義表達式生成器詳細信息更新您的web.config。

然後您就可以開始使用Expression構建器。

+0

道歉,我的代碼格式有問題。 – 2014-09-03 04:19:31

+0

嗯,我沒有得到你。那麼我的webconfig應該看起來像什麼?那我怎麼把它應用到我的頁面上呢? – Haminteu 2014-09-03 04:23:15