Development with the Cryptography Application Block
Now that you know about the configuration of the Cryptography Application Block, it's time to dive into actual programming code that demonstrates its simple usage. This section is divided into four scenarios, each of which shows a different use for the application block.
To encrypt sensitive data, you first must configure a symmetric provider as described previously. As soon as you have done that, you can use the method Cryptographer.EncryptSymmetric
for data encryption as shown in Listing 1.
string encryptedData = Cryptographer.EncryptSymmetric(
"Data to encrypt");
As you can see, the first parameter must be the unique name of the symmetric provider you configured through the Configuration Console. The second string parameter contains the sensitive data to encrypt. You also have an overloaded version of this method, which allows you to supply the sensitive data as a byte array. The call to this overloaded method returns the encrypted data as a byte array as well. This approach enables you to encrypt binary data as well as string data.
Decrypting the encrypted data can be done through a call to the method Cryptographer.DecryptSymmetric
, which also has an overloaded version for supplying a byte array for the decryption process. Listing 2 shows the necessary code.
string decryptedData = Cryptographer.DecryptSymmetric(
"Data to decrypt");
As you can see from both listings, the methods used for encryption and decryption are very easy and straightforward. All information regarding the different providers and algorithms is completely separated from development and configured visually through the Configuration Console. Therefore, an administrator also can reconfigure all the security aspects of the application without a recompilation of the underlying source code.
Hash Value Calculation
An additional function that the Cryptography Application Block offers is the usage of hash providers for calculating hash values. As mentioned previously, you can use hash values for the careful storage of passwords in a database (no cleartext). You also can create a hash value from a string through the method Cryptographer.CreateHash
. Listing 3 provides further details.
string hashValue = Cryptographer.CreateHash(
"Data to hash");
The method accepts the name of the configured hash provider in the first parameter. (This hash provider is configured through the Configuration Console as described earlier.) The second parameter contains the data for which the hash value should be calculated.
Hash Value Comparison
As soon as you have calculated a hash value, you can compare it with another hash value through the method Cryptographer.CompareHash
. Listing 4 shows how this works.
bool theSame = Cryptographer.CompareHash(
"generated hash value");
When you call this method, you must provide in the first parameter the string for which a hash value should be calculated for the comparisonand not the actual hash value.
Quick, Easy, and Essential
The Cryptography Application Block is very simple to use because it consists of only four methods. You handle all the other aspects of the application block through the Configuration Console, a tool that is itself very easy to use. Therefore, you can use this application block to build security solutions very quickly.