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 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) 從 Unmanaged Microsoft Cryptography API (CAPI) 傳遞至受控加密類別。 名稱結尾為 「CryptoServiceProvider」 的類別是對應 CSP 的 Managed 程式碼包裝函式。

CspParameters使用 類別來執行下列動作:

  • 將提供者類型傳遞至 ProviderTypeProviderName 屬性,以指定特定的 CSP。 您也可以使用建構函式的多載來指定 CSP。

  • 建立金鑰容器,您可以在其中儲存密碼編譯金鑰。 金鑰容器提供最安全的方式來保存密碼編譯金鑰,並防止惡意的協力廠商使用密碼編譯金鑰。 如需建立金鑰容器的詳細資訊,請參閱 如何:將非對稱金鑰儲存在金鑰容器中

  • 指定是否要使用 屬性建立非對稱簽章金鑰或非對稱交換金鑰 KeyNumber

建構函式

CspParameters()

初始化 CspParameters 類別的新執行個體。

CspParameters(Int32)

使用指定的提供者類型碼,來初始化 CspParameters 類別的新執行個體。

CspParameters(Int32, String)

使用指定的提供者類型程式碼和名稱,來初始化 CspParameters 類別的新執行個體。

CspParameters(Int32, String, String)

使用指定的提供者類型程式碼和名稱,以及指定的容器名稱,初始化 CspParameters 類別的新執行個體。

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

使用提供者類型、提供者名稱、容器名稱、存取資訊和 Unmanaged 智慧卡密碼對話方塊的控制代碼,初始化 CspParameters 類別的新執行個體。

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

使用提供者類型、提供者名稱、容器名稱、存取資訊和與智慧卡金鑰關聯的密碼,初始化 CspParameters 類別的新執行個體。

欄位

KeyContainerName

表示 CspParameters 的金鑰容器名稱。

KeyNumber

指定將非對稱金鑰建立為簽章金鑰或交換金鑰。

ProviderName

表示 CspParameters 的提供者名稱。

ProviderType

表示 CspParameters 的提供者類型代碼。

屬性

CryptoKeySecurity

取得或設定 CryptoKeySecurity 物件,表示容器的存取權限和稽核規則。

Flags

代表 CspParameters 的旗標,這些旗標修改密碼編譯服務提供者 (CSP) 的行為。

KeyPassword

取得或設定與智慧卡金鑰關聯的密碼。

ParentWindowHandle

取得或設定智慧卡密碼對話方塊之 Unmanaged 父視窗的控制代碼。

方法

Equals(Object)

判斷指定的物件是否等於目前的物件。

(繼承來源 Object)
GetHashCode()

做為預設雜湊函式。

(繼承來源 Object)
GetType()

取得目前執行個體的 Type

(繼承來源 Object)
MemberwiseClone()

建立目前 Object 的淺層複製。

(繼承來源 Object)
ToString()

傳回代表目前物件的字串。

(繼承來源 Object)

適用於

另請參閱