Export (0) Print
Expand All
0 out of 1 rated this helpful - Rate this topic

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 */
public boolean get_PersistKeyInCsp ()

/** @property */
public void set_PersistKeyInCsp (boolean value)

public function get PersistKeyInCsp () : boolean

public function set PersistKeyInCsp (value : boolean)

Property Value

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 a CspParameters object and use it to initialize an RSACryptoServiceProvider object. You can specify a container name using the KeyContainerName field.

If you set the PersistKeyInCsp property to true without initializing the RSACryptoServiceProvider object with a CspParameters object, a random key container name prepended with "CLR" is created.

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

        }
    }
}

import System.*;
import System.Security.Cryptography.*;

class RSACSPSample
{
    public static void main(String args[])
    {
        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);
    } //main

    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.get_Message());
        }
    } //RSAPersistKeyInCSP

    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.set_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.get_Message());
        }
    } //RSADeleteKeyInCSP
} //RSACSPSample

  • KeyContainerPermission  for permission to delete a key. Associated enumeration: Delete. Security action: Demand. -or- for permission to create a key. Associated enumeration: Create. Security action: Demand.

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

.NET Framework

Supported in: 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0
Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.