Update:- 1. The virsion of the project is upgraded nwo it is in 4.6.1
Introduction
This tip is about encryption of the connection string from app.config file. I searched a lot on the net but was not able to find proper step by step information, which gives us proper instruction on how we can develop this functionality. Then I found a good link on Microsoft forums where I found how we can encrypt the connection string. Then I found it is very easy and can be implemented within no time. This application will generate the Encrypted connection string from the config file. We just need to copy that section and put in our app.config.
Background
If we want to encrypt the Connection with a Windows Application Service while deploying the application to the customer we can use this code.
Using the Code
This project is very simple. I have used Microsoft’s ConfigSection.SectionInformation.ProtectSection("DataProtectionConfigurationProvider")
using this command it will encrypt the config file. What my program does is that it will look for .NET executable file. When you provide the executable file, you will have two options:
- Encrypt File
- Decrypt File
The below code is very easy to understand. The EncryptConnectionString
takes two parameters. We are passing the .NET executable file name which we need to execute. The code will convert the ConnectionString
Section in the encrypted format and open the file in Notepad.
public static void EncryptConnectionString(bool encrypt,string fileName)
{
Configuration configuration = null;
try
{
configuration = ConfigurationManager.OpenExeConfiguration(fileName);
ConnectionStringsSection configSection =
configuration.GetSection("connectionStrings") as ConnectionStringsSection;
if ((!(configSection.ElementInformation.IsLocked)) &&
(!(configSection.SectionInformation.IsLocked)))
{
if (encrypt && !configSection.SectionInformation.IsProtected)
{
configSection.SectionInformation.ProtectSection
("DataProtectionConfigurationProvider");
}
if (!encrypt &&
configSection.SectionInformation.IsProtected)
{
configSection.SectionInformation.UnprotectSection();
}
configSection.SectionInformation.ForceSave = true;
configuration.Save();
Process.Start("notepad.exe", configuration.FilePath);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
If you set the config file entry like below:
Section 1
<connectionStrings>
<add name="SecurePassDataBase" connectionString="Data Source=D-6058;
Initial Catalog=DEMO_Test;User ID=sysdba;Password=xxxxxx" />
</connectionStrings>
You will get the encrypted connection string like below:
Section 2
<connectionStrings configProtectionProvider="DataProtectionConfigurationProvider">
<EncryptedData>
<CipherData>
<CipherValue>AQAAANCMnd8BFdERjHoAwE/Cl+sBAAAATeylFe/
xsUiVdcZvovEYDwQAAAACAAAAAAADZgAAwAAAABAAAABZsoaKP62hL85wpS+O3+
znAAAAAASAAACgAAAAEAAAAHZ5NcKcDcWuEVDKyU4mz7J4AQAAAILD3fmIimyY2rkEkAdAtRn0dh9tI7+
Y5+ILciikoSd/y2myUS88vJ59pIf82vOLk/0UwKL8TnHEaFTeX7SJ5par6pW7Pyhu4kKTEMyMUQsZX/
h8RjNOnt+Q/kZIdqF2YWxFUP0RF3GWirvMNWS3do7IE0WaJ1W3wL+HhalglmKURWIGHsvJlybl+
EGI8crPnli0W/yMN+fR0P/ndaTY87kR4+0gvKDWzZ/dMh8E7ZtodFzTQ4pjpl5YyRHH/
Tc3oFUtimCnzXvCVT4ykK6NEQfPiPc5KJW6ajTEEGOrAXTnr9HF2wCRekE3WUVPYkeHRTjtuf
2hUyvYx4eoGeOIAzFFXxY1GzZqhl8YaHlukZagiTVbfXA6Wh+K0dsAiOPz+wbCT92/
blgsdkoKSMy8vRqFxAhX8HoW6KbJhsBPOvv36iBr1RecCpzUxWrVssS+wi/JclVfVs0nYb/
pFidcJwhuwBsS6IzvV1tgrk8F9CUor+6DYHd/ABQAAABZjFi30hPRmKj+pvxFzjeNH+
Dhhg==</CipherValue>
</CipherData>
</EncryptedData>
</connectionStrings>
Just replace Section 1 with Section 2 in your application config file.
You can easily access the connection in your program like below:
string connectionString = ConfigurationManager.ConnectionStrings["SecurePassDataBase"].ToString();
No need to change or do anything else to be able to access the connection string. Hope this code will help you to generate the encryption in your WinForm application.
Acknowledgement
I would like to thank Shreyas Ramdahve who reviews my articles and makes them perfect.