|
|||||||||||||||||||||
|
|||||||||||||||||||||
|
Announcements
Want a new Job?
Chapters
Services
Feature Zones
|
IntroductionIt all happened when I came across a need to encrypt sensitive information in my Windows Forms application’s configuration file. I decided to use the new Protected Configuration feature of Microsoft .NET 2.0 for encrypting it. I started searching the web but was unable to find any practical application of Protected Configuration for Windows Forms applications. Almost all of the online resources tell you to do that with ASP.NET. In most of the articles, encryption is done via the aspnet_regiis tool, and aspnet_regiis does not support encryption of Windows Forms application configuration files, so virtually one is left with no option. Maybe I am not completely correct in saying this; few resources on the web do mention that you can use the When it comes to a real life implementation of the
As you may have realized by now, the Protected Configuration is not a fancy option with Windows applications. As the
There is a very simple solution for all the problems mentioned above. Use the BackgroundFor those of you who are not familiar with the new Protected Configuration feature of .NET 2.0, it enables you to encrypt application configuration information and configure the application to automatically decrypt it at runtime. This is implemented by using different Protected Configuration Providers, which can be configured in App.config files. Each provider has its own method of performing the encryption. For details, take a look at the MSDN article: Encrypting Configuration Information Using Protected Configuration. Using the CodeAmong the two built-in Protected Configuration Providers, i.e., Note: The purpose of this article is not to demonstrate the best possible encryption mechanism, but to demonstrate how Protected Configuration can be used with Windows Forms apps. Feel free to implement your own provider, which may use a custom algorithm. See Implementing a Protected Configuration Provider for more details. Recreating the Demo ProjectTo recreate the demo project, follow the steps mentioned below. Creating the Windows Application ProjectOpen Visual Studio 2005, and create a new C# Windows Application project. Name it DemoWinApp.
Open the Settings file, and add a setting named
Open
Add the following code in lblSecretMessage.Text = DemoWinApp.Properties.Settings.Default.SecretMessage;
Specifying the Protected Configuration ProviderOpen the App.Config file, and add the following code as the child node of the <configProtectedData>
<providers>
<add useMachineProtection="true" name="DPAPIProtection"
type="System.Configuration.DpapiProtectedConfigurationProvider,
System.Configuration, Version=2.0.0.0, Culture=neutral,
PublicKeyToken=b03f5f7f11d50a3a" />
</providers>
</configProtectedData>
The above code is necessary to configure a Protected Configuration provider, which will be used for encrypting and decrypting the configuration section which contains sensitive information. Whenever you want to specify your own provider with custom settings, you can declare a new provider instance using the The Notice the attribute For this article, we shall use the machine specific protection. The attribute Other attributes like Here is how the app.config file looks like: <?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<sectionGroup name="applicationSettings"
type="System.Configuration.ApplicationSettingsGroup,
System, Version=2.0.0.0, Culture=neutral,
PublicKeyToken=b77a5c561934e089" >
<section name="DemoWinApp.Properties.Settings"
type="System.Configuration.ClientSettingsSection,
System, Version=2.0.0.0, Culture=neutral,
PublicKeyToken=b77a5c561934e089"
requirePermission="false" />
</sectionGroup>
</configSections>
<applicationSettings>
<DemoWinApp.Properties.Settings>
<setting name="SecretMessage" serializeAs="String">
<value>This is the secret message.</value>
</setting>
</DemoWinApp.Properties.Settings>
</applicationSettings>
<configProtectedData>
<providers>
<add useMachineProtection="true" name="DPAPIProtection"
type="System.Configuration.DpapiProtectedConfigurationProvider,
System.Configuration, Version=2.0.0.0,
Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
</providers>
</configProtectedData>
</configuration>
Adding an Installer ClassAs mentioned earlier, we shall use the Add a new installer class in your project, and name it DemoWinAppInstaller.cs.
For those of you who are not familiar with using installers, installers are components that help install applications on a computer. A custom class is derived from the base Here is the code listing of the custom installer class: using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration.Install;
using System.Configuration;
namespace DemoWinApp
{
[RunInstaller(true)]
public partial class DemoWinAppInstaller : Installer
{
public DemoWinAppInstaller()
{
InitializeComponent();
}
public override void Install(System.Collections.IDictionary stateSaver)
{
base.Install(stateSaver);
//get Configuration section
//name from custom action parameter
string sectionName = this.Context.Parameters["sectionName"];
//get Protected Configuration Provider
//name from custom action parameter
string provName = this.Context.Parameters["provName"];
// get the exe path from the default context parameters
string exeFilePath = this.Context.Parameters["assemblypath"];
//encrypt the configuration section
ProtectSection(sectionName, provName, exeFilePath);
}
private void ProtectSection(string sectionName,
string provName, string exeFilePath)
{
Configuration config =
ConfigurationManager.OpenExeConfiguration(exeFilePath);
ConfigurationSection section = config.GetSection(sectionName);
if (!section.SectionInformation.IsProtected)
{
//Protecting the specified section with the specified provider
section.SectionInformation.ProtectSection(provName);
}
section.SectionInformation.ForceSave = true;
config.Save(ConfigurationSaveMode.Modified);
}
}
}
Notice that only the Also notice that, you don’t have to pass the The The The The DemoWinApp application is complete. Don’t forget to build the project. Creating the Visual Studio Setup ProjectNow, let's see how a Visual Studio Setup project can be created, which will make use of the installer class presented above to encrypt the sensitive configuration information upon installation. Add a new setup project to the solution, and name it DemoWinAppSetup.
From the File System Editor, add the primary output of the DemoWinApp project to the setup project.
The key to using an installer class with a Visual Studio Setup project is Custom Actions. Open the Custom Action Editor, and add the primary output of the DemoWinApp project under the Install folder.
Open the Properties window of the newly added custom action, and add custom action parameters and their values in the
Notice the format of specifying the parameters. Each parameter takes the form /[Parameter Name]="[Value]". Individual parameters are delimited by spaces. Make sure there are no spaces between the parameter name, equal sign, and the parameter value. To encrypt the whole Note that adding a value in the Settings.settings file automatically creates the <applicationSettings>
<DemoWinApp.Properties.Settings>
<setting name="SecretMessage" serializeAs="String">
<value>This is the secret message.</value>
</setting>
</DemoWinApp.Properties.Settings>
</applicationSettings>
The Now we are ready to go. Build the Setup project to generate the MSI Installer package. Running the ApplicationRun the generated installer package to install the application. Go to the installation folder, and open the configuration file. Note the In my case, it looks like the following: <applicationSettings>
<DemoWinApp.Properties.Settings configProtectionProvider="DPAPIProtection">
<EncryptedData>
<CipherData>
<CipherValue>AQAAANCMnd8BFdERjHoAwE/Cl+sBAAAAJY9eXbg340Gn
Ay8Dyzkf4QQAAAACAAAAAAADZgAAqAAAABAAAAAac+j1
UbifDAtrOUt9xNUWAAAAAASAAACgAAAAEAAAAKwqPS6A
bcFdlDZiS8gBMvCIAQAAgBPMemCdMCcX6juvCaB02kNJ
Vb3tyHHLztYWkMyNgs60cIBiqQbNYPUcPkuMg42tbffk
0VQ0KEwMu8/mRAc4+LQwni9kRbpSR4GOUPoOzLSrdxcK
6D1mjntJ+804dZ7fk9gq793GpPJroV0VfxoaMBDZtHKw
uRbAnFvVn+yWfH6ZVN1gQFIM3vhinc/kpiBB+pPLCO/5
XbsOfwu3eLSw436LGqfTPsvAj6JY6pax8hd7KnIsCDte
EkZVGjHAfJnSj6mB5vI9u7fBLwjTa0V4qhznW+lcb6uP
JeR565sRnJq7Od+3c716bJ6fOS/AY91zF+f5rYLN+ebZ
ZnKabrilA6+xS70+rSPuLnXueKp6UymP0R4k9oVjfmAm
Utd4/PYuZqk+nKbEyJwr8lzc7lkwsy/aYE7IK9/BHlDf
rpJfR0B11ZeUBmXbGLD0N0hFQHZO6FHDxnRIzadJx7UX
o5VGVoE63tjAnDfRtj/UbudIq8GhM8CHxkh0o/AUuEpo
PsobGQ576EDdwo4UAAAAP2wB/QutHyUIYCG6T7n6YNbE
4gg=</CipherValue>
</CipherData>
</EncryptedData>
</DemoWinApp.Properties.Settings>
</applicationSettings>
So as you can see, the installation process has encrypted the Note that the Now, the responsibility of decrypting the configuration section is on the .NET runtime, not the application programmer. To recall, in the DemoWinApp application, we did not add any code for decrypting the lblSecretMessage.Text = DemoWinApp.Properties.Settings.Default.SecretMessage;
Now, run the DemWinApp application installed on your PC. The output should look like the following:
ConclusionThe | ||||||||||||||||||||