VS开发程序用户防范安全问题

  一个开发的系统程序从需求、设计到打包、用户使用的过程中,安全问题一直是开发者关注的焦点。对于用户来说,不考虑加密工具(如加密精灵等),面对的是一个系统的各个组件集合及各类的配置文件( 如App.Config / Web.Config)。其中,涉及到安全防范问题如App.Config配置文件,里面会包含很多信息,包括最不想让用户知道的服务器地址、登录名和密码等,特殊的文件除外。本篇文章会展开两种方式来处理安全问题。

  (1)如果只是简单的防使用人员的话,那么你可以考虑在appsetting或其他的配置节中放加密后的连接字符串,然后在使用的地方先解密再使用,这里我介绍下DES加解密的方式(密匙为8位字节)。

 1 /// <summary> 
 2         /// DES加密,密钥为8位字符 
 3         /// </summary> 
 4         /// <param name="strEncrypt">需要加密的字符串</param> 
 5         /// <param name="strKey">8位的密钥</param> 
 6         /// <returns></returns> 
 7         public static string DesEncrypt(string strEncrypt, string strKey) 
 8         { 
 9             if (string.IsNullOrEmpty(strEncrypt)) return null; 
10             try 
11             { 
12                 DESCryptoServiceProvider des = new DESCryptoServiceProvider(); 
13                 byte[] inputByteArray = Encoding.Default.GetBytes(strEncrypt); 
14                 des.Key = ASCIIEncoding.ASCII.GetBytes(strKey); 
15                 des.IV = ASCIIEncoding.ASCII.GetBytes(strKey); 
16                 MemoryStream ms = new MemoryStream(); 
17                 CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write); 
18                 cs.Write(inputByteArray, 0, inputByteArray.Length); 
19                 cs.FlushFinalBlock(); 
20                 StringBuilder ret = new StringBuilder(); 
21                 foreach (byte b in ms.ToArray()) 
22                 { 
23                     ret.AppendFormat("{0:X2}", b); 
24                 } 
25                 ret.ToString(); 
26                 return ret.ToString(); 
27             } 
28             catch 
29             { 
30                 return null; 
31             } 
32         } 
DES加密,密钥为8位字符
 1 /// <summary> 
 2         /// DES解密,密钥为8为字符 
 3         /// </summary> 
 4         /// <param name="strDecrypt">需要加密的字符串</param> 
 5         /// <param name="strKey">8位的密钥</param> 
 6         /// <returns></returns> 
 7         public string DesDecrypt(string strDecrypt, string strKey) 
 8         { 
 9             if (string.IsNullOrEmpty(strDecrypt)) return null; 
10             try 
11             { 
12                 DESCryptoServiceProvider des = new DESCryptoServiceProvider(); 
13                 byte[] inputByteArray = new byte[strDecrypt.Length / 2]; 
14                 for (int x = 0; x < strDecrypt.Length / 2; x++) 
15                 { 
16                     int i = (Convert.ToInt32(strDecrypt.Substring(x * 2, 2), 16)); 
17                     inputByteArray[x] = (byte)i; 
18                 } 
19                 des.Key = ASCIIEncoding.ASCII.GetBytes(strKey); 
20                 des.IV = ASCIIEncoding.ASCII.GetBytes(strKey); 
21                 MemoryStream ms = new MemoryStream(); 
22                 CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write); 
23                 cs.Write(inputByteArray, 0, inputByteArray.Length); 
24                 cs.FlushFinalBlock(); 
25                 return System.Text.Encoding.Default.GetString(ms.ToArray()); 
26             } 
27             catch 
28             { 
29                 return null; 
30             } 
31         } 
DES解密,密钥为8为字符

关于DES加解密的密匙的获取,一般是内部人员掌控,可访问服务器获取,安全上更有保障。

那么有了上面加密后的一堆数据,如何更改对应配置文件中的某个配置节上的数据,微软提供System.Configuration.dll组件来操作App.Config配置文件等数据将其处理,如:

Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
config.AppSettings.Settings["配置节"].Value = strDesEncrypt;//DES加密后的数据
config.Save();

 缺点:安全性低,局限于非计算机操作者,对于一个业务繁琐的系统不切实际。

   提供参考网站:https://wenda.so.com/q/1370928295068825

  (2) 默认情况下,我们需要对App.config文件里的connectionStrings或其他配置节片断进行加密处理,ASP.NET IIS 注册工具 (Aspnet_regiis.exe)可以胜任这个工作,但这个工具只能针对ASP.NET的Web.config文件,难道我们就没有办法了吗?答案当然是否定的。

  配置选项:

