Click here to Skip to main content
15,892,059 members
Articles / Web Development / IIS

DPAPI and Triple DES: A powerful combination to secure connection strings and other application settings

Rate me:
Please Sign up or sign in to vote.
4.77/5 (47 votes)
26 Aug 20056 min read 133.7K   1.7K   85  
This article shows how DPAPI and Triple DES can be used to encrypt connection strings and other sensitive strings for storage in the ASP.NET web.config file.
using System;
using System.Collections.Specialized;
using System.Xml;
using Microsoft.Win32;

namespace Foulds.Security.SectionHandlers.DataProtection
{
	/// <author>Hannes Foulds, 18 August 2005</author>
	/// <summary>
	/// This class is used to read the encrypted section from the config file.
	/// </summary>
	public class EncryptedSettings : NameValueCollection
	{
		#region Declarations
		/// <summary>The class that will be used for the encryption and decryotion of data.</summary>
		protected EncryptionEngine encryptionEngine;

		private string _registryKey;		// the registry key containing the descryption key
		#endregion

		#region Properties
		/// <summary>
		/// The registry key containing the descryption key.
		/// </summary>
		public string RegistryKey
		{
			get 
			{ 
				return this._registryKey; 
			}
			
			set
			{ 
				this._registryKey = value; 
				this.LoadDecryptionKey();
			}
		}
		#endregion

		#region Constructor
		/// <summary>
		/// Initialize a default instance of the class.
		/// </summary>
		public EncryptedSettings()
		{
			this.Initialize();
		}

		/// <summary>
		/// Initialize a new instance of the class and load the data from the XML node provided.
		/// </summary>
		/// <param name="dataNode">The XML Node from which data should be loaded for the class.</param>
		public EncryptedSettings(XmlNode dataNode)
		{
			this.Load(dataNode);
		}
		#endregion

		#region Initialize
		/// <summary>
		/// Initialize the object.
		/// </summary>
		private void Initialize()
		{
			this.encryptionEngine = new EncryptionEngine();
			this.RegistryKey = string.Empty;
			this.Clear();
		}
		#endregion

		#region Load
		/// <summary>
		/// Load the data for the class from the XML node provided.
		/// </summary>
		/// <param name="dataNode">The XML Node from which data should be loaded.</param>
		public void Load(XmlNode dataNode)
		{
			try
			{
				// initialize the object
				this.Initialize();

				// load the registry key
				this.RegistryKey = dataNode.Attributes["registryKey"].Value;

				// load the value nodes
				XmlNodeList valueNodes = dataNode.SelectNodes("add");
				foreach(XmlNode valueNode in valueNodes)
				{
					this.LoadValueNode(valueNode);
				}
			}
			catch (Exception ex)
			{
				string message = string.Concat(ex.Message, System.Environment.NewLine, ex.StackTrace);
				Console.WriteLine(message);
				throw;
			}
		}
		#endregion

		#region Load Value Node
		/// <summary>
		/// Load a value node for the object.
		/// </summary>
		/// <param name="valueNode"></param>
		private void LoadValueNode(XmlNode valueNode)
		{
			string key = valueNode.Attributes["key"].Value;
			string value = valueNode.Attributes["value"].Value;

			this.Add(key, this.DecryptValue(value));
		}
		#endregion

		#region Load Decryption Key
		/// <summary>
		/// Load the decryption key from the registry.
		/// </summary>
		private void LoadDecryptionKey()
		{
			if (this.RegistryKey.Length > 0)
			{
				this.encryptionEngine.RegistryKey = this.RegistryKey;
				this.encryptionEngine.LoadMasterKey();
			}
		}
		#endregion

		#region Decrypt Value
		/// <summary>
		/// Decrypt the value of a key value pair for storing in the collection.
		/// </summary>
		/// <param name="cipherText">The cipher text of the value to decrypt.</param>
		/// <returns>Returns the decrypted value.</returns>
		private string DecryptValue(string cipherText)
		{
			this.encryptionEngine.CipherText = cipherText;
			this.encryptionEngine.DecryptString();
			return this.encryptionEngine.PlainText;
		}
		#endregion
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
South Africa South Africa

Comments and Discussions