CspParameters CspParameters CspParameters CspParameters Class

定义

包含传递给执行加密计算的加密服务提供程序 (CSP) 的参数。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.InteropServices.ComVisible(true)]
public sealed class CspParameters
type CspParameters = class
Public NotInheritable Class CspParameters
继承
CspParametersCspParametersCspParametersCspParameters
属性

示例

下面的代码示例使用CspParameters类创建一个密钥容器, 并将该密钥保存到容器中。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 'StoreKey

下面的代码示例使用CspParameters类来选择智能卡加密服务提供程序。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"SHA1" );
   Console::WriteLine( L"Signature	: {0}", BitConverter::ToString( sig ) );
   
   // Verify the data using the Smart Card CryptoGraphic Provider.
   bool verified = rsa->VerifyData( data, L"SHA1", 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, "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);

        }
    }
}
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, "SHA1")

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

        ' Verify the data using the Smart Card CryptoGraphic Provider.
        Dim verified As Boolean = rsa.VerifyData(data, "SHA1", sig)

        Console.WriteLine("Verified")

    End Sub

End Module

注解

CspParameters类表示一些参数, 你可以将这些参数传递给托管的加密类, 这些类在内部使用 microsoft 加密服务提供程序 (csp) 的非托管的 microsoft 加密 API (CAPI)。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). 名称以 "CryptoServiceProvider" 结尾的类是对应 CSP 的托管代码包装。Classes with names ending in "CryptoServiceProvider" are managed code wrappers for the corresponding CSP.

CspParameters使用类来执行以下操作:Use the CspParameters class to do the following:

  • 通过向ProviderTypeProviderName属性传递提供程序类型来指定特定 CSP。Specify a particular CSP by passing the provider type to the ProviderType or ProviderName property. 还可以使用构造函数的重载来指定 CSP。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.

  • 使用KeyNumber属性指定是创建非对称签名密钥还是非对称交换密钥。Specify whether to create an asymmetric signature key or an asymmetric exchange key using the KeyNumber property.

构造函数

CspParameters() CspParameters() CspParameters() CspParameters()

初始化 CspParameters 类的新实例。Initializes a new instance of the CspParameters class.

CspParameters(Int32) CspParameters(Int32) CspParameters(Int32) CspParameters(Int32)

使用指定的提供程序类型代码初始化 CspParameters 类的新实例。Initializes a new instance of the CspParameters class with the specified provider type code.

CspParameters(Int32, String) CspParameters(Int32, String) CspParameters(Int32, String) CspParameters(Int32, String)

使用指定的提供程序类型代码和名称初始化 CspParameters 类的新实例。Initializes a new instance of the CspParameters class with the specified provider type code and name.

CspParameters(Int32, String, String) CspParameters(Int32, String, String) CspParameters(Int32, String, String) CspParameters(Int32, String, String)

使用指定的提供程序类型代码和名称以及指定的容器名称初始化 CspParameters 类的新实例。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) CspParameters(Int32, String, String, CryptoKeySecurity, IntPtr) CspParameters(Int32, String, String, CryptoKeySecurity, IntPtr) CspParameters(Int32, String, String, CryptoKeySecurity, IntPtr)

使用提供程序类型、提供程序名称、容器名称、访问信息以及非托管智能卡密码对话框的句柄初始化 CspParameters 类的新实例。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) CspParameters(Int32, String, String, CryptoKeySecurity, SecureString) CspParameters(Int32, String, String, CryptoKeySecurity, SecureString) CspParameters(Int32, String, String, CryptoKeySecurity, SecureString)

使用提供程序类型、提供程序名称、容器名称、访问信息以及与智能卡密钥相关的密码初始化 CspParameters 类的新实例。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.

字段

KeyContainerName KeyContainerName KeyContainerName KeyContainerName

表示 CspParameters 的密钥容器名称。Represents the key container name for CspParameters.

KeyNumber KeyNumber KeyNumber KeyNumber

指定非对称密钥是作为签名密钥还是交换密钥创建。Specifies whether an asymmetric key is created as a signature key or an exchange key.

ProviderName ProviderName ProviderName ProviderName

表示 CspParameters 的提供程序名称。Represents the provider name for CspParameters.

ProviderType ProviderType ProviderType ProviderType

表示 CspParameters 的提供程序类型代码。Represents the provider type code for CspParameters.

属性

CryptoKeySecurity CryptoKeySecurity CryptoKeySecurity CryptoKeySecurity

获取或设置表示容器访问权限及审核规则的 CryptoKeySecurity 对象。Gets or sets a CryptoKeySecurity object that represents access rights and audit rules for a container.

Flags Flags Flags Flags

表示修改加密服务提供程序 (CSP) 的行为的 CspParameters 的标志。Represents the flags for CspParameters that modify the behavior of the cryptographic service provider (CSP).

KeyPassword KeyPassword KeyPassword KeyPassword

获取或设置与智能卡密钥相关的密码。Gets or sets a password associated with a smart card key.

ParentWindowHandle ParentWindowHandle ParentWindowHandle ParentWindowHandle

获取或设置智能卡密码对话框的非托管父级窗口的句柄。Gets or sets a handle to the unmanaged parent window for a smart card password dialog box.

方法

Equals(Object) Equals(Object) Equals(Object) Equals(Object)

确定指定的对象是否等于当前对象。Determines whether the specified object is equal to the current object.

(Inherited from Object)
GetHashCode() GetHashCode() GetHashCode() GetHashCode()

作为默认哈希函数。Serves as the default hash function.

(Inherited from Object)
GetType() GetType() GetType() GetType()

获取当前实例的 TypeGets the Type of the current instance.

(Inherited from Object)
MemberwiseClone() MemberwiseClone() MemberwiseClone() MemberwiseClone()

创建当前 Object 的浅表副本。Creates a shallow copy of the current Object.

(Inherited from Object)
ToString() ToString() ToString() ToString()

返回表示当前对象的字符串。Returns a string that represents the current object.

(Inherited from Object)

适用于

另请参阅