CngKey.Open Method

Definition

Creates an instance of an CngKey object that represents an existing key.

Overloads

Open(String)

Creates an instance of an CngKey object that represents an existing named key.

Open(SafeNCryptKeyHandle, CngKeyHandleOpenOptions)

Creates an instance of an CngKey object by using a handle to an existing key.

Open(String, CngProvider)

Creates an instance of an CngKey object that represents an existing named key, using the specified key storage provider (KSP).

Open(String, CngProvider, CngKeyOpenOptions)

Creates an instance of an CngKey object that represents an existing named key, using the specified key storage provider (KSP) and key open options.

Open(String)

Source:
Cng.NotSupported.cs
Source:
Cng.NotSupported.cs
Source:
Cng.NotSupported.cs

Creates an instance of an CngKey object that represents an existing named key.

C#
[System.Runtime.Versioning.SupportedOSPlatform("windows")]
public static System.Security.Cryptography.CngKey Open(string keyName);
C#
public static System.Security.Cryptography.CngKey Open(string keyName);

Parameters

keyName
String

The name of the key.

Returns

An existing key.

Attributes

Exceptions

keyName is null.

Cryptography Next Generation (CNG) is not supported on this system.

All other errors.

Remarks

This overload uses the following defaults:

Applies to

.NET 10 and other versions
Product Versions
.NET Core 1.0, Core 1.1, 6, 7, 8, 9, 10
.NET Framework 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 (package-provided), 2.1 (package-provided)
Windows Desktop 3.0, 3.1, 5

Open(SafeNCryptKeyHandle, CngKeyHandleOpenOptions)

Source:
Cng.NotSupported.cs
Source:
Cng.NotSupported.cs
Source:
Cng.NotSupported.cs

Creates an instance of an CngKey object by using a handle to an existing key.

C#
[System.Runtime.Versioning.SupportedOSPlatform("windows")]
public static System.Security.Cryptography.CngKey Open(Microsoft.Win32.SafeHandles.SafeNCryptKeyHandle keyHandle, System.Security.Cryptography.CngKeyHandleOpenOptions keyHandleOpenOptions);
C#
public static System.Security.Cryptography.CngKey Open(Microsoft.Win32.SafeHandles.SafeNCryptKeyHandle keyHandle, System.Security.Cryptography.CngKeyHandleOpenOptions keyHandleOpenOptions);
C#
[System.Security.SecurityCritical]
public static System.Security.Cryptography.CngKey Open(Microsoft.Win32.SafeHandles.SafeNCryptKeyHandle keyHandle, System.Security.Cryptography.CngKeyHandleOpenOptions keyHandleOpenOptions);

Parameters

keyHandle
SafeNCryptKeyHandle

A handle to an existing key.

keyHandleOpenOptions
CngKeyHandleOpenOptions

One of the enumeration values that indicates whether keyHandle represents an ephemeral key or a named key.

Returns

An existing key.

Attributes

Exceptions

keyHandle is null.

keyHandle is invalid or malformed, or it is already closed. This exception is also thrown if the key is an ephemeral key that is created by the common language runtime (CLR), but the EphemeralKey value is not specified.

Cryptography Next Generation (CNG) is not supported on this system.

All other errors.

Remarks

This overload enables advanced users to use the CNG classes even if they rely on platform invoke or the managed interop capability of C++. For example, if you have a native library that returns the key as an NCRYPT_KEY_HANDLE, this overload lets you create a managed wrapper around the key and use the CNG classes to manipulate it.

When you open a key by using its handle, you cannot determine the key's ephemeral state and must specify it yourself. Several properties of the CngKey class rely on this value to be correct. Therefore, make sure that you pass the correct flag value to the Open methods.

Applies to

.NET 10 and other versions
Product Versions
.NET Core 1.0, Core 1.1, 6, 7, 8, 9, 10
.NET Framework 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 (package-provided), 2.1 (package-provided)
Windows Desktop 3.0, 3.1, 5

Open(String, CngProvider)

Source:
Cng.NotSupported.cs
Source:
Cng.NotSupported.cs
Source:
Cng.NotSupported.cs

Creates an instance of an CngKey object that represents an existing named key, using the specified key storage provider (KSP).

C#
[System.Runtime.Versioning.SupportedOSPlatform("windows")]
public static System.Security.Cryptography.CngKey Open(string keyName, System.Security.Cryptography.CngProvider provider);
C#
public static System.Security.Cryptography.CngKey Open(string keyName, System.Security.Cryptography.CngProvider provider);

Parameters

keyName
String

The name of the key.

provider
CngProvider

The KSP that contains the key.

Returns

An existing key.

Attributes

Exceptions

keyName or provider is null.

Cryptography Next Generation (CNG) is not supported on this system.

All other errors.

Remarks

Unlike the Open(String) method overload, this overload lets you specify the provider. In addition, a default CngKeyOpenOptions value of None is created and bound to the key.

Applies to

.NET 10 and other versions
Product Versions
.NET Core 1.0, Core 1.1, 6, 7, 8, 9, 10
.NET Framework 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 (package-provided), 2.1 (package-provided)
Windows Desktop 3.0, 3.1, 5

Open(String, CngProvider, CngKeyOpenOptions)

Source:
Cng.NotSupported.cs
Source:
Cng.NotSupported.cs
Source:
Cng.NotSupported.cs

Creates an instance of an CngKey object that represents an existing named key, using the specified key storage provider (KSP) and key open options.

C#
[System.Runtime.Versioning.SupportedOSPlatform("windows")]
public static System.Security.Cryptography.CngKey Open(string keyName, System.Security.Cryptography.CngProvider provider, System.Security.Cryptography.CngKeyOpenOptions openOptions);
C#
public static System.Security.Cryptography.CngKey Open(string keyName, System.Security.Cryptography.CngProvider provider, System.Security.Cryptography.CngKeyOpenOptions openOptions);
C#
[System.Security.SecurityCritical]
public static System.Security.Cryptography.CngKey Open(string keyName, System.Security.Cryptography.CngProvider provider, System.Security.Cryptography.CngKeyOpenOptions openOptions);

Parameters

keyName
String

The name of the key.

provider
CngProvider

The KSP that contains the key.

openOptions
CngKeyOpenOptions

A bitwise combination of the enumeration values that specify options for opening the key, such as where the key is opened from (machine or user storage) and whether to suppress UI prompting.

Returns

An existing key.

Attributes

Exceptions

keyName or provider is null.

Cryptography Next Generation (CNG) is not supported on this system.

All other errors.

Remarks

Unlike the Open(String) method overload, this overload lets you specify both the provider and the key open options.

Applies to

.NET 10 and other versions
Product Versions
.NET Core 1.0, Core 1.1, 6, 7, 8, 9, 10
.NET Framework 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 (package-provided), 2.1 (package-provided)
Windows Desktop 3.0, 3.1, 5