CspParameters 类 
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
包含一些参数,这些参数传递到指定加密计算的加密服务提供程序 (CSP)。 此类不能被继承。
public ref class CspParameters sealed[System.Runtime.Versioning.SupportedOSPlatform("windows")]
public sealed class CspParameterspublic sealed class CspParameters[System.Runtime.InteropServices.ComVisible(true)]
public sealed class CspParameters[<System.Runtime.Versioning.SupportedOSPlatform("windows")>]
type CspParameters = classtype CspParameters = class[<System.Runtime.InteropServices.ComVisible(true)>]
type CspParameters = classPublic NotInheritable Class CspParameters- 继承
- 
				CspParameters
- 属性
示例
下面的代码示例使用 类创建密钥容器, CspParameters 并将密钥保存在容器中。
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
下面的代码示例使用 CspParameters 类来选择智能卡加密服务提供程序。 然后,它使用智能卡对数据进行签名和验证。
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
注解
类 CspParameters 表示可以传递给托管加密类的参数,这些类在内部使用 Microsoft 加密服务提供程序 (CSP) 从非托管 Microsoft 加密 API (CAPI) 。 名称以“CryptoServiceProvider”结尾的类是相应 CSP 的托管代码包装器。
CspParameters使用 类执行以下操作:
- 通过将提供程序类型传递给 或 ProviderName 属性来ProviderType指定特定的 CSP。 还可以使用构造函数的重载指定 CSP。 
- 创建可在其中存储加密密钥的密钥容器。 密钥容器提供了最安全的方式来保存加密密钥,并将它们保密给恶意第三方。 有关创建密钥容器的详细信息,请参阅 如何:在密钥容器中存储非对称密钥。 
- 指定是使用 KeyNumber 属性创建非对称签名密钥还是非对称交换密钥。 
构造函数
| CspParameters() | 初始化 CspParameters 类的新实例。 | 
| CspParameters(Int32) | 使用指定的提供程序类型代码初始化 CspParameters 类的新实例。 | 
| CspParameters(Int32, String) | 使用指定的提供程序类型代码和名称初始化 CspParameters 类的新实例。 | 
| CspParameters(Int32, String, String) | 使用指定的提供程序类型代码和名称以及指定的容器名称初始化 CspParameters 类的新实例。 | 
| CspParameters(Int32, String, String, CryptoKeySecurity, IntPtr) | 使用提供程序类型、提供程序名称、容器名称、访问信息以及非托管智能卡密码对话框的句柄初始化 CspParameters 类的新实例。 | 
| CspParameters(Int32, String, String, CryptoKeySecurity, SecureString) | 使用提供程序类型、提供程序名称、容器名称、访问信息以及与智能卡密钥相关的密码初始化 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) |