Home page  

Products

CipherLite.NET

CipherSafe.NET

Downloads

Resources

Articles

Libraries

Samples

Site Map

BIGGER FONTsmaller font

Using CipherLite.NET™ to Encrypt and Decrypt Application Setting

The following example shows how to encrypt a connection string using DPAPI with machine key (machine store), save the encrypted value in the application configuration file, and retrieve and decrypt the value at run time.

Step 1. Encrypt value

Start the CipherLite.NET™ GUI tool and encrypt the value using the same (or similar) settings as shown in Figure 1 (for additional information about using the CipherLite.NET™ GUI tool for encrypting data, read the Encryption chapter of the CipherLite.NET™ User's Guide).


Figure 1. Encrypting connection string using DPAPI with machine key

Once you encrypt the value, click the Copy button to save ciphertext to the clipboard.

Step 2. Save encrypted value in .config file

Open your application's .config file and paste the encrypted value from the clipboard to the appropriate appSettings section as shown below (in this example, the appSettings element is called DB):

<?xml version="1.0" encoding="utf-8" ?> <configuration> <appSettings> <add key="DB" value="AQAA...4SUSg==" /> </appSettings> </configuration>

Step 3. Decrypt the value in your application

Add a reference to the CipherLite.dll file to your application and add code similar to the following:

// // C# example // using System.Configuration; using Obviex.CipherLite; ... try { // Get base64-encoded ciphertext value. string configValue; configValue = ConfigurationSettings.AppSettings["DB"]; // Convert base64-encoded value to byte array. byte[] cipherText; cipherText = Encoder.Base64Decode(configValue); // Decrypt value using DPAPI. string connString; connString= Dpapi.Decrypt(cipherText); // Use decrypted connection string. ... } catch (Exception ex) { // Process exception. }

 

' ' Visual Basic.NET example ' Imports System.Configuration Imports Obviex.CipherLite ... Try ' Get base64-encoded ciphertext value. Dim configValue As String configValue = ConfigurationSettings.AppSettings("DB") ' Convert base64-encoded value to byte array. Dim cipherText As Byte() cipherText = Encoder.Base64Decode(configValue) ' Decrypt value using DPAPI. Dim connString As String connString= Dpapi.Decrypt(cipherText) ' Use decrypted connection string. ... Catch ex As Exception ' Process exception. End Try

 

Copyright © 2002-2010 Obviex™ | All rights reserved | Legal | Privacy | Contact us