. The WinForm and ASP. NET configuration files are saved by default in clear text, this article uses. NET itself to encrypt the configuration file, does not contain custom encryption rules, but. NET is a feature that provides direct access to profile encryption, and configuration sections that use. NET encryption do not need to be decrypted manually when they are read. Net Decrypts itself and returns the decrypted data. The encrypted data is saved to a separate configuration node (regardless of how many subkeys are under the encrypted node, the encrypted data is in CipherValue). NET is encrypted according to the node, so if a node like appsettings is encrypted, then all the data under that node will be encrypted (separate key encryption can be implemented by its own code, not very clear.) NET itself can only encrypt the node under a certain N key) encrypted data and nodes:
< EncryptedData > < CipherData > < CipherValue > Aqaaancmnd8bfderjhoawe/cl+sbaaaabblhx[...] </ CipherValue > </ CipherData > </ EncryptedData >
Encryption method
Method one (implemented with code)
/// <summary> ///Add a health value to a appsettings node///change the value if the health already exists///re-save and refresh the node after it is added/// </summary> /// <param name= "Dict" >added set of health values</param> /// <param name= "isprotected" >whether to encrypt the appsettings node data, if all the data under the Trueappsettings node is encrypted</param> Public Static voidAddconfig (system.collections.generic.dictionary<string,string> Dict,BOOLisprotected) { if(Dict = =NULL|| Dict. Count <=0)return; System.Configuration.Configuration Configuration=System.Configuration.ConfigurationManager.OpenExeConfiguration (Configurationuserlevel.none); //loop Add or change a health value foreach(system.collections.generic.keyvaluepair<string,string> Key_valueinchdict) { if(string. IsNullOrEmpty (Key_value. Key))Continue; if(Configuration. Appsettings.settings[key_value. key]!=NULL) configuration. Appsettings.settings[key_value. Key]. Value=Key_value. Value; Elseconfiguration. APPSETTINGS.SETTINGS.ADD (Key_value. Key, Key_value. Value); } //Save configuration file Try { //Encrypting configuration Information if(isprotected &&!)configuration. AppSettings.SectionInformation.IsProtected) {configuration. AppSettings.SectionInformation.ForceSave=true; Configuration. AppSettings.SectionInformation.ProtectSection ("DataProtectionConfigurationProvider"); } configuration. Save (); } Catch(Exception) {Throw; } configurationmanager.refreshsection ("appSettings"); } }
Method Two
Using the out-of-the-box tool, ASP. NET IIS Registration tool (Aspnet_regiis.exe), but it can only target the Web. config file of ASP. The answer is, of course, negative.
Configuration options
| -pdf section webapplicationdirectory |
Decrypts the specified configuration section of a Web. config file in a specified physical (non-virtual) directory. |
| -pef section webapplicationdirectory |
Encrypts the specified configuration section of a Web. config file in a specified physical (non-virtual) directory. |
The-pdf and-PEF parameters are to encrypt the Web. config file in the specified physical directory, and we can rename the app. Config file to Web. config, which allows you to "cheat" the system and let it encrypt the specified configuration section. We just need to change the encrypted file name back to App. Config, so let's experiment:
First step: Rename the app. config in the directory to Web. config.
The second step: Open the SDK Command prompt, enter the command: ASPNET_REGIIS-PEF "configuration section" "Directory", for my project, for example, the config file before encryption content is as follows:
<?XML version= "1.0" encoding= "Utf-8"?> <Configuration> <configsections> < Sectionname= "Dataconfiguration"type= "Microsoft.Practices.EnterpriseLibrary.Data.Configuration.DatabaseSettings, Microsoft.Practices.EnterpriseLibrary.Data, version=2.0.0.0, Culture=neutral, Publickeytoken=null " /> </configsections> <dataconfigurationDefaultDatabase= "Connection String" /> <connectionStrings> <Addname= "Connection String"connectionString= "DATABASE=LOCOMOTIVESTAT; server=10.167.61.49; User Id=sa; Password=sa; "ProviderName= "System.Data.SqlClient" /> </connectionStrings></Configuration>
Input command: ASPNET_REGIIS-PEF "connectionStrings" "E:\ Development Directory", the content of the encrypted config file is as follows:
<?XML version= "1.0" encoding= "Utf-8"?><Configuration> <configsections> < Sectionname= "Dataconfiguration"type= "Microsoft.Practices.EnterpriseLibrary.Data.Configuration.DatabaseSettings, Microsoft.Practices.EnterpriseLibrary.Data, version=2.0.0.0, Culture=neutral, Publickeytoken=null " /> </configsections> <dataconfigurationDefaultDatabase= "Connection String" /> <connectionStringsconfigProtectionProvider= "RsaProtectedConfigurationProvider"> <EncryptedDataType= "Http://www.w3.org/2001/04/xmlenc#Element"xmlns= "http://www.w3.org/2001/04/xmlenc#"> <EncryptionMethodalgorithm= "HTTP://WWW.W3.ORG/2001/04/XMLENC#TRIPLEDES-CBC" /> <KeyInfoxmlns= "http://www.w3.org/2000/09/xmldsig#"> <EncryptedKeyxmlns= "http://www.w3.org/2001/04/xmlenc#"> <EncryptionMethodalgorithm= "Http://www.w3.org/2001/04/xmlenc#rsa-1_5" /> <KeyInfoxmlns= "http://www.w3.org/2000/09/xmldsig#"> <KeyName>Rsa Key</KeyName> </KeyInfo> <CipherData> <CipherValue>G2qfqqbhu1l6wupyqjadqfavhcdq/7dqcd1u9glqfei/nhdvhjqswvjnywoztqqg7q/yw7g8xlrco0h2+yyd/tqtnovmu/rkdjmsjzmnmnwpwq +s2vewk4u106jqwlcfbr/baf4dhvg47b9kb0jbrfxbt5v2wjvaai9u3kzuj50=</CipherValue> </CipherData> </EncryptedKey> </KeyInfo> <CipherData> <CipherValue>blwv/zw1izfzl80yl5rkcjrijwkq0l1gjhgzbxezztgoct24ihranv3/rdcg+wiz7tl5d/rmm7dqwkisij1sh3befg6f3+pxcw4oe1w/ bovikuzjs3tokupbvttj+fscs2w/mwuhqawmkqwkhfs2ajt6gl6mtytb3pfqup0pdhberxoqdiaksq1zzsi1ftrti7gtt7hnpf0pjs+ W9mxtvdmo/qszxfxloemis/a5excfvr5gjpapudelusscn3xtjaixzadq3it7j+r66+l2c0xvehbt9ssg</CipherValue> </CipherData> </EncryptedData> </connectionStrings></Configuration>
This shows that we have completed the task, Now just change the Web. config file name back to App. config, and the. NET Framework does not need to decrypt the file in the application project, and if you want to decrypt the file it is easy to do so, enter aspnet_regiis in the SDK command prompt- PDF "configuration section" "Directory".
C # Encrypts the appsettings node data of App. Config