习惯了将数据库连接字段直接写在web.config中,这样虽然方便,但是明文的数据库用户名和密码让人看着还是很反感!
想了许多方法 ,最后还是用了最方便、快捷的----aspnet_regiis.exe 来实现。
首先,打开VS2008的命令提示工具,输入:
C:\Windows\Microsoft.NET\Framework\v2.0.50727>aspnet_regiis.exe -pef "connectionStrings"D:\PersonalProjects\JQueryAndSql
(注意,只要输入到路径,不需要输入 web.config)
现在来看看加密后的connectionStrings节点:
<connectionStrings configProtectionProvider="RsaProtectedConfigurationProvider">
<EncryptedData Type="http://www.w3.org/2001/04/xmlenc#Element"
xmlns="http://www.w3.org/2001/04/xmlenc#">
<EncryptionMethod Algorithm="http://www.w3.org/2001/04/xmlenc#tripledes-cbc" />
<KeyInfo xmlns="http://www.w3.org/2000/09/xmldsig#">
<EncryptedKey xmlns="http://www.w3.org/2001/04/xmlenc#">
<EncryptionMethod Algorithm="http://www.w3.org/2001/04/xmlenc#rsa-1_5" />
<KeyInfo xmlns="http://www.w3.org/2000/09/xmldsig#">
<KeyName>Rsa Key</KeyName>
</KeyInfo>
<CipherData>
<CipherValue>d/+HLVqH82suUhGft/bL7t5Khzh0zD2pJWz7AYyUXKs4CpSrpMwEYlqpg1NI0phsT+CBan+w+zTTyT257eJL5XQbyeI2MpsGZLckR7ewXKgiF1WTwBC4O071N2C4OhH8GZrjJ2/rF43YCSr2o5b7H0Vj4exymDM2aaaUqI2A2lk=</CipherValue>
</CipherData>
</EncryptedKey>
</KeyInfo>
<CipherData>
<CipherValue>Q3Ew0n71wqEcu13RzArEDZCAwVug+84KAuzk0OU3PfkGn1c6duLn6m+IfmKrpqjZLl1PQnfOCShBF1sRbFUeTJ96stO/OPgBm5EfN8KF86h8X/OeAmVF5v1xlmzJjpiSd5qHnleLxpS2f7MruZImZPX4x9rbzb5FeHvWW+cgcVg=</CipherValue>
</CipherData>
</EncryptedData>
</connectionStrings>
当然也可以解密的,命令如下:
C:\Windows\Microsoft.NET\Framework\v2.0.50727>aspnet_regiis.exe -pdf "connectionStrings"D:\PersonalProjects\JQueryAndSql
看看解密后的代码:
<connectionStrings>
<add name="SQLCONN" connectionString="database=pubs;uid=sa;pwd=sa" />
</connectionStrings>
對weg.config中的敏感配置信息进行加密
最新推荐文章于 2025-06-23 10:51:00 发布