RSACryptoServiceProvider.CspKeyContainerInfo Property

Definition

Gets a CspKeyContainerInfo object that describes additional information about a cryptographic key pair.

C#
[System.Runtime.Versioning.SupportedOSPlatform("windows")]
public System.Security.Cryptography.CspKeyContainerInfo CspKeyContainerInfo { get; }
C#
public System.Security.Cryptography.CspKeyContainerInfo CspKeyContainerInfo { get; }
C#
[System.Runtime.InteropServices.ComVisible(false)]
public System.Security.Cryptography.CspKeyContainerInfo CspKeyContainerInfo { get; }

Property Value

A CspKeyContainerInfo object that describes additional information about a cryptographic key pair.

Implements

Attributes

Examples

The following code example demonstrates how to call the CspKeyContainerInfo property.

C#
using System;
using System.Security.Cryptography;
using System.Text;

public class CspKeyContainerInfoExample
{

    public static void Main(String[] args)
    {
        RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();

        try
        {
            // Note: In cases where a random key is generated,
            // a key container is not created until you call
            // a method that uses the key.  This example calls
            // the Encrypt method before calling the
            // CspKeyContainerInfo property so that a key
            // container is created.

            // Create some data to encrypt and display it.
            string data = "Here is some data to encrypt.";

            Console.WriteLine("Data to encrypt: " + data);

            // Convert the data to an array of bytes and
            // encrypt it.
            byte[] byteData = Encoding.ASCII.GetBytes(data);

            byte[] encData = rsa.Encrypt(byteData, false);

            // Display the encrypted value.
            Console.WriteLine("Encrypted Data: " + Encoding.ASCII.GetString(encData));

            Console.WriteLine();

            Console.WriteLine("CspKeyContainerInfo information:");

            Console.WriteLine();

            // Create a new CspKeyContainerInfo object.
            CspKeyContainerInfo keyInfo = rsa.CspKeyContainerInfo;

            // Display the value of each property.

            Console.WriteLine("Accessible property: " + keyInfo.Accessible);

            Console.WriteLine("Exportable property: " + keyInfo.Exportable);

            Console.WriteLine("HardwareDevice property: " + keyInfo.HardwareDevice);

            Console.WriteLine("KeyContainerName property: " + keyInfo.KeyContainerName);

            Console.WriteLine("KeyNumber property: " + keyInfo.KeyNumber.ToString());

            Console.WriteLine("MachineKeyStore property: " + keyInfo.MachineKeyStore);

            Console.WriteLine("Protected property: " + keyInfo.Protected);

            Console.WriteLine("ProviderName property: " + keyInfo.ProviderName);

            Console.WriteLine("ProviderType property: " + keyInfo.ProviderType);

            Console.WriteLine("RandomlyGenerated property: " + keyInfo.RandomlyGenerated);

            Console.WriteLine("Removable property: " + keyInfo.Removable);

            Console.WriteLine("UniqueKeyContainerName property: " + keyInfo.UniqueKeyContainerName);
        }
        catch (Exception e)
        {
            Console.WriteLine(e.ToString());
        }
        finally
        {
            // Clear the key.
            rsa.Clear();
        }
    }
}

Remarks

Use the CspKeyContainerInfo property to retrieve additional information about a cryptographic key pair. The returned CspKeyContainerInfo object describes whether the key is exportable, and specifies the key container name, information about the provider, and other information.

In cases where a random key is generated, a key container will not be created until you call a method that uses the key. Some properties of the CspKeyContainerInfo object returned by the CspKeyContainerInfo property will throw a CryptographicException if a key container has not been created. To make sure that a key container has been created, call a method such as Encrypt, Decrypt, SignData, SignHash, and so on, before you call the CspKeyContainerInfo property.

Applies to

Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

See also