Aracılığıyla paylaş


RSACryptoServiceProvider.PersistKeyInCsp Özellik

Tanım

Anahtarın şifreleme hizmeti sağlayıcısında (CSP) kalıcı olup olmayacağını belirten bir değer alır veya ayarlar.

public:
 property bool PersistKeyInCsp { bool get(); void set(bool value); };
public bool PersistKeyInCsp { get; set; }
member this.PersistKeyInCsp : bool with get, set
Public Property PersistKeyInCsp As Boolean

Özellik Değeri

true anahtarın CSP'de kalıcı olması gerekiyorsa; aksi takdirde , false.

Örnekler

Aşağıdaki kod örneği bir RSACryptoServiceProvider nesnesi oluşturur ve anahtarı bir anahtar kapsayıcısına kalıcı hale döndürür.

using namespace System;
using namespace System::Security::Cryptography;
void RSAPersistKeyInCSP( String^ ContainerName )
{
   try
   {
      
      // Create a new instance of CspParameters.  Pass
      // 13 to specify a DSA container or 1 to specify
      // an RSA container.  The default is 1.
      CspParameters^ cspParams = gcnew CspParameters;
      
      // Specify the container name using the passed variable.
      cspParams->KeyContainerName = ContainerName;
      
      //Create a new instance of RSACryptoServiceProvider to generate
      //a new key pair.  Pass the CspParameters class to persist the 
      //key in the container.  The PersistKeyInCsp property is true by 
      //default, allowing the key to be persisted. 
      RSACryptoServiceProvider^ RSAalg = gcnew RSACryptoServiceProvider( cspParams );
      
      //Indicate that the key was persisted.
      Console::WriteLine( "The RSA key was persisted in the container, \"{0}\".", ContainerName );
   }
   catch ( CryptographicException^ e ) 
   {
      Console::WriteLine( e->Message );
   }

}

void RSADeleteKeyInCSP( String^ ContainerName )
{
   try
   {
      
      // Create a new instance of CspParameters.  Pass
      // 13 to specify a DSA container or 1 to specify
      // an RSA container.  The default is 1.
      CspParameters^ cspParams = gcnew CspParameters;
      
      // Specify the container name using the passed variable.
      cspParams->KeyContainerName = ContainerName;
      
      //Create a new instance of RSACryptoServiceProvider. 
      //Pass the CspParameters class to use the 
      //key in the container.
      RSACryptoServiceProvider^ RSAalg = gcnew RSACryptoServiceProvider( cspParams );
      
      //Explicitly set the PersistKeyInCsp property to false
      //to delete the key entry in the container.
      RSAalg->PersistKeyInCsp = false;
      
      //Call Clear to release resources and delete the key from the container.
      RSAalg->Clear();
      
      //Indicate that the key was persisted.
      Console::WriteLine( "The RSA key was deleted from the container, \"{0}\".", ContainerName );
   }
   catch ( CryptographicException^ e ) 
   {
      Console::WriteLine( e->Message );
   }

}

int main()
{
   String^ KeyContainerName = "MyKeyContainer";
   
   //Create a new key and persist it in 
   //the key container.  
   RSAPersistKeyInCSP( KeyContainerName );
   
   //Delete the key from the key container.
   RSADeleteKeyInCSP( KeyContainerName );
}
using System;
using System.Security.Cryptography;

class RSACSPSample
{

    static void Main()
    {

        string KeyContainerName = "MyKeyContainer";

        //Create a new key and persist it in
        //the key container.
        RSAPersistKeyInCSP(KeyContainerName);

        //Delete the key from the key container.
        RSADeleteKeyInCSP(KeyContainerName);
    }

    public static void RSAPersistKeyInCSP(string ContainerName)
    {
        try
        {
            // Create a new instance of CspParameters.  Pass
            // 13 to specify a DSA container or 1 to specify
            // an RSA container.  The default is 1.
            CspParameters cspParams = new CspParameters();

            // Specify the container name using the passed variable.
            cspParams.KeyContainerName = ContainerName;

            //Create a new instance of RSACryptoServiceProvider to generate
            //a new key pair.  Pass the CspParameters class to persist the
            //key in the container.  The PersistKeyInCsp property is true by
            //default, allowing the key to be persisted.
            RSACryptoServiceProvider RSAalg = new RSACryptoServiceProvider(cspParams);

            //Indicate that the key was persisted.
            Console.WriteLine("The RSA key was persisted in the container, \"{0}\".", ContainerName);
        }
        catch(CryptographicException e)
        {
            Console.WriteLine(e.Message);
        }
    }

    public static void RSADeleteKeyInCSP(string ContainerName)
    {
        try
        {
            // Create a new instance of CspParameters.  Pass
            // 13 to specify a DSA container or 1 to specify
            // an RSA container.  The default is 1.
            CspParameters cspParams = new CspParameters();

            // Specify the container name using the passed variable.
            cspParams.KeyContainerName = ContainerName;

            //Create a new instance of RSACryptoServiceProvider.
            //Pass the CspParameters class to use the
            //key in the container.
            RSACryptoServiceProvider RSAalg = new RSACryptoServiceProvider(cspParams);

            //Explicitly set the PersistKeyInCsp property to false
            //to delete the key entry in the container.
            RSAalg.PersistKeyInCsp = false;

            //Call Clear to release resources and delete the key from the container.
            RSAalg.Clear();

            //Indicate that the key was persisted.
            Console.WriteLine("The RSA key was deleted from the container, \"{0}\".", ContainerName);
        }
        catch(CryptographicException e)
        {
            Console.WriteLine(e.Message);
        }
    }
}
Imports System.Security.Cryptography

Module RSACSPExample

    Sub Main()

        Dim KeyContainerName As String = "MyKeyContainer"

        'Create a new key and persist it in 
        'the key container.  
        RSAPersistKeyInCSP(KeyContainerName)

        'Delete the key from the key container.
        RSADeleteKeyInCSP(KeyContainerName)
    End Sub


    Sub RSAPersistKeyInCSP(ByVal ContainerName As String)
        Try
            ' Create a new instance of CspParameters.  Pass
            ' 13 to specify a DSA container or 1 to specify
            ' an RSA container.  The default is 1.
            Dim cspParams As New CspParameters

            ' Specify the container name using the passed variable.
            cspParams.KeyContainerName = ContainerName

            'Create a new instance of RSACryptoServiceProvider to generate
            'a new key pair.  Pass the CspParameters class to persist the 
            'key in the container.  The PersistKeyInCsp property is True by 
            'default, allowing the key to be persisted. 
            Dim RSAalg As New RSACryptoServiceProvider(cspParams)

            'Indicate that the key was persisted.
            Console.WriteLine("The RSA key was persisted in the container, ""{0}"".", ContainerName)
        Catch e As CryptographicException
            Console.WriteLine(e.Message)
        End Try
    End Sub


    Sub RSADeleteKeyInCSP(ByVal ContainerName As String)
        Try
            ' Create a new instance of CspParameters.  Pass
            ' 13 to specify a DSA container or 1 to specify
            ' an RSA container.  The default is 1.
            Dim cspParams As New CspParameters

            ' Specify the container name using the passed variable.
            cspParams.KeyContainerName = ContainerName

            'Create a new instance of RSACryptoServiceProvider. 
            'Pass the CspParameters class to use the 
            'key in the container.
            Dim RSAalg As New RSACryptoServiceProvider(cspParams)

            'Explicitly set the PersistKeyInCsp property to False
            'to delete the key entry in the container.
            RSAalg.PersistKeyInCsp = False

            'Call Clear to release resources and delete the key from the container.
            RSAalg.Clear()

            'Indicate that the key was persisted.
            Console.WriteLine("The RSA key was deleted from the container, ""{0}"".", ContainerName)
        Catch e As CryptographicException
            Console.WriteLine(e.Message)
        End Try
    End Sub

End Module

Açıklamalar

Anahtar kapsayıcısında bir anahtarı kalıcı hale getirmek için bu özelliği kullanın.

Bir nesnenin PersistKeyInCsp alanında CspParameters bir anahtar kapsayıcı adı belirttiğinizde KeyContainerName ve oluşturuculardan birini parametresiyle parameters çağırarak bir RSACryptoServiceProvider nesneyi başlatmak için kullandığınızda özelliği otomatik olarak olarak ayarlanırtrue.

PersistKeyInCsp Nesne bir anahtar kapsayıcı adıyla oluşturulduysa RSACryptoServiceProvider özelliğin hiçbir null etkisi olmaz.

Şunlara uygulanır

Ayrıca bkz.