What Does the Cryptography Application Block Do?
The Cryptography Application Block consists of a facade that allows you to access the two types of providers contained within the block. These two types of providers are:
- Hashing providers. These providers can be used to generate a hash from a value you supply, and compare two hash values. The block includes hash providers that use a range of common hashing algorithms.
- Cryptography providers. These providers can be used to encrypt and decrypt values that you supply. The block includes cryptography providers that use a range of common encryption algorithms.
When you use the cryptography providers to encrypt a value, you can specify the value as an array of bytes, and the method will return the result as an array of bytes. Alternatively, you can specify the input value as a string, and the methods will return the result as a base-64 encoded string. The methods that decrypt values work the same way, except that string values returned by the methods are not base-64 encoded.
When you use the hashing providers to create a hash, you can specify the value to hash as an array of bytes, and the method will return the result as an array of bytes. Alternatively, you can specify the input value as a string, and the methods will return the result as a string. The methods that compare hash values accept either an array of bytes or a string, and return either true or false.
Example Application Code
The following code shows how to encrypt and decrypt data. This code shows how to use the overloads that accept a string. This example assumes that you have resolved this class through the Enterprise Library container to inject the instance of the CryptographyManager class.
Note
For more information on instantiating objects, see Creating and Referencing Enterprise Library Objects.
public void ExampleScenario(CryptographyManager crypto)
{
string encryptedContentsBase64 = crypto.EncryptSymmetric("symmProvider", "SensitiveData");
// Decrypt the base64 encoded string
string readableString = crypto.DecryptSymmetric("symmProvider", encryptedContentsBase64);
}
'Usage
Public Sub ExampleScenario(ByVal crypto As CryptographyManager)
Dim encryptedContentsBase64 As String
encryptedContentsBase64 = crypto.EncryptSymmetric("symmProvider", "SensitiveData")
' Decrypt the base64 encoded string
Dim readableString As String
readableString = crypto.DecryptSymmetric("symmProvider", encryptedContentsBase64)
End Sub
For information about resolving Enterprise Library objects in your applications, see Creating and Referencing Enterprise Library Objects.