Microsoft Enterprise Library 5.0 |
Extending the Cryptography Application Block |
The Cryptography Application Block is designed to be used in a variety of applications and to be a general-purpose block. Extension points let you adapt the block to suit the needs of any particular application. You can extend the capabilities of the block by adding custom cryptography providers. Typically, these custom providers are third-party cryptography providers. The following table lists the interfaces that you can use to extend the block.
Custom Provider or Extension |
Interface |
---|---|
Hash Algorithm Provider |
IHashProvider |
Symmetric Encryption Algorithm Provider |
ISymmetricCryptoProvider |
To extend the Cryptography Application Block
- Create a new custom class and add it to your project.
- Make sure the class implements the required interfaces, constructors, and methods.
- Configure the generic provider in the Enterprise Library configuration tools:
- Specify your custom class as the type name.
- Specify any custom configuration properties by modifying the attributes of the object.
To create a custom hash algorithm provider
- Create a new class, and then add it to your project.
- (Optional) To use elements without fully qualifying the element reference, you can add the following using statement (C#) or Imports statement (Visual Basic) to the top of your source code file.
C# Copy Code using Microsoft.Practices.EnterpriseLibrary.Security.Cryptography; using Microsoft.Practices.EnterpriseLibrary.Security.Cryptography.Configuration;
Visual Basic Copy Code Imports Microsoft.Practices.EnterpriseLibrary.Security.Cryptography Imports Microsoft.Practices.EnterpriseLibrary.Security.Cryptography.Configuration
Note: For Visual Basic projects, you can also use the References page of the Project Designer to manage references and imported namespaces. To access the References page, select a project node in Solution Explorer, and then click [project name] Properties on the Project menu. When the Project Designer appears, click the References tab. - Specify that the class implements IHashProvider.
- Add the class attribute ConfigurationElementType. Specify the type CustomHashProviderData as the attribute parameter.
C# Copy Code [ConfigurationElementType(typeof(CustomHashProviderData))] public class MyHashProvider : IHashProvider
Visual Basic Copy Code <ConfigurationElementType(GetType(CustomHashProviderData))> _ Public Class MyHashProvider Implements IHashProvider
- Add a constructor that has a parameter of type NameValueCollection.
C# Copy Code public MyHashProvider(NameValueCollection attributes) { }
Visual Basic Copy Code Public Sub New(ByVal attributes As NameValueCollection) End Sub
- Add the CreateHash and CompareHash methods to your class, and then implement the required behavior.
C# Copy Code public byte[] CreateHash(byte[] plaintext) { } public bool CompareHash(byte[] plaintext, byte[] hashedtext) { }
Visual Basic Copy Code Public Function CreateHash(ByVal plaintext As Byte()) As Byte() End Function Public Function CompareHash(ByVal plaintext As Byte(), ByVal hashedtext As Byte()) As Boolean End Function
To create a custom symmetric encryption algorithm provider
- Create a new class, and then add it to your project.
- (Optional) To use elements without fully qualifying the element reference, you can add the following using statement (C#) or Imports statement (Visual Basic) to the top of your source code file.
C# Copy Code using Microsoft.Practices.EnterpriseLibrary.Security.Cryptography; using Microsoft.Practices.EnterpriseLibrary.Security.Cryptography.Configuration;
Visual Basic Copy Code Imports Microsoft.Practices.EnterpriseLibrary.Security.Cryptography Imports Microsoft.Practices.EnterpriseLibrary.Security.Cryptography.Configuration
Note: For Visual Basic projects, you can also use the References page of the Project Designer to manage references and imported namespaces. To access the References page, select a project node in Solution Explorer, and then click [project name] Properties on the Project menu. When the Project Designer appears, click the References tab. - Specify that the class implements ISymmetricCryptoProvider.
- Add the class attribute ConfigurationElementType. Specify the type CustomSymmetricCryptoProviderData as the attribute parameter.
C# Copy Code [ConfigurationElementType(typeof(CustomSymmetricCryptoProviderData))] public class MyCustomEncryptionProvider : ISymmetricCryptoProvider
Visual Basic Copy Code <ConfigurationElementType(GetType(CustomSymmetricCryptoProviderData))> _ Public Class MyCustomEncryptionProvider Implements ISymmetricCryptoProvider
- Add a constructor that has a parameter of type NameValueCollection.
C# Copy Code public MyCustomEncryptionProvider (NameValueCollection attributes) { }
Visual Basic Copy Code Public Sub New(ByVal attributes As NameValueCollection) End Sub
- Add the Encrypt and Decrypt methods to your class, and then implement the required behavior.
C# Copy Code public byte[] Encrypt(byte[] plaintext) { } public byte[] Decrypt(byte[] ciphertext) { }
Visual Basic Copy Code Public Function Encrypt(ByVal plaintext As Byte()) As Byte() End Function Public Function Decrypt(ByVal ciphertext As Byte()) As Byte() End Function
For detailed information about how to integrate custom providers with the Enterprise Library configuration system and configuration tools see Creating Custom Providers for Enterprise Library.