The registry represents one possible location for an application to store database connection strings. Although individual registry keys can be secured with Windows access control lists (ACLs), for added security you should store encrypted connection strings.
This How To describes how to store an encrypted database connection string in the registry and retrieve it from an ASP.NET Web application It uses the generic encryption and decryption managed class library created in How to:. Create an Encryption Library, which can be found IN Reference Section of this Guide.
IF you have not already create, do so reference library assembly
For More Information About Other Locations and Ways of Securely Storing Database Connection Strings, See Storing Database Connection Strings Securely in Chapter 12, "Data Access Security."
Notes
THE CONNECTION STRING, INTILALIZATION vector and key used for Encryption Will Be stored in the registryption
The Initialization Vector and Key Must Be Stored In Order To Allow The Connection String To Be Decrypted.
Requirements
The Following Items Describe The Recommended Hardware, Software, Network Infrastructure, Skills and Knowledge, And Service Packs you will need.
Microsoft? Windows? 2000 Operating System Microsoft Visual Studio? .Net Development System
The Procedures in this article.
Summary
This How To Includes The Following Procedures:
Store The Encrypted Data in The Registry Create An ASP.NET Web Application
1. Store The Encrypted Data in The Registry
THIS Procedure Creates A Windows Application That Will BE Used to Encrypt A Sample Database String and Store It in The Registry.to Store The Encrypted Data in The Registry
. Start Visual Studio .NET and create a new C # Windows project called EncryptionTestApp 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 Guide. add the the the existing limited;
Using system.text;
USING Microsoft.win32;
Add The Controls in Table 1 To Form1 And Arrange THEM AS ILLUSTRATED IN FIGURE 1. TABLE 1. EncryptionTestApp Controls
ControlTextIDLabelConnection String: TextBox txtConnectionStringLabelKey: TextBox txtKeyLabelInitialization Vector: TextBox txtInitializationVectorLabelEncrypted String TextBox txtEncryptedStringLabelDecrypted String TextBox txtDecryptedStringButtonEncryptbtnEncryptButtonDecryptbtnDecryptButtonWrite Registry DatabtnWriteRegistryData Figure 1. Encryption Test Harness dialog box Set the Text property of txtConnectionString to "Server = local; database = pubs; uid = Bob; pwd = PASSWORD "
SET THE TEXT Property of TxtKey to "0123456789012345"
THE Key Length IS 16 BYTES To Suite The Triple Des Encryption Algorithm. Set The Text Property Of Form1 To "Encryption Test Harnes"
Double-Click The Encrypt Button to Create A Button Click Event Handler And Add The Following Code to the Event Handler. Try
{
// Create the Encryptor Object, Specifying 3des as the
// encryption algorithm
Encryptor Enc = New Encryptor (EncryptionAlithm.tripledes);
// Get the connection string as a byte arraybete [] plaintext =
Encoding.ascii.getbytes (txtconnectionstring.text);
Byte [] Key = Encoding.ascii.getbytes (txtKey.Text);
// perform the encryption
Byte [] ciphertext = enc.encrypt (plaintext, key);
// store the int attriblation vector, as this will be required, AS THIS WILL BE Required
// for Decryption
TxtinitializationVector.text = encoding.ascii.getstring (enc.iv);
// Display the Encrypted String
TXTENCRYPTEDSTRING.TEXT = Convert.TOBASE64String (Ciphertext);
}
Catch (Exception EX)
{
Messagebox.show ("Exception Encrypting:" EX.MESSAGE,
"Encryption Test Harness";
}
Return to Form1 in Designer Mode and Double-Click The Decrypt Button to Create a Button Click Event Handler. Add The Following Code to The Decrypt Button Event Handler. Try
{
// set up the decryptor object Object
Decryptor Dec = New Decryptor (EncryptionAliithm.tripledes);
// set the initialization vector
Dec.iv = encoding.ascii.getbytes (txtInitializationVector.text);
Byte [] Key = Encoding.ascii.getbytes (txtKey.Text);
// perform the decryption
Byte [] plaintext = DEC.DECRYPT (Convert.FromBase64String)
TxtencryptedString.Text),
Key);
// Display the decrypted string.
TXTDECRYPTEDSTRING.TEXT = Encoding.ascii.getstring (plaintext);
}
Catch (Exception EX)
{
Messagebox.show ("Exception Decrypting." EX.MESSAGE,
"Encryption Test Harness";
}
Return to Form1 in Designer Mode and Double-Click The Write Registry Data Button to Create A Button Click Event Handler. Add The Following Code To The Event Handler. // Create Registry Key and Named Values
RegistryKey rk = registry.localmachine.opensubKey ("Software", true);
RK = rk.createsubkey ("testapplication"); // Write Encrypted string, Initialization Vector and key to the
Registry
RK.SetValue ("Connectionstring", txtencryptedstring.text);
RK.SetValue ("InitVector", Convert.TOBASE64STRING
Encoding.ascii.getbytes (txtInitializationVector.text))))
RK.SetValue ("Key", Convert.TOBASE64String (Encoding.ascii.GetBytes
TXTKEY.TEXT))))))))))))))))))))))))))))))))))))
Messagebox.show ("THE DATA HAS Been successfully written to the
Registry ");
Run the application, and then click Encrypt. The encrypted connection string is displayed in the Encrypted String field. Click Decrypt. The original string is displayed in the Decrypted String field. Click Write Registry Data. In the message box, click OK. Run regedit .exe and view the contents of the folowing key. HKLM / Software / TestApplication
Confirm That Encoded Values Are Present for the Connectionstring, InitVector and Key Named Values. Close Regedit and The Test Harnes Application.
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
. 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 guide Open Webform1.aspx.cs and add the stock of the file beneath the existing sale. Using Encryption
Using system.text;
USING Microsoft.win32;
Add the controls listed in table 2 to Webform1.aspx. Table 2: Webform1.aspx Controls
ControlTextIDLabel lblEncryptedStringLabel lblDecryptedStringButtonGet Connection StringbtnGetConnectionString 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 ("InIitVector");
String strkey = (string) RK.getValue ("key");
Decryptor Dec = New Decryptor (EncryptionAliithm.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.