简体   繁体   English

从CodeBehind C#解密SQLDataSource

[英]Decrypt SQLDataSource from CodeBehind C#


I have the following SQLDataSource: 我有以下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>

I am Encrypting and Decrypting my connectionstring using the following method: 我正在使用以下方法对我的连接字符串进行加密和解密:

        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;
        }

My Question is, how can I put Decrypt(connString) on the SQL DataSource while the SQL Data Source in html source? 我的问题是,当在HTML源代码中使用SQL数据源时,如何将Decrypt(connString)放在SQL数据源上?

Thank you. 谢谢。

You need to use a custom ExpressionBuilder to achieve this. 您需要使用自定义ExpressionBuilder来实现。

Steps: 脚步:

  1. Define a custom class: 定义一个自定义类:

    [ExpressionPrefix("MyConnectionExpression")] [ExpressionEditor("MyConnectionExpressionEditor")] public class MyConnectionStringExpressionBuilder : ExpressionBuilder { } [ExpressionPrefix(“ MyConnectionExpression”)] [ExpressionEditor(“ MyConnectionExpressionEditor”)]公共类MyConnectionStringExpressionBuilder:ExpressionBuilder {}

Refer to this: http://msdn.microsoft.com/en-us/library/system.web.compilation.expressionbuilder(v=vs.110).aspx 引用此: http : //msdn.microsoft.com/zh-cn/library/system.web.compilation.expressionbuilder(v=vs.110).aspx

  1. In that sample, instead of the GetEvalData method, use your method which does the following: 在该示例中,请使用执行以下操作的方法来代替GetEvalData方法:

Reads the encrypted connection string from the config file. 从配置文件中读取加密的连接字符串。 Uses your Decrypt method and returns the data as an object. 使用您的Decrypt方法并将数据作为对象返回。

  1. ensure you update your web.config with this custom expression builder details. 确保使用此自定义表达式构建器详细信息更新web.config。

then you can start using the Expression builder. 然后可以开始使用“表达式”构建器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM