本文共 3755 字,大约阅读时间需要 12 分钟。
The following example demonstrates how to create an asymmetric key, save it in a key container, retrieve the key at a later time, and delete the key from the container.
Notice that that code in the GenKey_SaveInContainer method and the GetKeyFromContainer method is similar. When you specify a key container name for a object and pass it to an object with the property or property set to true, the following occurs. If a key container with the specified name does not exist, then one is created and the key is persisted. If a key container with the specified name does exist, then the key in the container is automatically loaded into the current object. Therefore, the code in the GenKey_SaveInContainer method persists the key because it is run first, while the code in the GetKeyFromContainer method loads the key because it is run second.
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:Key Information A Key retrieved from container :Key Information A Key deleted.Key added to container:Key Information B Key deleted.
转载地址:http://gbhvi.baihongyu.com/