Browse DevX
Sign up for e-mail newsletters from DevX


Browse the MS Enterprise Library: Cryptography Application Block : Page 3

One of the seven application blocks in Microsoft's new Enterprise Library is dedicated only to security issues: the Cryptography Application Block. See how easily you can implement it in your .NET applications to encrypt and decrypt sensitive data.




Building the Right Environment to Support AI, Machine Learning and Deep Learning

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.

Data Encryption
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.

Listing 1
string encryptedData = Cryptographer.EncryptSymmetric( "MySymmetricProvider", "Data to encrypt"); Console.WriteLine(encryptedData);

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.

Data Decryption
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.

Listing 2
string decryptedData = Cryptographer.DecryptSymmetric( "MySymmetricProvider", "Data to decrypt"); Console.WriteLine(decrytedData);

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.

Listing 3
string hashValue = Cryptographer.CreateHash( "MyHashProvider", "Data to hash"); Console.WriteLine(hashValue);

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.

Listing 4
bool theSame = Cryptographer.CompareHash( "MyHashProvider", "stringToCompare", "generated hash value"); Console.WriteLine(theSame);

When you call this method, you must provide in the first parameter the string for which a hash value should be calculated for the comparison—and 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.

Klaus Aschenbrenner is software architect and consultant for ANECON in Vienna, Austria. He helps software architects and developers design and implement enterprise solutions based on the .NET Framework and Web services. Find further information about Klaus Aschenbrenner at his weblog.
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date