CspParameters 类

定义

包含传递给执行加密计算的加密服务提供程序(CSP)的参数。 此类不能被继承。

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
继承
CspParameters
属性

示例

下面的代码示例使用 CspParameters 类创建密钥容器,并将密钥保存在容器中。

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

下面的代码示例使用 CspParameters 类选择智能卡加密服务提供程序。 然后,它使用智能卡对数据进行签名和验证。

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

注解

CspParameters 类表示可以传递给托管加密类的参数,这些类可从非托管Microsoft加密 API (CAPI) 内部使用Microsoft加密服务提供商(CSP)。 名称以“CryptoServiceProvider”结尾的类是相应的 CSP 的托管代码包装器。

使用 CspParameters 类执行以下操作:

  • 通过将提供程序类型传递给 ProviderTypeProviderName 属性来指定特定的 CSP。 还可以使用构造函数的重载指定 CSP。

  • 创建密钥容器,可在其中存储加密密钥。 密钥容器提供最安全的方法来保存加密密钥,并将它们保密给恶意第三方。 有关创建密钥容器的详细信息,请参阅 如何:在密钥容器中存储非对称密钥

  • 指定是使用 KeyNumber 属性创建非对称签名密钥还是非对称交换密钥。

构造函数

名称 说明
CspParameters()

初始化 CspParameters 类的新实例。

CspParameters(Int32, String, String, CryptoKeySecurity, IntPtr)

使用提供程序类型、提供程序名称、容器名称、访问信息和对非托管智能卡密码对话框的句柄初始化类的新实例 CspParameters

CspParameters(Int32, String, String, CryptoKeySecurity, SecureString)

使用提供程序类型、提供程序名称、容器名称、访问信息和与智能卡密钥关联的密码初始化类的新实例 CspParameters

CspParameters(Int32, String, String)

使用指定的提供程序类型代码和名称以及指定的容器名称初始化类的新实例 CspParameters

CspParameters(Int32, String)

使用指定的提供程序类型代码和名称初始化类的新实例 CspParameters

CspParameters(Int32)

使用指定的提供程序类型代码初始化类的新实例 CspParameters

字段

名称 说明
KeyContainerName

表示的 CspParameters密钥容器名称。

KeyNumber

指定非对称密钥是创建为签名密钥还是交换密钥。

ProviderName

表示的 CspParameters提供程序名称。

ProviderType

表示 . CspParameters的提供程序类型代码。

属性

名称 说明
CryptoKeySecurity

获取或设置一个 CryptoKeySecurity 对象,该对象表示容器的访问权限和审核规则。

Flags

表示修改加密服务提供程序(CSP)行为的标志 CspParameters

KeyPassword

获取或设置与智能卡密钥关联的密码。

ParentWindowHandle

获取或设置智能卡密码对话框的非托管父窗口的句柄。

方法

名称 说明
Equals(Object)

确定指定的对象是否等于当前对象。

(继承自 Object)
GetHashCode()

用作默认哈希函数。

(继承自 Object)
GetType()

获取当前实例的 Type

(继承自 Object)
MemberwiseClone()

创建当前 Object的浅表副本。

(继承自 Object)
ToString()

返回一个表示当前对象的字符串。

(继承自 Object)

适用于

另请参阅