You do not need to write anyCodeAnd you do not need to modify any code. You only need to use the aspnet_regiis tool to modify the configuration file.
For example, the following configuration file needs to be encrypted:
<Configuration>
<Connectionstrings>
<Add name = "sqlservices" connectionstring = "Data Source = localhost;
Integrated Security = sspi; initial catalog = northwind; "/>
</Connectionstrings>
</Configuration>
Assume that the configuration file is in the myapplication directory.
Encryption command
Aspnet_regiis-pe "connectionstrings"-app "/myapplication"
The aspnet_regiis command is in the. NET Framework directory you have installed. The default value is:
C:/Windows/Microsoft. NET/framework/v2.0 .*
Encrypted effect:
<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> 0ru0xfrexc6alfyzm + F + iwzvinqtzaaunysovpv0dlipm72d
34mj/gx7pzvhsjnqclixeyjsayse
12oauf4rlieraa/rhiqdkjqyjtrrrciqnwqt5pet5lm9q0ait20kpb2g2hn/0qb
7vkcwydbotdbwmua7fxaqjhmckavi0mc = </ciphervalue>
</Cipherdata>
</Encryptedkey>
</Keyinfo>
<Cipherdata> <ciphervalue> bpws3liouxhd0qdlfrmwdy9xwn1jphnmoskuvn3jvpw
Kmd21_hjo2betijyioaq/2j1saldjm
Jfgg85bekfvuunbmrg6czcgxhyokeahzghzdw + d
Za8qef/t7witzuiqeslgk2wluxndfg4zfsydivmxy6xqh3fvw4jochzlxg/
Zjpolicychik3i27oh/xuxtsq0vnol
Gfssm/mtgwb4tloelcrj6jm5u0dja2fvmjpdc =
</Ciphervalue>
</Cipherdata>
</Encrypteddata>
</Connectionstrings>
</Configuration>
Note: To avoid a long line, I add several carriage returns to the encrypted information here.
ASP. NET automatically decrypts the content of the Web. config file. Therefore,
You can decrypt encrypted configuration settings without any additional steps for other ASP. NET functions or to access values in the code.
If you want to modify the configuration information, you need to decrypt the file and then encrypt it. Decrypt the-Pd option using the aspnet_regiis.exe command.
The command is as follows:
Aspnet_regiis-Pd "connectionstrings"-app "/myapplication"
The example above is for the IIS Site. If your site uses Asp.net Development Server of vs2005
You need to use the-Arg parameter. Of course, this can be used by the IIS Site.
Aspnet_regiis.exe-Arg "connectionstrings" "D:/my2005codes/webtestcode/testwebsite"
Note:
-You can encrypt the specified configuration section of the web. config file in the specified physical (non-virtual) directory by running.
The corresponding decryption is
-The PDF parameter decrypts the specified configuration section of the web. config file in the specified physical (non-virtual) directory.
References:
Http://msdn.microsoft.com/library/default.asp? Url =/library/en-US/dnbda/html/bdasamppet4.asp