This procedure develops a simple ASP.NET Web application that will retrieve the encrypted connection string from the registry and decrypt it.
To create an ASP.NET application
Create a new Visual C# ASP.NET Web Application called EncryptionWebApp.
Add an assembly reference to the Encryption.dll assembly.
To create this assembly, you must perform the steps described in “How To: Create an Encryption Library” in the Reference section of this book.
Open Webform1.aspx.cs and add the following using statements at the top of the file beneath the existing using statements.
using Encryption; using System.Text; using Microsoft.Win32;
Add the controls listed in Table 2 to WebForm1.aspx.
|
Control |
Text |
ID |
|---|---|---|
|
Label |
lblEncryptedString |
|
|
Label |
lblDecryptedString |
|
|
Button |
Get Connection String |
btnGetConnectionString |
Double-click the Get Connection String button to create a button click event handler.
Add the following code to the event handler.
RegistryKey rk = Registry.LocalMachine.OpenSubKey(
@"Software\TestApplication",false);
lblEncryptedString.Text = (string)rk.GetValue("connectionString");
string initVector = (string)rk.GetValue("initVector");
string strKey = (string)rk.GetValue("key");
Decryptor dec = new Decryptor(EncryptionAlgorithm.TripleDes );
dec.IV = Convert.FromBase64String(initVector);
// Decrypt the string
byte[] plainText = dec.Decrypt(Convert.FromBase64String(
lblEncryptedString.Text),
Convert.FromBase64String(strKey));
lblDecryptedString.Text = Encoding.ASCII.GetString(plainText);
On the Build menu, click Build Solution.
Right-click Webform1.aspx in Solution Explorer, and then click View in Browser.
Click Get Connection String.
The encrypted and decrypted connection strings are displayed on the Web form.