Team LiB
Previous Section Next Section

2. Create an ASP.NET Web Application

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

  1. Create a new Visual C# ASP.NET Web Application called EncryptionWebApp.

  2. 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.

  3. 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;
    
    
  4. Add the controls listed in Table 2 to WebForm1.aspx.

    Table 2: WebForm1.aspx controls

    Control

    Text

    ID

    Label

     

    lblEncryptedString

    Label

     

    lblDecryptedString

    Button

    Get Connection String

    btnGetConnectionString

  5. Double-click the Get Connection String button to create a button click event handler.

  6. 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);
    
  7. On the Build menu, click Build Solution.

  8. Right-click Webform1.aspx in Solution Explorer, and then click View in Browser.

  9. Click Get Connection String.

    The encrypted and decrypted connection strings are displayed on the Web form.


Team LiB
Previous Section Next Section