In this article I will provide a tutorial with example to encrypt and decrypt Connection String in App.Config file used in Windows and Console Applications.
The Encryption and Decryption of the App.Config file’s ConnectionStrings section will be performed using aspnet_regiis.exe Command Line Utility of the Visual Studio.
 
The Plain Connection String in the App.Config file
The below screenshot shows the Plain Connection String in the App.Config file before encryption.
Encrypt and Decrypt Connection String in App.Config file in Windows and Console applications
 
 
Encrypting the Connection Strings in App.Config file
You need to follow the following steps for encrypting the Connection Strings in the App.Config file.
1. Renaming App.Config file to Web.Config
The aspnet_regiis.exe Command Line Utility of the Visual Studio does not recognize for App.Config files and hence we need to temporarily rename the App.Config file to Web.Config.
 
2. Open Visual Studio Command Prompt
You will need to open the Visual Studio Command Prompt from the Start Menu => Programs => Microsoft Visual Studio 2010 => Visual Studio Tools => Visual Studio Command Prompt.
Note: You must be log in as Administrator and right click Visual Studio Command Prompt and select Run as Administrator.
 
Note: In this tutorial, I am explaining the process using Microsoft Visual Studio 2010. The same process will be applicable for the other versions. The only difference will be that you need to open Visual Studio Command Prompt from the folder of the respective version of Visual Studio installed on your machine.
 
Encrypt and Decrypt Connection String in App.Config file in Windows and Console applications
 
3. Encrypting the Connection String in App.Config using aspnet_regiis.exe tool
In order to encrypt the ConnectionString section in the App.Config file, we will need to use the aspnet_regiis.exe tool.
Parameters
Action – It notifies the action to be performed. In order to perform Encryption, the parameter value is -pef.
Section Name – The name of the section of the App.Config file to be encrypted. For this case, the value will be connectionStrings.
Path of the folder – Here we need to specify the path of the folder containing the App.Config file.
Syntax
aspnet_regiis.exe -pef "connectionStrings" "<Path of the Folder containing the App.Config file>"
Example
aspnet_regiis.exe -pef "connectionStrings" "D:\Mudassar\Projects\MyTestApp"
 
Note: The above command will encrypt all the Connection Strings present in the ConnectionStrings section of the App.Config file.

Encrypt and Decrypt Connection String in App.Config file in Windows and Console applications
 
4. Renaming Web.Config file to App.Config
Once the encryption is successful, you can now rename the Web.Config file to App.Config so that it can be used in the Windows or Console applications.
 
 
Encrypted Connection String in the App.Config file
The following screenshot shows the Encrypted Connection String in the App.Config file after encryption.
Encrypt and Decrypt Connection String in App.Config file in Windows and Console applications
 
 
Accessing the Encrypted Connection String value in Code
The ConfigurationManager class will automatically decrypt the Connection String when it is fetched in the code behind and hence in code behind you need to access the Connection String in the same way as you would do normally.
Encrypt and Decrypt Connection String in App.Config file in Windows and Console applications
 
Decrypting the Connection String in App.Config using aspnet_regiis.exe tool
In order to decrypt the ConnectionString section in the App.Config file, we will need to use the same aspnet_regiis.exe tool that was used for encryption.
Note: You will have to again rename the App.Config file to Web.Config and once the decryption is successful change its name it back to App.Config.
Parameters
Action – It notifies the action to be performed. In order to perform Decryption, the parameter value is -pdf.
Section Name – The name of the section of the App.Config file to be decrypted. For this case the value will be connectionStrings.
Path of the folder – Here we need to specify the path of the folder containing the App.Config file.
Syntax
aspnet_regiis.exe -pdf "connectionStrings" "<Path of the Folder containing the App.Config file>"
Example
aspnet_regiis.exe -pdf "connectionStrings" "D:\Mudassar\Projects\MyTestApp"
 
Note: The above command will decrypt all the Connection Strings present in the ConnectionStrings section of the App.Config file.
 
Encrypt and Decrypt Connection String in App.Config file in Windows and Console applications