虽然不怎么新鲜,但相信还是有许多人不知道,好,不说废话,直接给方法:开始--->运行,输入cmd,接着输入以下内容
加密:
C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\aspnet_regiis.exe -pef "connectionStrings" "你的Web项目路径"
解密:
C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\aspnet_regiis.exe -pdf "connectionStrings" "你的Web项目路径"
.NET为版本的路径自行修改,其中connectionStrings连接字符串的名称。
需要注意的是,加密过程中使用了一个基于本机的密钥,这意味着解密过程必须在同一台计算机上完成。如果是将加密后的Web.config文件移动到其它计算机上,那么Web.config文件中的连接字符串将不能够正常解密。
附加密前后对比:
1.加密前
1
2
3
4
5
6
|
< configuration > < connectionStrings > < add name = "ConnectionName" connectionString = "Server=127.0.0.1;Database=TestDB;User ID=sa;Password=ok" providerName = "System.Data.SqlClient" /> </ connectionStrings > </ configuration > |
2.加密后
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
< configuration > < 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 >fDfW3bnVt21RF3N39vDoPphEmDYbUX4cmciD/3+LMY0yRLHckyulnnyBLoflB7DUjyXXms0V33e7MOKt+u2TAocn6x+QHo9Z4Onf1fV0nEq6uTprWZ04M8SLbKp+Vg63JLtYQUft6xF+Bi/aN/ZJ3PYal93bdfNJjtXA2xsb82k=</ CipherValue > </ CipherData > </ EncryptedKey > </ KeyInfo > < CipherData > < CipherValue >92WEStBHBh0zeu705wseRUajhAHumV9uCrmFJWII8SrhQpjEDrSl0OAfhwYFENr4xpHSfkNDTEFVV5D4MXr3meMsCcp+oYEQxQ/mg1QYLe9mGD+NEaBnv95WzaDcdDyE1SkNKkq01pX94OUV1OygsQtEx1fCZd6le8fd7kx4PAFKDD0he6ajzNFmCoFxg1Dd1+MD3mukgFef64NbjYovTNW8v2G67wLE8vnrokxIvs6+0+rnpLepDAyiEDaL2D3jJWNcQrl+UXI=</ CipherValue > </ CipherData > </ EncryptedData > </ connectionStrings > </ configuration > |