Team LiB
Previous Section Next Section

8. Write a Web Application to Test the Encryption and Decryption Routines

This procedure develops a simple Web application that you will use to test the encryption and decryption routines. Later, you will also use it to decrypt encrypted data maintained within the Web.config file.

To write a Web application to test the encryption and decryption routines

  1. Add a new C# Web application project called DPAPIWeb to the existing DPAPIComp solution.

  2. Add an assembly reference to System.EnterpriseServices and add a project reference to the DPAPIComp project.

  3. Open WebForm1.aspx in Design mode and create a form similar to the one shown in Figure 2. Use the IDs listed in Table 1 for the individual controls.

    Table 1: WebForm1.aspx control IDs

    Control

    ID

    Data To Encrypt Text Box

    txtDataToEncrypt

    Encrypted Data

    txtEncryptedData

    Decrypted Data

    txtDecryptedData

    Encrypt Button

    btnEncrypt

    Decrypt Button

    btnDecrypt

    Error Label

    lblError

    Click To expand
    Figure 2: DPAPIWeb Web Form

  4. Double-click the Encrypt button to display the button click event handler.

  5. Add the following using statements to the top of the file beneath the existing using statements.

    using System.Text;
    using DPAPIComp;
    
    
  6. Return to the Encrypt button click event handler and add the following code to call the DataProtectorComp serviced component to encrypt the data entered via the Web form.

    DataProtectorComp dp = new DataProtectorComp();
    try
    {          
      byte[] dataToEncrypt = Encoding.ASCII.GetBytes(txtDataToEncrypt.Text);
      txtEncryptedData.Text = Convert.ToBase64String(
                                        dp.Encrypt(dataToEncrypt));
    }
    catch(Exception ex)
    {
      lblError.ForeColor = Color.Red;
      lblError.Text = "Exception.<br>" + ex.Message;
      return;
    }
    lblError.Text = "";
    
  7. Display the Web form again and double-click the Decrypt button to create a button click event handler.

  8. Add the following code to call the DataProtectorComp services component to decrypt the previous encrypted data contained within the txtEncryptedData field.

    DataProtectorComp dp = new DataProtectorComp();
    try
    {          
      byte[] dataToDecrypt = Convert.FromBase64String(txtEncryptedData.Text);
      txtDecryptedData.Text = Encoding.ASCII.GetString(
                                      dp.Decrypt(dataToDecrypt));
    }
    catch(Exception ex)
    {
      lblError.ForeColor = Color.Red;
      lblError.Text = "Exception.<br>" + ex.Message;
      return;
    }
    lblError.Text = "";
    
  9. On the Build menu, click Build Solution.

  10. Right-click WebForm1.aspx, and then click View in Browser.

  11. Enter a text string into the Data to Encrypt field.

  12. Click the Encrypt button. This results in a call to the DataProtector serviced component within the COM+ application. The encrypted data should be displayed in the Encrypted Data field.

  13. Click the Decrypt button and confirm that the original text string is displayed in the Decrypted Data field.

  14. Close the browser window.

    Note 

    If an access denied error message appears that indicates that the component’s ProgID cannot be read from HKEY_CLASSES_ROOT, you probably need to re-run Regsvcs.exe to reregister the serviced component.

    This error message appears if you have recompiled the serviced component assembly but not reregistered the assembly. Because the assembly version changes on each build (due to the default “1.0.*” assembly version attribute), a new CLSID is generated on each successive build. The error is due to the fact that ASP.NET cannot access this CLSID in the registry as it doesn’t exist yet. Rerun Regsvcs.exe and restart the Web application to resolve the problem.


Team LiB
Previous Section Next Section