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 CspParameters object and pass it to an AsymmetricAlgorithm object with the PersistKeyInCsp property orPersistKeyInCsp 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 AsymmetricAlgorithm 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: <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.