zoukankan      html  css  js  c++  java
  • 安装程序为配置文件的连接字符串加密

    由于安全需要,我们发布的程序不应该将连接字符串直接暴露给终端用户,所以有必要对配置文件中的连接字符串加密。

    这里简要介绍在安装程序中为连接字符串加密的方法(针对winform程序)。

    首先,创建一个类库程序,名称为ClientInstallAction,新建类InstallAction.cs。代码如下。

    [RunInstaller(true)]
       
    public class InstallAction : Installer
       {
           
    public override void Install(System.Collections.IDictionary stateSaver)
           {
               
    base.Install(stateSaver);
               mEncryptConfig();
           }

           
    /// <summary>
           
    /// 安装路径
           
    /// </summary>

           
    private string _installPath
           {
               
    get
               {
                   
    return Context.Parameters["TARGETDIR"];
               }
           }
           
    /// <summary>
           
    /// 配置文件路径 configuration file path
           
    /// </summary>
           private string XmlPath
           {
               
    get
               {
                   
    return Path.Combine(_installPath, @"");
               }
           }

           
    private void mEncryptConfig()
           {
               XmlDocument doc 
    = new XmlDocument();
               doc.Load(XmlPath);
               XmlNode connectionStringsNode 
    = doc.SelectSingleNode("/configuration/connectionStrings");//查找connectionstring结点

               DESProtectedConfigurationProvider provider 
    = new DESProtectedConfigurationProvider();

               XmlElement newConnectionStringsNode 
    = doc.CreateElement("EncryptedData");
               newConnectionStringsNode.InnerXml 
    = provider.Encrypt(connectionStringsNode).InnerXml;//加密字符串

               connectionStringsNode.RemoveAll();
               connectionStringsNode.AppendChild(newConnectionStringsNode 
    as XmlNode);//删除并替换connectionstring结点

               XmlAttribute configProtectionAtt 
    = doc.CreateAttribute("configProtectionProvider");
               configProtectionAtt.Value 
    = "DESProtectedConfigurationProvider";

               connectionStringsNode.Attributes.Append(configProtectionAtt);
               doc.Save(XmlPath);
           }
       } 

    然后创建用于加密解密的类DESProtectedConfigurationProvider.cs

    代码如下

    public class DESProtectedConfigurationProvider : ProtectedConfigurationProvider
      {
          
    //Set Key and IV
          private byte[] desKey = { 2344541242774533 };
          
    private byte[] desIV = { 394853255463099 };

          
    //Create DES provider
          private DESCryptoServiceProvider mProvider = new DESCryptoServiceProvider();

          
    private string pName;

          
    // Gets the provider name.
          public override string Name
          {
              
    get { return pName; }
          }

          
    // Performs provider initialization.
          public override void Initialize(string name,
              NameValueCollection config)
          {
              pName 
    = name;
          }

          
    public override System.Xml.XmlNode Decrypt(XmlNode encryptedNode)
          {
              
    string decryptedData = DecryptString(encryptedNode.InnerText);

              XmlDocument xmlDoc 
    = new XmlDocument();
              xmlDoc.PreserveWhitespace 
    = true;
              xmlDoc.LoadXml(decryptedData);

              
    return xmlDoc.DocumentElement;
          }

          
    public override System.Xml.XmlNode Encrypt(XmlNode node)
          {
              
    string encryptedData = EncryptString(node.OuterXml);

              XmlDocument xmlDoc 
    = new XmlDocument();
              xmlDoc.PreserveWhitespace 
    = true;
              xmlDoc.LoadXml(
    "<EncryptedData>" +
                  encryptedData 
    + "</EncryptedData>");

              
    return xmlDoc.DocumentElement;
          }

          
    // Encrypts a configuration section and returns
          
    // the encrypted XML as a string.
          private string EncryptString(string encryptValue)
          {
              
    // Create a memory stream.
              MemoryStream ms = new MemoryStream();

              
    // Create a CryptoStream using the memory stream and the
              
    // CSP DES key. 
              CryptoStream encStream = new CryptoStream(ms, mProvider.CreateEncryptor(desKey, desIV), CryptoStreamMode.Write);

              
    // Create a StreamWriter to write a string
              
    // to the stream.
              StreamWriter sw = new StreamWriter(encStream);

              
    // Write the plaintext to the stream.
              sw.WriteLine(encryptValue);

              
    // Close the StreamWriter and CryptoStream.
              sw.Close();
              encStream.Close();

              
    // Get an array of bytes that represents
              
    // the memory stream.
              string encryptedValue = Convert.ToBase64String(ms.ToArray());

              
    // Close the memory stream.
              ms.Close();

              
    // Return the encrypted byte array.
              return encryptedValue;
          }
          
    // Decrypts an encrypted configuration section and
          
    // returns the unencrypted XML as a string.
          private string DecryptString(string encryptedValue)
          {
              
    // Create a memory stream to the passed buffer.
              MemoryStream ms = new MemoryStream(Convert.FromBase64String(encryptedValue));

              
    // Create a CryptoStream using the memory stream and the
              
    // CSP DES key.
              CryptoStream encStream = new CryptoStream(ms, mProvider.CreateDecryptor(desKey, desIV), CryptoStreamMode.Read);

              
    // Create a StreamReader for reading the stream.
              StreamReader sr = new StreamReader(encStream);

              
    // Read the stream as a string.
              string val = sr.ReadToEnd();

              
    // Close the streams.
              sr.Close();
              encStream.Close();
              ms.Close();

              
    return val;
          }
      } 

    编辑完毕

    在安装程序中将该项目一起输出。

    在安装项目上面选择视图->自定义操作,在安装项上面添加该项目,同时编辑属性CustomerActionData,值为/TARGETDIR="[TARGETDIR]\" 这个属性用来确定安装时的路径。

    最后,不要忘记了在你的配置文件中加上该provider的引用,否则将不能解密
     <configProtectedData defaultProvider="DESProtectedConfigurationProvider">
        
    <providers>
          
    <add name="DESProtectedConfigurationProvider" type="添加上类型和程序集" />
        
    </providers>
      
    </configProtectedData>

    ok,一切完毕。

    后记:简单的说就是在安装项目的时候打开配置文件,通过加密类的算法将连接字符串加密,并替换过去的connectionstring的结点。

  • 相关阅读:
    <<iText in Action 2nd>>3.1节(Introducing the concept of direct content)读书笔记
    <<iText in Action 2nd>>4.1节(Constructing tables)读书笔记
    <<iText in Action 2nd>>3.2节(Adding text at absolute positions)读书笔记
    <<iText in Action 2nd>4.3节(Dealing with large tables)读书笔记
    <<iText in Action 2nd>>3.3节(Working with the ColumnText object)读书笔记
    <<iText in Action 2nd>>3.4节(Creating reusable content)读书笔记
    <<iText in Action 2nd>>2.3节(Adding Anchor, Image, Chapter, and Section objects)读书笔记
    <<iText in Action 2nd>>1.3节(Creating a PDF document in five steps with iText)读书笔记
    IBatisNet+Castle构架开发指南 (续) 附代码生成模板
    IBatisNet+Castle构架开发指南
  • 原文地址:https://www.cnblogs.com/xiao_p/p/841564.html
Copyright © 2011-2022 走看看