Share via


Decrypting Data Using a Symmetric 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 of the static DecryptSymmetric method of 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 as a string or a byte array.

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 = Cryptographer.DecryptSymmetric("symmProvider", encryptedContentsBase64);
'Usage
Dim encryptedContentsBase64 As String = Cryptographer.EncryptSymmetric("symmProvider", "password")

' Decrypt the base64 encoded string.
Dim readableString As String = 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);

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)

Dim decryptedContents As Byte() = Cryptographer.DecryptSymmetric("symmProvider", encryptedContents)
Dim plainText As String = (New UnicodeEncoding()).GetString(decryptedContents)

Usage Notes

Make sure you configure the appropriate symmetric provider in the Enterprise Library configuration tools.