Megjegyzés
Az oldalhoz való hozzáféréshez engedély szükséges. Megpróbálhat bejelentkezni vagy módosítani a címtárat.
Az oldalhoz való hozzáféréshez engedély szükséges. Megpróbálhatja módosítani a címtárat.
Az aszimmetrikus titkos kulcsokat soha nem szabad szó szerint vagy egyszerű szövegben tárolni a helyi számítógépen. Ha titkos kulcsot kell tárolnia, használjon egy kulcstárolót. További információ a kulcstárolókról: A gépszintű és a felhasználói szintű RSA-kulcstárolók ismertetése.
Feljegyzés
A cikkben szereplő kód a Windowsra vonatkozik, és a .NET Core 2.2-ben és a korábbi verziókban nem elérhető funkciókat használ. További információ: dotnet/runtime#23391.
Aszimmetrikus kulcs létrehozása és mentése kulcstárolóba
Hozzon létre egy új osztálypéldányt CspParameters , és adja meg a kulcstárolónak CspParameters.KeyContainerName a mezőnek meghívni kívánt nevet.
Hozzon létre egy új osztálypéldányt, amely az AsymmetricAlgorithm osztályból (általában RSACryptoServiceProvider vagy DSACryptoServiceProvider) származik, és adja át a korábban létrehozott
CspParameters
objektumot a konstruktornak.
Feljegyzés
Az aszimmetrikus kulcs létrehozása és lekérése egyetlen művelet. Ha egy kulcs még nincs a tárolóban, a rendszer a visszaadás előtt hozza létre.
A kulcs törlése a kulcstárolóból
Hozzon létre egy új osztálypéldányt
CspParameters
, és adja meg a kulcstárolónak CspParameters.KeyContainerName a mezőnek meghívni kívánt nevet.Hozzon létre egy új osztálypéldányt, amely az AsymmetricAlgorithm osztályból (általában
RSACryptoServiceProvider
vagyDSACryptoServiceProvider
) származik, és adja át a korábban létrehozottCspParameters
objektumot a konstruktornak.Állítsa be annak az RSACryptoServiceProvider.PersistKeyInCspDSACryptoServiceProvider.PersistKeyInCsp osztálynak a tulajdonságát vagy tulajdonságát, amely a (Visual Basic)-ből
AsymmetricAlgorithm
false
False
származik.Hívja meg annak az
Clear
osztálynak a metódusát, amelyből származikAsymmetricAlgorithm
. Ez a metódus felszabadítja az osztály összes erőforrását, és törli a kulcstárolót.
Példa
Az alábbi példa bemutatja, hogyan hozhat létre aszimmetrikus kulcsot, mentheti egy kulcstárolóba, lekérheti a kulcsot egy későbbi időpontban, és törölheti a kulcsot a tárolóból.
Figyelje meg, hogy a metódus és a GenKey_SaveInContainer
GetKeyFromContainer
metódus kódja hasonló. Ha egy objektum kulcstárolójának nevét CspParameters adja meg, és átadja egy AsymmetricAlgorithm objektumnak, PersistKeyInCsp amelynek tulajdonsága vagy PersistKeyInCsp tulajdonsága a true
következő, a viselkedés a következő:
- Ha a megadott névvel rendelkező kulcstároló nem létezik, akkor létrejön egy, és a kulcs megmarad.
- Ha létezik egy megadott nevű kulcstároló, a rendszer automatikusan betölti a tárolóban lévő kulcsot az aktuális AsymmetricAlgorithm objektumba.
Ezért a metódus kódja megőrzi a GenKey_SaveInContainer
kulcsot, mert először fut, míg a GetKeyFromContainer
metódus kódja betölti a kulcsot, mert a második futtatás.
Imports System
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
Private 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 parameters As New CspParameters With {
.KeyContainerName = ContainerName
}
' Create a new instance of RSACryptoServiceProvider that accesses
' the key container MyKeyContainerName.
Using rsa As New RSACryptoServiceProvider(parameters)
' Display the key information to the console.
Console.WriteLine($"Key added to container: {rsa.ToXmlString(True)}")
End Using
End Sub
Private 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 parameters As New CspParameters With {
.KeyContainerName = ContainerName
}
' Create a new instance of RSACryptoServiceProvider that accesses
' the key container MyKeyContainerName.
Using rsa As New RSACryptoServiceProvider(parameters)
' Display the key information to the console.
Console.WriteLine($"Key retrieved from container : {rsa.ToXmlString(True)}")
End Using
End Sub
Private 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 parameters As New CspParameters With {
.KeyContainerName = ContainerName
}
' Create a new instance of RSACryptoServiceProvider that accesses
' the key container.
' Delete the key entry in the container.
Dim rsa As New RSACryptoServiceProvider(parameters) With {
.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.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);
}
}
private static void GenKey_SaveInContainer(string containerName)
{
// Create the CspParameters object and set the key container
// name used to store the RSA key pair.
var parameters = new CspParameters
{
KeyContainerName = containerName
};
// Create a new instance of RSACryptoServiceProvider that accesses
// the key container MyKeyContainerName.
using var rsa = new RSACryptoServiceProvider(parameters);
// Display the key information to the console.
Console.WriteLine($"Key added to container: \n {rsa.ToXmlString(true)}");
}
private static void GetKeyFromContainer(string containerName)
{
// Create the CspParameters object and set the key container
// name used to store the RSA key pair.
var parameters = new CspParameters
{
KeyContainerName = containerName
};
// Create a new instance of RSACryptoServiceProvider that accesses
// the key container MyKeyContainerName.
using var rsa = new RSACryptoServiceProvider(parameters);
// Display the key information to the console.
Console.WriteLine($"Key retrieved from container : \n {rsa.ToXmlString(true)}");
}
private static void DeleteKeyFromContainer(string containerName)
{
// Create the CspParameters object and set the key container
// name used to store the RSA key pair.
var parameters = new CspParameters
{
KeyContainerName = containerName
};
// Create a new instance of RSACryptoServiceProvider that accesses
// the key container.
using var rsa = new RSACryptoServiceProvider(parameters)
{
// Delete the key entry in the container.
PersistKeyInCsp = false
};
// Call Clear to release resources and delete the key from the container.
rsa.Clear();
Console.WriteLine("Key deleted.");
}
}
A kimenet a következő:
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.