Freigeben über


Gewusst wie: Speichern von asymmetrischen Schlüsseln in einem Schlüsselcontainer

Aktualisiert: November 2007

Asymmetrische private Schlüssel sollten in keinem Fall in vollem Wortlaut oder in Klartext auf dem lokalen Computer gespeichert werden. Wenn ein privater Schlüssel gespeichert werden muss, sollten Sie einen Schlüsselcontainer verwenden. Weitere Informationen über Schlüsselcontainer finden Sie in der Platform SDK-Dokumentation im Abschnitt zur CryptoAPI (https://www.microsoft.com/germany/msdn.).

So erstellen Sie einen asymmetrischen Schlüssel und speichern ihn in einem Schlüsselcontainer

  1. Erstellen SIe eine neue Instanz einer CspParameters-Klasse, und übergeben Sie den Namen des Schlüsselcontainers an das CspParameters.KeyContainerName-Feld.

  2. Erstellen Sie eine neue Instanz einer Klasse, die von der AsymmetricAlgorithm-Klasse abgeleitet ist (normalerweise RSACryptoServiceProvider oder DSACryptoServiceProvider), und übergeben Sie das zuvor erstellte CspParameters-Objekt an den Konstruktor.

So löschen Sie einen Schlüssel aus einem Schlüsselcontainer

  1. Erstellen Sie eine neue Instanz einer CspParameters-Klasse, und übergeben Sie den gewünschten Namen des Schlüsselcontainers an das CspParameters.KeyContainerName-Feld.

  2. Erstellen Sie eine neue Instanz einer Klasse, die von der AsymmetricAlgorithm-Klasse abgeleitet ist (normalerweise RSACryptoServiceProvider oder DSACryptoServiceProvider), und übergeben Sie das zuvor erstellte CspParameters-Objekt an den Konstruktor.

  3. Legen Sie die PersistKeyInCSP-Eigenschaft der von AsymmetricAlgorithm abgeleiteten Klasse auf false (in Visual Basic False) fest.

  4. Rufen Sie die Clear-Methode der von AsymmetricAlgorithm abgeleiteten Klasse auf. Diese Methode gibt alle Ressourcen der Klasse frei und entfernt den Inhalt des Schlüsselcontainers.

Beispiel

Im folgenden Beispiel wird dargestellt, wie ein asymmetrischer Schlüssel erstellt, in einem Schlüsselcontainer gespeichert, zu einem späteren Zeitpunkt abgerufen und aus dem Schlüsselcontainer gelöscht wird.

Beachten Sie, dass der Code in der GenKey_SaveInContainer-Methode und in der GetKeyFromContainer-Methode ähnlich ist. Wenn Sie für ein CspParameters-Objekt einen Schlüsselcontainernamen angeben und ihn an ein AsymmetricAlgorithm-Objekt übergeben, für das die PersistKeyInCsp-Eigenschaft oder die PersistKeyInCsp-Eigenschaft auf true festgelegt ist, tritt Folgendes auf. Wenn kein Schlüsselcontainer mit dem angebenen Namen vorhanden ist, wird ein solcher erstellt und der Schlüssel wird beibehalten. Wenn kein Schlüsselcontainer mit dem angegebenen Namen vorhanden ist, wird der Schlüssel im Container automatisch in das aktuelle AsymmetricAlgorithm-Objekt geladen. Daher behält der Code in der GenKey_SaveInContainer-Methode den Schlüssel bei, da sie zuerst ausgeführt wird, während der Code in der GetKeyFromContainer-Methode den Schlüssel lädt, da sie als zweites ausgeführt wird.

Imports System
Imports System.IO
Imports System.Security.Cryptography
 _

Public Class StoreKey

    Public Shared Sub Main()
        Try
            ' Create a key and save it in a container.
            GenKey_SaveInContainer("MyKeyContainer")

            ' Retrieve the key from the container.
            GetKeyFromContainer("MyKeyContainer")

            ' Delete the key from the container.
            DeleteKeyFromContainer("MyKeyContainer")

            ' Create a key and save it in a container.
            GenKey_SaveInContainer("MyKeyContainer")

            ' Delete the key from the container.
            DeleteKeyFromContainer("MyKeyContainer")
        Catch e As CryptographicException
            Console.WriteLine(e.Message)
        End Try
    End Sub

    Public Shared Sub GenKey_SaveInContainer(ByVal ContainerName As String)
        ' Create the CspParameters object and set the key container 
        ' name used to store the RSA key pair.
        Dim cp As New CspParameters()
        cp.KeyContainerName = ContainerName

        ' Create a new instance of RSACryptoServiceProvider that accesses
        ' the key container MyKeyContainerName.
        Dim rsa As New RSACryptoServiceProvider(cp)

        ' Display the key information to the console.
        Console.WriteLine("Key added to container:  {0}", rsa.ToXmlString(True))
    End Sub

    Public Shared Sub GetKeyFromContainer(ByVal ContainerName As String)
        ' Create the CspParameters object and set the key container 
        '  name used to store the RSA key pair.
        Dim cp As New CspParameters()
        cp.KeyContainerName = ContainerName

        ' Create a new instance of RSACryptoServiceProvider that accesses
        ' the key container MyKeyContainerName.
        Dim rsa As New RSACryptoServiceProvider(cp)

        ' Display the key information to the console.
        Console.WriteLine("Key retrieved from container : {0}", rsa.ToXmlString(True))
    End Sub

    Public Shared Sub DeleteKeyFromContainer(ByVal ContainerName As String)
        ' Create the CspParameters object and set the key container 
        '  name used to store the RSA key pair.
        Dim cp As New CspParameters()
        cp.KeyContainerName = ContainerName

        ' Create a new instance of RSACryptoServiceProvider that accesses
        ' the key container.
        Dim rsa As New RSACryptoServiceProvider(cp)

        ' Delete the key entry in the container.
        rsa.PersistKeyInCsp = False

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

        Console.WriteLine("Key deleted.")
    End Sub
End Class
using System;
using System.IO;
using System.Security.Cryptography;

public class StoreKey

{
    public static void Main()
    {
        try
        {
            // Create a key and save it in a container.
            GenKey_SaveInContainer("MyKeyContainer");
            
            // Retrieve the key from the container.
            GetKeyFromContainer("MyKeyContainer");
    
            // Delete the key from the container.
            DeleteKeyFromContainer("MyKeyContainer");

            // Create a key and save it in a container.
            GenKey_SaveInContainer("MyKeyContainer");

            // Delete the key from the container.
            DeleteKeyFromContainer("MyKeyContainer");
        }
        catch(CryptographicException e)
        {
            Console.WriteLine(e.Message);
        }

    }

    public static void GenKey_SaveInContainer(string ContainerName)
    {
        // Create the CspParameters object and set the key container 
        // name used to store the RSA key pair.
        CspParameters cp = new CspParameters();
        cp.KeyContainerName = ContainerName;

        // Create a new instance of RSACryptoServiceProvider that accesses
        // the key container MyKeyContainerName.
        RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(cp);

        // Display the key information to the console.
        Console.WriteLine("Key added to container: \n  {0}", rsa.ToXmlString(true));
    }

    public static void GetKeyFromContainer(string ContainerName)
    {
        // Create the CspParameters object and set the key container 
        // name used to store the RSA key pair.
        CspParameters cp = new CspParameters();
        cp.KeyContainerName = ContainerName;

        // Create a new instance of RSACryptoServiceProvider that accesses
        // the key container MyKeyContainerName.
        RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(cp);

        // Display the key information to the console.
        Console.WriteLine("Key retrieved from container : \n {0}", rsa.ToXmlString(true));
    }

    public static void DeleteKeyFromContainer(string ContainerName)
    {
        // Create the CspParameters object and set the key container 
        // name used to store the RSA key pair.
        CspParameters cp = new CspParameters();
        cp.KeyContainerName = ContainerName;

        // Create a new instance of RSACryptoServiceProvider that accesses
        // the key container.
        RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(cp);

        // Delete the key entry in the container.
        rsa.PersistKeyInCsp = false;

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

        Console.WriteLine("Key deleted.");
    }
}
Key added to container:
<RSAKeyValue> Key Information A</RSAKeyValue>
Key retrieved from container :
<RSAKeyValue> Key Information A</RSAKeyValue>
Key deleted.
Key added to container:
<RSAKeyValue> Key Information B</RSAKeyValue>
Key deleted.

Siehe auch

Konzepte

Erzeugen von Schlüsseln für die Ver- und Entschlüsselung

Verschlüsseln von Daten

Entschlüsseln von Daten

Weitere Ressourcen

Kryptografische Aufgaben

Kryptografische Dienste