Freigeben über


RSACryptoServiceProvider.PersistKeyInCsp-Eigenschaft

Ruft einen Wert ab, der angibt, ob der Schlüssel im Kryptografiedienstanbieter (Cryptographic Service Provider, CSP) beibehalten werden soll, oder legt diesen fest.

Namespace: System.Security.Cryptography
Assembly: mscorlib (in mscorlib.dll)

Syntax

'Declaration
Public Property PersistKeyInCsp As Boolean
'Usage
Dim instance As RSACryptoServiceProvider
Dim value As Boolean

value = instance.PersistKeyInCsp

instance.PersistKeyInCsp = value
public bool PersistKeyInCsp { get; set; }
public:
property bool PersistKeyInCsp {
    bool get ();
    void set (bool value);
}
/** @property */
public boolean get_PersistKeyInCsp ()

/** @property */
public void set_PersistKeyInCsp (boolean value)
public function get PersistKeyInCsp () : boolean

public function set PersistKeyInCsp (value : boolean)

Eigenschaftenwert

true, wenn der Schlüssel im CSP beibehalten werden soll, andernfalls false.

Hinweise

Mit dieser Eigenschaft kann ein Schlüssel in einem Schlüsselcontainer beibehalten werden.

Die PersistKeyInCsp-Eigenschaft wird automatisch auf true festgelegt, wenn Sie in einem CspParameters-Objekt einen Schlüsselcontainernamen angeben und mit diesem ein RSACryptoServiceProvider-Objekt initialisieren. Sie können mit dem KeyContainerName-Feld einen Containernamen angeben.

Wenn Sie die PersistKeyInCsp-Eigenschaft auf true festlegen, ohne das RSACryptoServiceProvider-Objekt mit einem CspParameters-Objekt zu initialisieren, wird ein zufälliger Schlüsselcontainername erstellt, dem „CLR" vorangestellt wird.

Beispiel

Im folgenden Codebeispiel wird ein RSACryptoServiceProvider-Objekt erstellt und der Schlüssel in einem Schlüsselcontainer beibehalten.

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
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);

        }
    }
}
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 );
}
import System.*;
import System.Security.Cryptography.*;

class RSACSPSample
{
    public static void main(String args[])
    {
        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);
    } //main

    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.get_Message());
        }
    } //RSAPersistKeyInCSP

    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.set_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.get_Message());
        }
    } //RSADeleteKeyInCSP
} //RSACSPSample

.NET Framework-Sicherheit

  • KeyContainerPermission  für die Berechtigung zum Löschen eines Schlüssels. Zugeordnete Enumeration: Delete. Sicherheitsaktion: Demand. – oder – für die Berechtigung zum Erstellen eines Schlüssels. Zugeordnete Enumeration: Create. Sicherheitsaktion: Demand.

Plattformen

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile für Pocket PC, Windows Mobile für Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

.NET Framework unterstützt nicht alle Versionen sämtlicher Plattformen. Eine Liste der unterstützten Versionen finden Sie unter Systemanforderungen.

Versionsinformationen

.NET Framework

Unterstützt in: 2.0, 1.1, 1.0

.NET Compact Framework

Unterstützt in: 2.0

Siehe auch

Referenz

RSACryptoServiceProvider-Klasse
RSACryptoServiceProvider-Member
System.Security.Cryptography-Namespace

Weitere Ressourcen

Kryptografische Dienste