Exercise 3: encrypt database connection information
Through this exercise, you will learn how to encrypt database connection information.
Step 1
OpenDataex3.slnProject, the default installation path should be c: \ Program Files \ Microsoft Enterprise Library January 2006 \ labs \ CS \ data access \ exercises \ ex03 \ begin, and compile.
Step 2 encrypt the database connection string
1. The encrypted connection string in enterprise library1.1 depends on the cryptography Application Block .. Net framework2.0 has built in this function, through some classes in the configuration namespace, supports two types of encryption:
Dpapiprotectedconfigurationprovider: using Windows Data Protection API (dpapi)
Rsaprotectedconfigurationprovider: RSA Algorithm
2. Select the productmaintenance project and selectProject | add reference...Menu command, select the. NET page in the pop-up dialog box, and add the following assembly.
System. configuration. dll
3. In solution manager, selectProgram. CSFile, selectView | codeMenu command to add the following namespace.
Using system. configuration;
4. Add the following code to the method protectconfiguration.
Static void protectconfiguration ()
{
// Todo: Protect the connection strings
String provider = "rsaprotectedconfigurationprovider ";
Configuration Config = NULL;
Config = configurationmanager. openexeconfiguration (configurationuserlevel. None );
Configurationsection section = config. connectionstrings;
If (section. sectioninformation. isprotected = false )&&
(Section. elementinformation. islocked = false ))
{
// Protect (encrypt) the "connectionstrings" section.
Section. sectioninformation. protectsection (provider );
// Save the encrypted section.
Section. sectioninformation. forcesave = true;
Config. Save (configurationsavemode. Full );
}
}
Step 3 run the application
SelectDebug | start without debuggingMenu command and run the application. Note that the example is the same as the example in Exercise 2. In the project bin \ debugdirectory, open the productmaintenance.exe. config configuration file and note that the connection information has become ciphertext.
<? XML version = "1.0" encoding = "UTF-8"?>
<Configuration>
<Configsections>
<Section name = "dataconfiguration" type = "Microsoft. practices. enterpriselibrary. data. configuration. databasesettings, Microsoft. practices. enterpriselibrary. data, version = 2.0.0.0, culture = neutral, publickeytoken = NULL "/>
</Configsections>
<Dataconfiguration defaultdatabase = "quickstarts instance"/>
<Connectionstrings configprotectionprovider = "rsaprotectedconfigurationprovider">
<Encrypteddata type = "http://www.w3.org/2001/04/xmlenc#Element"
Xmlns = "http://www.w3.org/2001/04/xmlenc#">
<Encryptionmethod algorithm = "http://www.w3.org/2001/04/xmlenc#tripledes-cbc"/>
<Keyinfo xmlns = "http://www.w3.org/2000/09/xmldsig#">
<Encryptedkey xmlns = "http://www.w3.org/2001/04/xmlenc#">
<Encryptionmethod algorithm = "http://www.w3.org/2001/04/xmlenc#rsa-1_5"/>
<Keyinfo xmlns = "http://www.w3.org/2000/09/xmldsig#">
<Keyname> RSA key </keyname>
</Keyinfo>
<Cipherdata>
<Ciphervalue> encrypt/decrypt + 1 uedt + PL + iuf1ewgeh9vb +/8a9xmbywtmbacr/f/qusc1nq = </ciphervalue>
</Cipherdata>
</Encryptedkey>
</Keyinfo>
<Cipherdata>
<Ciphervalue> signature + pcbrz6h/Signature + signature/Signature/jigrdollaqbo3/ueavanc0swol/6bhws5mw/Signature/S80/i61qjxvsjeq/Signature + n1jvfqtadxqmckoy1nzllrd3ca9cb1qruqn/rxbgofht1f6y/rules = </ciphervalue>
</Cipherdata>
</Encrypteddata>
</Connectionstrings>
</Configuration>
Note that according to the time suggestions given by hands on lab, the time for completing the above three exercises should be 30 minutes.
For more articles about Enterprise Library, refer to Enterprise Library articles.