RSACryptoServiceProvider.PersistKeyInCsp Property

 

Gets or sets a value indicating whether the key should be persisted in the cryptographic service provider (CSP).

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

public bool PersistKeyInCsp { get; set; }

Property Value

Type: System.Boolean

true if the key should be persisted in the CSP; otherwise, false.

Use this property to persist a key in a key container.

The PersistKeyInCsp property is automatically set to true when you specify a key container name in the KeyContainerName field of a CspParameters object and use it to initialize an RSACryptoServiceProvider object by calling one of the constructors with a parameters parameter.

The PersistKeyInCsp property has no effect if the RSACryptoServiceProvider object is created with a null key container name.

The following code example creates an RSACryptoServiceProvider object and persists the key to a key container.

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

        }
    }
}

KeyContainerPermissionAccessEntryCollection

for permission to delete a key. Associated enumeration: KeyContainerPermissionFlags.Delete

-or-

for permission to create a key. Associated enumeration: KeyContainerPermissionFlags.Create

.NET Framework
Available since 1.1
Windows Phone Silverlight
Available since 7.1
Return to top
Show: