Decrypting Data by Using a Symmetric Encryption Provider

Retired Content

This content is outdated and is no longer being maintained. It is provided as a courtesy for individuals who are still using these technologies. This page may contain URLs that were valid when originally published, but now link to sites or pages that no longer exist.

The latest Enterprise Library information can be found at the Enterprise Library site.

If you encrypt data by using a symmetric encryption provider, you usually have to decrypt the data using the same provider.

Typical Goals

In this scenario, you want use a symmetric provider to decrypt data that you provide. The output of the symmetric provider is the unencrypted data.

Solution

Call the appropriate overload (string or byte array) of the static method DecryptSymmetric on the Cryptographer class to perform the decryption, supplying the name of the configured symmetric provider to be used and the data that you want to decrypt.

QuickStart

For an extended example of how to use the DecryptSymmetric method to decrypt data, see Walkthrough: Decrypting a Secret.

Using DecryptSymmetric

The following code shows how to use the DecryptSymmetric method to decrypt data in the form of a base64-encoded string.

string encryptedContentsBase64 = Cryptographer.EncryptSymmetric("symmProvider", "password");

// Decrypt the base64 encoded string.
string readableString; 
readableString = Cryptographer.DecryptSymmetric("symmProvider", encryptedContentsBase64);
'Usage
Dim encryptedContentsBase64 As String
encryptedContentsBase64 = Cryptographer.EncryptSymmetric("symmProvider", "password")

' Decrypt the base64 encoded string.
Dim readableString As String
readableString = Cryptographer.DecryptSymmetric("symmProvider", encryptedContentsBase64)

The following code shows how to use the DecryptSymmetric method to decrypt data in the form of a byte array.

byte[] valueToEncrypt = Encoding.Unicode.GetBytes("password");
byte[] encryptedContents = Cryptographer.EncryptSymmetric("symmProvider", valueToEncrypt);

// stringToDecrypt contains an encrypted string.
byte[] decryptedContents = Cryptographer.DecryptSymmetric("symmProvider", encryptedContents);
string plainText = (new UnicodeEncoding()).GetString(decryptedContents);
'Usage
Dim valueToEncrypt = Encoding.Unicode.GetBytes("password")
Dim encryptedContents As Byte() = Cryptographer.EncryptSymmetric("symmProvider", valueToEncrypt)

' stringToDecrypt contains an encrypted string.
Dim decryptedContents As Byte() = Cryptographer.EncryptSymmetric("symmProvider", stringToDecrypt)
Dim plainText As String = (New UnicodeEncoding).GetString(decryptedContents)

Usage Notes

Make sure you configure the appropriate symmetric provider in the Enterprise Library Configuration Console.

Retired Content

This content is outdated and is no longer being maintained. It is provided as a courtesy for individuals who are still using these technologies. This page may contain URLs that were valid when originally published, but now link to sites or pages that no longer exist.

The latest Enterprise Library information can be found at the Enterprise Library site.