Extending the Cryptography Application Block

Retired Content

This content is outdated and is no longer being maintained. It is provided as a courtesy for individuals who are still using these technologies. This page may contain URLs that were valid when originally published, but now link to sites or pages that no longer exist.

The latest Enterprise Library information can be found at the Enterprise Library site.

The Cryptography Application Block is designed to be used in a variety of applications and to be a general-purpose application block. Extension points let you adapt the application block to suit the needs of any particular application. You can extend the capabilities of the application block by adding custom cryptography providers. Typically, these custom providers are third-party cryptography providers.

To extend the Cryptography Application Block

  1. Create a new custom class and add it to your project.
  2. Make sure the class implements the required interfaces, constructors, and methods.
  3. Create the generic object in the Enterprise Library Configuration Console.
  4. Specify your custom class as the type name.
  5. Specify any custom configuration properties by modifying the attributes of the object.

To create a custom hash algorithm provider

  1. Create a new class, and then add it to your project.

  2. (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.

    using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;
    using Microsoft.Practices.EnterpriseLibrary.Security.Cryptography;
    using Microsoft.Practices.EnterpriseLibrary.Security.Cryptography.Configuration;
    
    'Usage
    Imports Microsoft.Practices.EnterpriseLibrary.Common.Configuration
    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 Properties on the Project menu. When the Project Designer appears, click the References tab.

  3. Specify that the class implements IHashProvider.

  4. Add the class attribute ConfigurationElementType. Specify the type CustomHashProviderData as the attribute parameter.

    [ConfigurationElementType(typeof(CustomHashProviderData))]
    public class MyHashProvider : IHashProvider
    
    'Usage
    <ConfigurationElementType(GetType(CustomHashProviderData))> _
    Public Class MyHashProvider: Implements IHashProvider
    
  5. Add a constructor that has parameter of type NameValueCollection.

    public MyHashProvider(NameValueCollection attributes)
    {
    }
    
    'Usage
    Public Sub New(ByVal attributes As NameValueCollection)
    
    End Sub
    
  6. Add the CreateHash and CompareHash methods to your class, and then implement the required behavior.

    public byte[] CreateHash(byte[] plaintext)
    {
    }
    public bool CompareHash(byte[] plaintext, byte[] hashedtext)
    {
    }
    
    'Usage
    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

  1. Create a new class, and then add it to your project.

  2. (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.

    using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;
    using Microsoft.Practices.EnterpriseLibrary.Security.Cryptography;
    using Microsoft.Practices.EnterpriseLibrary.Security.Cryptography.Configuration;
    
    'Usage
    Imports Microsoft.Practices.EnterpriseLibrary.Common.Configuration
    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, and then click Properties on the Project menu. When the Project Designer appears, click the References tab.

  3. Specify that the class implements ISymmetricCryptoProvider.

  4. Add the class attribute ConfigurationElementType. Specify the type CustomSymmetricCryptoProviderData as the attribute parameter.

    [ConfigurationElementType(typeof(CustomSymmetricCryptoProviderData))]
    public class MyCustomEncryptionProvider : ISymmetricCryptoProvider
    
    'Usage
    <ConfigurationElementType(GetType(CustomSymmetricCryptoProviderData))> _
    Public Class MyCustomEncryptionProvider: Implements ISymmetricCryptoProvider
    
  5. Add a constructor that has parameter of type NameValueCollection.

    public MockCustomSymmetricProvider(NameValueCollection attributes)
    {
    }
    
    'Usage
    Public Sub New(ByVal attributes As NameValueCollection)
    
    End Sub
    
  6. Add the Encrypt and Decrypt methods to your class, and then implement the required behavior.

    public byte[] Encrypt(byte[] plaintext)
    {
    }
    
    public byte[] Decrypt(byte[] ciphertext)
    {
    } 
    
    'Usage
    Public Function Encrypt(ByVal plaintext As Byte()) As Byte()
    End Function 
    Public Function Decrypt(ByVal ciphertext As Byte()) As Byte() 
    End Function
    
Retired Content

This content is outdated and is no longer being maintained. It is provided as a courtesy for individuals who are still using these technologies. This page may contain URLs that were valid when originally published, but now link to sites or pages that no longer exist.

The latest Enterprise Library information can be found at the Enterprise Library site.