-pdf section webApplicationDirectory 对指定物理(非虚拟)目录中的 Web.config 文件的指定配置节进行解密。
-pef section webApplicationDirectory 对指定物理(非虚拟)目录中的 Web.config 文件的指定配置节进行加密。
-pdf 和-pef 参数是对指定的物理目录里的Web.config文件进行加密,我们可以先将App.config文件改名为Web.config,通过这两个参数便可以“骗”过系统,让它将指定的配置节  进行加密,我们只需要将加密后的文件名改回App.config即可,我们来实验一下:
 第一步:先将目录下的App.config改名为Web.config。
 第二步:打开SDK命令提示,输入命令:aspnet_regiis -pef "配置节" "目录",以我的项目为例,加密前的config文件内容如下:
  
1<?xml version="1.0" encoding="utf-8"?>
 2<configuration>
 3  <configSections>
 4    <section name="dataConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Data.Configuration.DatabaseSettings, Microsoft.Practices.EnterpriseLibrary.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=null" />
 5  </configSections>
 6  <dataConfiguration defaultDatabase="Connection String" />
 7  <connectionStrings>
 8    <add name="Connection String" connectionString="Database=LocomotiveStat;Server=10.167.61.49;User ID=sa;Password=sa;"
 9      providerName="System.Data.SqlClient" />
10  </connectionStrings>
11</configuration>

输入命令:aspnet_regiis -pef "你要加密的【配置节】" "你要加密的【目录】",加密后的config文件内容如下:

1<?xml version="1.0" encoding="utf-8"?>
 2<configuration>
 3  <configSections>
 4    <section name="dataConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Data.Configuration.DatabaseSettings, Microsoft.Practices.EnterpriseLibrary.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=null" />
 5  </configSections>
 6  <dataConfiguration defaultDatabase="Connection String" />
 7  <connectionStrings configProtectionProvider="RsaProtectedConfigurationProvider">
 8    <EncryptedData Type="http://www.w3.org/2001/04/xmlenc#Element"
 9      xmlns="http://www.w3.org/2001/04/xmlenc#">
10      <EncryptionMethod Algorithm="http://www.w3.org/2001/04/xmlenc#tripledes-cbc" />
11      <KeyInfo xmlns="http://www.w3.org/2000/09/xmldsig#">
12        <EncryptedKey xmlns="http://www.w3.org/2001/04/xmlenc#">
13          <EncryptionMethod Algorithm="http://www.w3.org/2001/04/xmlenc#rsa-1_5" />
14          <KeyInfo xmlns="http://www.w3.org/2000/09/xmldsig#">
15            <KeyName>Rsa Key</KeyName>
16          </KeyInfo>
17          <CipherData>
18            
<CipherValue>g2QFQqbHU1L6WUPYqjADqFAvHcdq/7dqCd1U9GlQFEi/nHDVHjqsWvjNywOZtQQg7Q/yW7g8xlRCo0h2+yYd/tQTNoVMu/RKdJmSjZMnmnwpWq+S2VEWK4U106JQwLCfBR/bAF4DHvG47B9KB0JbRfXBt5V2wJVaAI9u3kzuj50=</CipherValue>
19          </CipherData>
20        </EncryptedKey>
21      </KeyInfo>
22      <CipherData>
23        
<CipherValue>blwV/ZW1izFZL80YL5RkcjrIjWkQ0L1gJhgZbxEzzTgOcT24ihrAnv3/rDCG+WIZ7TL5D/rMm7dQwkIsij1Sh3befg6F3+pxcW4oe1w/bovIKuzjs3tokUpBvTTj+fsCs2W/MWUhQaWMKQWkHfS2Ajt6gL6MTYtb3pfQUp0pdHbeRxoqdiAksQ1Zzsi1FtRTi7gTT7hnpF0pJs+W9mxTVDMO/qSZXfXLOEMIs/A5ExcfvR5GjpaPuDeLuSsCN3XtjaiXzaDQ3It7j+r66+L2C0xvEhbT9SsG</CipherValue>
24      </CipherData>
25    </EncryptedData>
26  </connectionStrings>
27</configuration>

  自己也随便找了个配置文件式下了,成功了。

 

 

由此可见,我们已经完成了任务,现在只需要将App.config文件名改回Web.config即可,在应用程序项目中无需对该文件进行解密操作,.NET框架会自动替我们完成,如果想解密该文件也很简单,在SDK命令提示里输入aspnet_regiis -pdf "配置节" "目录"即可。

  参考网址如:https://blog.csdn.net/dqs78833488/article/details/51115537

  希望本篇文章对大家有一定的帮助,以上有不足之处,或有其他更合理的方法请留言赐教。

  A young ilder ~ An old baggar !

posted @ 2018-06-09 18:25  追梦中  阅读(400)  评论(0编辑  收藏  举报