Поделиться через


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

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

  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.Security.Cryptography;
    using Microsoft.Practices.EnterpriseLibrary.Security.Cryptography.Configuration;
    
    'Usage
    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.

  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 a 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.Security.Cryptography;
    using Microsoft.Practices.EnterpriseLibrary.Security.Cryptography.Configuration;
    
    'Usage
    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.

  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 a parameter of type NameValueCollection.

    public MyCustomEncryptionProvider (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
    

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.