This documentation is archived and is not being maintained.

CspParameters Class

Contains parameters that are passed to the cryptographic service provider (CSP) that performs cryptographic computations. This class cannot be inherited.


Namespace:  System.Security.Cryptography
Assembly:  mscorlib (in mscorlib.dll)

public sealed class CspParameters

The CspParameters type exposes the following members.

Public methodCspParameters()Initializes a new instance of the CspParameters class.
Public methodCspParameters(Int32)Initializes a new instance of the CspParameters class with the specified provider type code.
Public methodCspParameters(Int32, String)Initializes a new instance of the CspParameters class with the specified provider type code and name.
Public methodCspParameters(Int32, String, String)Initializes a new instance of the CspParameters class with the specified provider type code and name, and the specified container name.
Public methodCspParameters(Int32, String, String, CryptoKeySecurity, IntPtr)Initializes a new instance of the CspParameters class using a provider type, a provider name, a container name, access information, and a handle to an unmanaged smart card password dialog.
Public methodCspParameters(Int32, String, String, CryptoKeySecurity, SecureString)Initializes a new instance of the CspParameters class using a provider type, a provider name, a container name, access information, and a password associated with a smart card key.

Public propertyCryptoKeySecurityGets or sets a CryptoKeySecurity object that represents access rights and audit rules for a container.
Public propertyFlagsRepresents the flags for CspParameters that modify the behavior of the cryptographic service provider (CSP).
Public propertyKeyPasswordGets or sets a password associated with a smart card key.
Public propertyParentWindowHandleGets or sets a handle to the unmanaged parent window for a smart card password dialog.

Public methodEquals(Object)Determines whether the specified Object is equal to the current Object. (Inherited from Object.)
Protected methodFinalizeAllows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.)
Public methodGetHashCodeServes as a hash function for a particular type. (Inherited from Object.)
Public methodGetTypeGets the Type of the current instance. (Inherited from Object.)
Protected methodMemberwiseCloneCreates a shallow copy of the current Object. (Inherited from Object.)
Public methodToStringReturns a string that represents the current object. (Inherited from Object.)

Public fieldKeyContainerNameRepresents the key container name for CspParameters.
Public fieldKeyNumberSpecifies whether an asymmetric key is created as a signature key or an exchange key.
Public fieldProviderNameRepresents the provider name for CspParameters.
Public fieldProviderTypeRepresents the provider type code for CspParameters.

The CspParameters class represents parameters that you can pass to managed cryptography classes that internally use Microsoft Cryptographic Service Providers (CSPs) from the unmanaged Microsoft Cryptography API (CAPI). Classes with names ending in "CryptoServiceProvider" are managed code wrappers for the corresponding CSP.

Use the CspParameters class to do the following:

  • Specify a particular CSP by passing the provider type to the ProviderType or ProviderName property. You can also specify a CSP using an overload of the constructor.

  • Create a key container where you can store cryptographic keys. Key containers provide the most secure way to persist cryptographic keys and keep them secret from malicious third parties. For more information about creating key containers, see How to: Store Asymmetric Keys in a Key Container.

  • Specify whether to create an asymmetric signature key or an asymmetric exchange key using the KeyNumber property.

The following code example creates a key container using the CspParameters class and saves the key in the container.

using System;
using System.IO;
using System.Security.Cryptography;

public class StoreKey
	public static void Main()
		// creates the CspParameters object and sets the key container name used to store the RSA key pair
		CspParameters cp = new CspParameters();
		cp.KeyContainerName = "MyKeyContainerName";

		// instantiates the rsa instance accessing the key container MyKeyContainerName
		RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(cp);
		// add the below line to delete the key entry in MyKeyContainerName
		// rsa.PersistKeyInCsp = false;

		//writes out the current key pair used in the rsa instance
		Console.WriteLine("Key is : \n" + rsa.ToXmlString(true));

The following code example uses the CspParameters class to select a Smart Card Cryptographic Service Provider. It then signs and verifies data using the smart card.

using System;
using System.Security.Cryptography;

namespace SmartCardSign
    class SCSign
        static void Main(string[] args)
            // To idendify the Smart Card CryptoGraphic Providers on your
            // computer, use the Microsoft Registry Editor (Regedit.exe).
            // The available Smart Card CryptoGraphic Providers are listed
            // in HKEY_LOCAL_MACHINE\Software\Microsoft\Cryptography\Defaults\Provider.

            // Create a new CspParameters object that identifies a 
            // Smart Card CryptoGraphic Provider.
            // The 1st parameter comes from HKEY_LOCAL_MACHINE\Software\Microsoft\Cryptography\Defaults\Provider Types.
            // The 2nd parameter comes from HKEY_LOCAL_MACHINE\Software\Microsoft\Cryptography\Defaults\Provider.
            CspParameters csp = new CspParameters(1, "Schlumberger Cryptographic Service Provider");
            csp.Flags = CspProviderFlags.UseDefaultKeyContainer;

            // Initialize an RSACryptoServiceProvider object using
            // the CspParameters object.
            RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(csp);

            // Create some data to sign.
            byte[] data = new byte[] { 0, 1, 2, 3, 4, 5, 6, 7 };

            Console.WriteLine("Data			: " + BitConverter.ToString(data));

            // Sign the data using the Smart Card CryptoGraphic Provider.
            byte[] sig = rsa.SignData(data, "SHA1");

            Console.WriteLine("Signature	: " + BitConverter.ToString(sig));

            // Verify the data using the Smart Card CryptoGraphic Provider.
            bool verified = rsa.VerifyData(data, "SHA1", sig);

            Console.WriteLine("Verified		: " + verified);


.NET Framework

Supported in: 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.