The .NET abstract encryption classes

The .NET abstract encryption classes provide the following:

1. They define the basic members that encryption implementations need to support.

2. They offer some functionality through the static Create() method, which you can use to indirectly create a class instance for you. You can use it to create one of the concrete implementation classes without needing to know how it is implemented.


For example the static Create() method, in the next examples, returns an instance of the default DES implementation class:


in C#


RC2 crypt = RC2.Create();




Dim Crypt as RC2 = RC2.Create()


In this case, the class is RC2CryptoServiceProvider. The advantage of this technique is that you can code generically, without creating a dependency on a specific implementation. If Microsoft updates the framework and the default RC2 implementation class changes, your code will pick up the change faultlessly. This is particularly useful if you are using a CryptoAPI class, which could be replaced with a managed class equivalent in the future.


Important notes:

1. It is good practice to use abstract algorithm classes, because you will know which type of algorithm you are using (and any limitations it may have) without worrying about the underlying implementation.

2. Most of the algorithm classes support a GenerateKey() method, in addition to methods for encrypting and decrypting data with an algorithm. This method generates a random key that follows the key requirements of the corresponding algorithm. The key is generated on strong cryptographic random number generators that are part of the Windows platform so that the value is really unpredictable and random.