CspParameters Class
Definition
Important
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
Contains parameters that are passed to the cryptographic service provider (CSP) that performs cryptographic computations. This class cannot be inherited.
public ref class CspParameters sealed
[System.Runtime.Versioning.SupportedOSPlatform("windows")]
public sealed class CspParameters
public sealed class CspParameters
[System.Runtime.InteropServices.ComVisible(true)]
public sealed class CspParameters
[<System.Runtime.Versioning.SupportedOSPlatform("windows")>]
type CspParameters = class
type CspParameters = class
[<System.Runtime.InteropServices.ComVisible(true)>]
type CspParameters = class
Public NotInheritable Class CspParameters
- Inheritance
-
CspParameters
- Attributes
Examples
The following code example creates a key container using the CspParameters class and saves the key in the container.
using namespace System;
using namespace System::IO;
using namespace System::Security::Cryptography;
int main()
{
// creates the CspParameters object and sets the key container name used to store the RSA key pair
CspParameters^ cp = gcnew CspParameters;
cp->KeyContainerName = "MyKeyContainerName";
// instantiates the rsa instance accessing the key container MyKeyContainerName
RSACryptoServiceProvider^ rsa = gcnew 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{0}", rsa->ToXmlString( true ) );
}
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));
}
}
Imports System.IO
Imports System.Security.Cryptography
Public Class StoreKey
Public Shared Sub Main()
' creates the CspParameters object and sets the key container name used to store the RSA key pair
Dim cp As New CspParameters()
cp.KeyContainerName = "MyKeyContainerName"
' instantiates the rsa instance accessing the key container MyKeyContainerName
Dim rsa As 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 : " & rsa.ToXmlString(True))
End Sub
End Class
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 namespace System;
using namespace System::Security::Cryptography;
int main()
{
// 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 = gcnew CspParameters( 1,L"Schlumberger Cryptographic Service Provider" );
csp->Flags = CspProviderFlags::UseDefaultKeyContainer;
// Initialize an RSACryptoServiceProvider object using
// the CspParameters object.
RSACryptoServiceProvider^ rsa = gcnew RSACryptoServiceProvider( csp );
// Create some data to sign.
array<Byte>^data = gcnew array<Byte>{
0,1,2,3,4,5,6,7
};
Console::WriteLine( L"Data : {0}", BitConverter::ToString( data ) );
// Sign the data using the Smart Card CryptoGraphic Provider.
array<Byte>^sig = rsa->SignData( data, L"SHA256" );
Console::WriteLine( L"Signature : {0}", BitConverter::ToString( sig ) );
// Verify the data using the Smart Card CryptoGraphic Provider.
bool verified = rsa->VerifyData( data, L"SHA256", sig );
Console::WriteLine( L"Verified : {0}", verified );
}
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, "SHA256");
Console.WriteLine("Signature : " + BitConverter.ToString(sig));
// Verify the data using the Smart Card CryptoGraphic Provider.
bool verified = rsa.VerifyData(data, "SHA256", sig);
Console.WriteLine("Verified : " + verified);
}
}
}
Imports System.Security.Cryptography
Module SCSign
Sub Main(ByVal args() As String)
' 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.
Dim csp As New CspParameters(1, "Schlumberger Cryptographic Service Provider")
csp.Flags = CspProviderFlags.UseDefaultKeyContainer
' Initialize an RSACryptoServiceProvider object using
' the CspParameters object.
Dim rsa As New RSACryptoServiceProvider(csp)
' Create some data to sign.
Dim data() As Byte = {0, 1, 2, 3, 4, 5, 6, 7}
Console.WriteLine("Data : " + BitConverter.ToString(data))
' Sign the data using the Smart Card CryptoGraphic Provider.
Dim sig As Byte() = rsa.SignData(data, "SHA256")
Console.WriteLine("Signature : " + BitConverter.ToString(sig))
' Verify the data using the Smart Card CryptoGraphic Provider.
Dim verified As Boolean = rsa.VerifyData(data, "SHA256", sig)
Console.WriteLine("Verified")
End Sub
End Module
Remarks
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.
Constructors
CspParameters() |
Initializes a new instance of the CspParameters class. |
CspParameters(Int32) |
Initializes a new instance of the CspParameters class with the specified provider type code. |
CspParameters(Int32, String) |
Initializes a new instance of the CspParameters class with the specified provider type code and name. |
CspParameters(Int32, String, String) |
Initializes a new instance of the CspParameters class with the specified provider type code and name, and the specified container name. |
CspParameters(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. |
CspParameters(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. |
Fields
KeyContainerName |
Represents the key container name for CspParameters. |
KeyNumber |
Specifies whether an asymmetric key is created as a signature key or an exchange key. |
ProviderName |
Represents the provider name for CspParameters. |
ProviderType |
Represents the provider type code for CspParameters. |
Properties
CryptoKeySecurity |
Gets or sets a CryptoKeySecurity object that represents access rights and audit rules for a container. |
Flags |
Represents the flags for CspParameters that modify the behavior of the cryptographic service provider (CSP). |
KeyPassword |
Gets or sets a password associated with a smart card key. |
ParentWindowHandle |
Gets or sets a handle to the unmanaged parent window for a smart card password dialog box. |
Methods
Equals(Object) |
Determines whether the specified object is equal to the current object. (Inherited from Object) |
GetHashCode() |
Serves as the default hash function. (Inherited from Object) |
GetType() |
Gets the Type of the current instance. (Inherited from Object) |
MemberwiseClone() |
Creates a shallow copy of the current Object. (Inherited from Object) |
ToString() |
Returns a string that represents the current object. (Inherited from Object) |