Skip to content

OnixLabs.Security.Cryptography.EcdhPrivateKey

matthew edited this page May 27, 2026 · 2 revisions

EcdhPrivateKey Class

Represents an EC Diffie-Hellman cryptographic private key used for shared-secret key agreement.

Namespace OnixLabs.Security.Cryptography
Assembly OnixLabs.Security.Cryptography.dll (NuGet)
Inheritance object → PrivateKey → EcdhPrivateKey
Implements IEcdhPrivateKey, ISpanParsable<EcdhPrivateKey>, IPrivateKeyDerivable<EcdhPublicKey>, IPrivateKeyImportable<EcdhPrivateKey>, IPrivateKeyExportable, IBinaryConvertible, IDisposable

Definition

public sealed partial class EcdhPrivateKey : PrivateKey, IEcdhPrivateKey, ISpanParsable<EcdhPrivateKey>

Remarks

EcdhPrivateKey is the concrete private-key type for the Elliptic Curve Diffie-Hellman (ECDH) key-agreement protocol. Unlike signing-oriented keys such as EcdsaPrivateKey or EddsaPrivateKey, an ECDH private key is used exclusively for shared-secret derivation: two parties each hold their own key pair, exchange public keys, and call DeriveSharedSecret to independently arrive at the same Secret value without the secret ever being transmitted. The resulting Secret is typically used as input to a key-derivation function (KDF) before it is used for symmetric encryption.

Obtaining instances — The recommended way to create a new key pair is Create(), which generates a fresh key on the platform default curve (P-256 on most runtimes). To target a specific curve, pass an ECCurve to Create(ECCurve), or supply explicit ECParameters to Create(ECParameters). Existing raw EC private key bytes (SEC 1 / RFC 5915 DER format) can be loaded via Import; PKCS #8 DER bytes via ImportPkcs8; and RFC 7468 PEM text via ImportPem. Hex- or Base64-encoded key strings can be parsed back via Parse / TryParse.

Deriving the public key — Call GetPublicKey() to obtain the corresponding EcdhPublicKey. The resulting public key is safe to share with the remote party.

Shared-secret derivation — Call DeriveSharedSecret(IEcdhPublicKey) with the remote party's EcdhPublicKey. Both parties performing the same operation with their respective private keys and each other's public keys produce identical Secret bytes. The raw key material is zeroed from memory immediately after the Secret is constructed.

Export formats — EcdhPrivateKey supports four export formats:

Encrypted PKCS #8 variants accept a ReadOnlySpan<char> or ReadOnlySpan<byte> password together with a PbeParameters descriptor.

Named key — ToNamedPrivateKey() wraps this instance in a NamedPrivateKey tagged with the algorithm name "ECDH".

Memory safety — EcdhPrivateKey inherits PrivateKey's ProtectedData store, which encrypts key bytes at rest. Key material is only decrypted in-process during operations that require it, and it is scrubbed on disposal. Always dispose EcdhPrivateKey instances (or use a using statement).

Equality comparisons, operators, ToString, AsReadOnlySpan, AsReadOnlyMemory, and Dispose semantics are all inherited from PrivateKey and are documented there.

Examples

Generating a key pair and deriving a shared secret

using OnixLabs.Security.Cryptography;

// Alice generates her ECDH key pair.
using EcdhPrivateKey alicePrivateKey = EcdhPrivateKey.Create();
EcdhPublicKey alicePublicKey = alicePrivateKey.GetPublicKey();

// Bob generates his ECDH key pair.
using EcdhPrivateKey bobPrivateKey = EcdhPrivateKey.Create();
EcdhPublicKey bobPublicKey = bobPrivateKey.GetPublicKey();

// Each party derives the same shared secret independently.
using Secret aliceSecret = alicePrivateKey.DeriveSharedSecret(bobPublicKey);
using Secret bobSecret   = bobPrivateKey.DeriveSharedSecret(alicePublicKey);

// Both secrets are identical.
Console.WriteLine(aliceSecret == bobSecret); // True

Importing a key from PEM and exporting to PKCS #8

using OnixLabs.Security.Cryptography;

// Import from an existing PEM-encoded EC private key.
using EcdhPrivateKey privateKey = EcdhPrivateKey.ImportPem(pemText);

// Re-export as encrypted PKCS #8 PEM.
string encryptedPem = privateKey.ExportPkcs8Pem(
    password: "my-pass".AsSpan(),
    new System.Security.Cryptography.PbeParameters(
        System.Security.Cryptography.PbeEncryptionAlgorithm.Aes256Cbc,
        System.Security.Cryptography.HashAlgorithmName.SHA256,
        iterationCount: 100_000));

Console.WriteLine(encryptedPem);

Wrapping in a NamedPrivateKey

using OnixLabs.Security.Cryptography;

using EcdhPrivateKey privateKey = EcdhPrivateKey.Create();
NamedPrivateKey named = privateKey.ToNamedPrivateKey();

Console.WriteLine(named.AlgorithmName); // ECDH
Console.WriteLine(named);               // ECDH:<hex-encoded key bytes>

Constructors

Constructor Description
EcdhPrivateKey(ReadOnlySpan<byte>) Initializes a new instance from a span of bytes.
EcdhPrivateKey(ReadOnlyMemory<byte>) Initializes a new instance from a memory region.
EcdhPrivateKey(ReadOnlySequence<byte>) Initializes a new instance from a byte sequence.

EcdhPrivateKey(ReadOnlySpan<byte>)

public EcdhPrivateKey(ReadOnlySpan<byte> keyData)

Initializes a new instance of the EcdhPrivateKey class. The provided span is copied into the base-class ProtectedData store; the span itself is not retained.

Parameter Type Description
keyData ReadOnlySpan<byte> The underlying key data of the EC Diffie-Hellman cryptographic private key.

EcdhPrivateKey(ReadOnlyMemory<byte>)

public EcdhPrivateKey(ReadOnlyMemory<byte> keyData)

Initializes a new instance of the EcdhPrivateKey class. The provided memory region is copied into the base-class ProtectedData store; the original memory is not retained.

Parameter Type Description
keyData ReadOnlyMemory<byte> The underlying key data of the EC Diffie-Hellman cryptographic private key.

EcdhPrivateKey(ReadOnlySequence<byte>)

public EcdhPrivateKey(ReadOnlySequence<byte> keyData)

Initializes a new instance of the EcdhPrivateKey class. The provided byte sequence is copied into the base-class ProtectedData store; the original sequence is not retained.

Parameter Type Description
keyData ReadOnlySequence<byte> The underlying key data of the EC Diffie-Hellman cryptographic private key.

Static Methods

Method Returns Description
Create() EcdhPrivateKey Creates a new ECDH private key on the platform default curve.
Create(ECCurve) EcdhPrivateKey Creates a new ECDH private key on the specified elliptic curve.
Create(ECParameters) EcdhPrivateKey Creates a new ECDH private key from the specified EC parameters.
Import(IBinaryConvertible) EcdhPrivateKey Imports a raw EC private key from an IBinaryConvertible value.
Import(IBinaryConvertible, out int) EcdhPrivateKey Imports a raw EC private key from an IBinaryConvertible value and reports the number of bytes consumed.
Import(ReadOnlySpan<byte>) EcdhPrivateKey Imports a raw EC private key from a span of bytes.
Import(ReadOnlySpan<byte>, out int) EcdhPrivateKey Imports a raw EC private key from a span of bytes and reports the number of bytes consumed.
ImportPem(ReadOnlySpan<char>) EcdhPrivateKey Imports a key from RFC 7468 PEM text.
ImportPem(ReadOnlySpan<char>, ReadOnlySpan<char>) EcdhPrivateKey Imports a key from RFC 7468 PEM text encrypted with a character-based password.
ImportPem(ReadOnlySpan<char>, ReadOnlySpan<byte>) EcdhPrivateKey Imports a key from RFC 7468 PEM text encrypted with a byte-based password.
ImportPkcs8(IBinaryConvertible) EcdhPrivateKey Imports an unencrypted PKCS #8 private key from an IBinaryConvertible value.
ImportPkcs8(IBinaryConvertible, ReadOnlySpan<char>) EcdhPrivateKey Imports an encrypted PKCS #8 private key from an IBinaryConvertible value using a character-based password.
ImportPkcs8(IBinaryConvertible, ReadOnlySpan<byte>) EcdhPrivateKey Imports an encrypted PKCS #8 private key from an IBinaryConvertible value using a byte-based password.
ImportPkcs8(IBinaryConvertible, out int) EcdhPrivateKey Imports an unencrypted PKCS #8 private key from an IBinaryConvertible value and reports bytes consumed.
ImportPkcs8(IBinaryConvertible, ReadOnlySpan<char>, out int) EcdhPrivateKey Imports an encrypted PKCS #8 private key from an IBinaryConvertible value (char password) and reports bytes consumed.
ImportPkcs8(IBinaryConvertible, ReadOnlySpan<byte>, out int) EcdhPrivateKey Imports an encrypted PKCS #8 private key from an IBinaryConvertible value (byte password) and reports bytes consumed.
ImportPkcs8(ReadOnlySpan<byte>) EcdhPrivateKey Imports an unencrypted PKCS #8 private key from a span of bytes.
ImportPkcs8(ReadOnlySpan<byte>, ReadOnlySpan<char>) EcdhPrivateKey Imports an encrypted PKCS #8 private key from a span using a character-based password.
ImportPkcs8(ReadOnlySpan<byte>, ReadOnlySpan<byte>) EcdhPrivateKey Imports an encrypted PKCS #8 private key from a span using a byte-based password.
ImportPkcs8(ReadOnlySpan<byte>, out int) EcdhPrivateKey Imports an unencrypted PKCS #8 private key from a span and reports bytes consumed.
ImportPkcs8(ReadOnlySpan<byte>, ReadOnlySpan<char>, out int) EcdhPrivateKey Imports an encrypted PKCS #8 private key from a span (char password) and reports bytes consumed.
ImportPkcs8(ReadOnlySpan<byte>, ReadOnlySpan<byte>, out int) EcdhPrivateKey Imports an encrypted PKCS #8 private key from a span (byte password) and reports bytes consumed.
Parse(string, IFormatProvider?) EcdhPrivateKey Parses an encoded string into an EcdhPrivateKey.
Parse(ReadOnlySpan<char>, IFormatProvider?) EcdhPrivateKey Parses an encoded character span into an EcdhPrivateKey.
TryParse(string?, IFormatProvider?, out EcdhPrivateKey) bool Attempts to parse an encoded string into an EcdhPrivateKey.
TryParse(ReadOnlySpan<char>, IFormatProvider?, out EcdhPrivateKey) bool Attempts to parse an encoded character span into an EcdhPrivateKey.

Create()

public static EcdhPrivateKey Create()

Creates a new EC Diffie-Hellman cryptographic private key on the platform default elliptic curve (typically NIST P-256).

Returns — A new EcdhPrivateKey instance.

Create(ECCurve)

public static EcdhPrivateKey Create(ECCurve curve)

Creates a new EC Diffie-Hellman cryptographic private key on the specified elliptic curve.

Parameter Type Description
curve ECCurve The elliptic curve from which to create the key.

Returns — A new EcdhPrivateKey instance.

Exceptions

Exception Condition
CryptographicException curve does not represent a valid elliptic curve.

Create(ECParameters)

public static EcdhPrivateKey Create(ECParameters parameters)

Creates a new EC Diffie-Hellman cryptographic private key from the specified EC parameters.

Parameter Type Description
parameters ECParameters The elliptic curve parameters from which to create the key.

Returns — A new EcdhPrivateKey instance.

Exceptions

Exception Condition
CryptographicException parameters does not represent a valid EC Diffie-Hellman private key.

Import(IBinaryConvertible)

public static EcdhPrivateKey Import(IBinaryConvertible data)

Imports a raw EC private key (SEC 1 / RFC 5915 DER format) from an IBinaryConvertible value.

Parameter Type Description
data IBinaryConvertible The binary-convertible value containing the raw EC private key bytes to import.

Returns — A new EcdhPrivateKey instance.

Exceptions

Exception Condition
CryptographicException data does not represent a valid EC private key.

Import(IBinaryConvertible, out int)

public static EcdhPrivateKey Import(IBinaryConvertible data, out int bytesRead)

Imports a raw EC private key from an IBinaryConvertible value and reports the number of bytes consumed.

Parameter Type Description
data IBinaryConvertible The binary-convertible value containing the raw EC private key bytes to import.
bytesRead out int When this method returns, contains the number of bytes read from data.

Returns — A new EcdhPrivateKey instance.

Exceptions

Exception Condition
CryptographicException data does not represent a valid EC private key.

Import(ReadOnlySpan<byte>)

public static EcdhPrivateKey Import(ReadOnlySpan<byte> data)

Imports a raw EC private key (SEC 1 / RFC 5915 DER format) from a span of bytes.

Parameter Type Description
data ReadOnlySpan<byte> The span containing the raw EC private key bytes to import.

Returns — A new EcdhPrivateKey instance.

Exceptions

Exception Condition
CryptographicException data does not represent a valid EC private key.

Import(ReadOnlySpan<byte>, out int)

public static EcdhPrivateKey Import(ReadOnlySpan<byte> data, out int bytesRead)

Imports a raw EC private key from a span of bytes and reports the number of bytes consumed.

Parameter Type Description
data ReadOnlySpan<byte> The span containing the raw EC private key bytes to import.
bytesRead out int When this method returns, contains the number of bytes read from data.

Returns — A new EcdhPrivateKey instance.

Exceptions

Exception Condition
CryptographicException data does not represent a valid EC private key.

ImportPem(ReadOnlySpan<char>)

public static EcdhPrivateKey ImportPem(ReadOnlySpan<char> data)

Imports a key from RFC 7468 PEM-encoded text. Supports EC PRIVATE KEY and PRIVATE KEY (PKCS #8) labels.

Parameter Type Description
data ReadOnlySpan<char> The PEM-encoded character span to import.

Returns — A new EcdhPrivateKey instance.

Exceptions

Exception Condition
CryptographicException The PEM data is malformed or does not represent a valid ECDH private key.

ImportPem(ReadOnlySpan<char>, ReadOnlySpan<char>)

public static EcdhPrivateKey ImportPem(ReadOnlySpan<char> data, ReadOnlySpan<char> password)

Imports a key from encrypted RFC 7468 PEM text using a character-based password.

Parameter Type Description
data ReadOnlySpan<char> The encrypted PEM-encoded character span to import.
password ReadOnlySpan<char> The password used to decrypt the PEM data.

Returns — A new EcdhPrivateKey instance.

Exceptions

Exception Condition
CryptographicException The PEM data is malformed, the password is incorrect, or the key is not a valid ECDH private key.

ImportPem(ReadOnlySpan<char>, ReadOnlySpan<byte>)

public static EcdhPrivateKey ImportPem(ReadOnlySpan<char> data, ReadOnlySpan<byte> password)

Imports a key from encrypted RFC 7468 PEM text using a byte-based password.

Parameter Type Description
data ReadOnlySpan<char> The encrypted PEM-encoded character span to import.
password ReadOnlySpan<byte> The password bytes used to decrypt the PEM data.

Returns — A new EcdhPrivateKey instance.

Exceptions

Exception Condition
CryptographicException The PEM data is malformed, the password is incorrect, or the key is not a valid ECDH private key.

ImportPkcs8(IBinaryConvertible)

public static EcdhPrivateKey ImportPkcs8(IBinaryConvertible data)

Imports an unencrypted PKCS #8 private key from an IBinaryConvertible value.

Parameter Type Description
data IBinaryConvertible The binary-convertible value containing the PKCS #8 DER bytes.

Returns — A new EcdhPrivateKey instance.

Exceptions

Exception Condition
CryptographicException data does not represent a valid PKCS #8 ECDH private key.

ImportPkcs8(IBinaryConvertible, ReadOnlySpan<char>)

public static EcdhPrivateKey ImportPkcs8(IBinaryConvertible data, ReadOnlySpan<char> password)

Imports an encrypted PKCS #8 private key from an IBinaryConvertible value using a character-based password.

Parameter Type Description
data IBinaryConvertible The binary-convertible value containing the encrypted PKCS #8 DER bytes.
password ReadOnlySpan<char> The password used to decrypt the key.

Returns — A new EcdhPrivateKey instance.

Exceptions

Exception Condition
CryptographicException data is malformed, or the password is incorrect.

ImportPkcs8(IBinaryConvertible, ReadOnlySpan<byte>)

public static EcdhPrivateKey ImportPkcs8(IBinaryConvertible data, ReadOnlySpan<byte> password)

Imports an encrypted PKCS #8 private key from an IBinaryConvertible value using a byte-based password.

Parameter Type Description
data IBinaryConvertible The binary-convertible value containing the encrypted PKCS #8 DER bytes.
password ReadOnlySpan<byte> The password bytes used to decrypt the key.

Returns — A new EcdhPrivateKey instance.

Exceptions

Exception Condition
CryptographicException data is malformed, or the password is incorrect.

ImportPkcs8(IBinaryConvertible, out int)

public static EcdhPrivateKey ImportPkcs8(IBinaryConvertible data, out int bytesRead)

Imports an unencrypted PKCS #8 private key from an IBinaryConvertible value and reports the number of bytes consumed.

Parameter Type Description
data IBinaryConvertible The binary-convertible value containing the PKCS #8 DER bytes.
bytesRead out int When this method returns, contains the number of bytes read from data.

Returns — A new EcdhPrivateKey instance.

Exceptions

Exception Condition
CryptographicException data does not represent a valid PKCS #8 ECDH private key.

ImportPkcs8(IBinaryConvertible, ReadOnlySpan<char>, out int)

public static EcdhPrivateKey ImportPkcs8(IBinaryConvertible data, ReadOnlySpan<char> password, out int bytesRead)

Imports an encrypted PKCS #8 private key from an IBinaryConvertible value (character password) and reports bytes consumed.

Parameter Type Description
data IBinaryConvertible The binary-convertible value containing the encrypted PKCS #8 DER bytes.
password ReadOnlySpan<char> The password used to decrypt the key.
bytesRead out int When this method returns, contains the number of bytes read from data.

Returns — A new EcdhPrivateKey instance.

Exceptions

Exception Condition
CryptographicException data is malformed, or the password is incorrect.

ImportPkcs8(IBinaryConvertible, ReadOnlySpan<byte>, out int)

public static EcdhPrivateKey ImportPkcs8(IBinaryConvertible data, ReadOnlySpan<byte> password, out int bytesRead)

Imports an encrypted PKCS #8 private key from an IBinaryConvertible value (byte password) and reports bytes consumed.

Parameter Type Description
data IBinaryConvertible The binary-convertible value containing the encrypted PKCS #8 DER bytes.
password ReadOnlySpan<byte> The password bytes used to decrypt the key.
bytesRead out int When this method returns, contains the number of bytes read from data.

Returns — A new EcdhPrivateKey instance.

Exceptions

Exception Condition
CryptographicException data is malformed, or the password is incorrect.

ImportPkcs8(ReadOnlySpan<byte>)

public static EcdhPrivateKey ImportPkcs8(ReadOnlySpan<byte> data)

Imports an unencrypted PKCS #8 private key from a span of bytes.

Parameter Type Description
data ReadOnlySpan<byte> The span containing the PKCS #8 DER bytes.

Returns — A new EcdhPrivateKey instance.

Exceptions

Exception Condition
CryptographicException data does not represent a valid PKCS #8 ECDH private key.

ImportPkcs8(ReadOnlySpan<byte>, ReadOnlySpan<char>)

public static EcdhPrivateKey ImportPkcs8(ReadOnlySpan<byte> data, ReadOnlySpan<char> password)

Imports an encrypted PKCS #8 private key from a span of bytes using a character-based password.

Parameter Type Description
data ReadOnlySpan<byte> The span containing the encrypted PKCS #8 DER bytes.
password ReadOnlySpan<char> The password used to decrypt the key.

Returns — A new EcdhPrivateKey instance.

Exceptions

Exception Condition
CryptographicException data is malformed, or the password is incorrect.

ImportPkcs8(ReadOnlySpan<byte>, ReadOnlySpan<byte>)

public static EcdhPrivateKey ImportPkcs8(ReadOnlySpan<byte> data, ReadOnlySpan<byte> password)

Imports an encrypted PKCS #8 private key from a span of bytes using a byte-based password.

Parameter Type Description
data ReadOnlySpan<byte> The span containing the encrypted PKCS #8 DER bytes.
password ReadOnlySpan<byte> The password bytes used to decrypt the key.

Returns — A new EcdhPrivateKey instance.

Exceptions

Exception Condition
CryptographicException data is malformed, or the password is incorrect.

ImportPkcs8(ReadOnlySpan<byte>, out int)

public static EcdhPrivateKey ImportPkcs8(ReadOnlySpan<byte> data, out int bytesRead)

Imports an unencrypted PKCS #8 private key from a span of bytes and reports the number of bytes consumed.

Parameter Type Description
data ReadOnlySpan<byte> The span containing the PKCS #8 DER bytes.
bytesRead out int When this method returns, contains the number of bytes read from data.

Returns — A new EcdhPrivateKey instance.

Exceptions

Exception Condition
CryptographicException data does not represent a valid PKCS #8 ECDH private key.

ImportPkcs8(ReadOnlySpan<byte>, ReadOnlySpan<char>, out int)

public static EcdhPrivateKey ImportPkcs8(ReadOnlySpan<byte> data, ReadOnlySpan<char> password, out int bytesRead)

Imports an encrypted PKCS #8 private key from a span of bytes (character password) and reports bytes consumed.

Parameter Type Description
data ReadOnlySpan<byte> The span containing the encrypted PKCS #8 DER bytes.
password ReadOnlySpan<char> The password used to decrypt the key.
bytesRead out int When this method returns, contains the number of bytes read from data.

Returns — A new EcdhPrivateKey instance.

Exceptions

Exception Condition
CryptographicException data is malformed, or the password is incorrect.

ImportPkcs8(ReadOnlySpan<byte>, ReadOnlySpan<byte>, out int)

public static EcdhPrivateKey ImportPkcs8(ReadOnlySpan<byte> data, ReadOnlySpan<byte> password, out int bytesRead)

Imports an encrypted PKCS #8 private key from a span of bytes (byte password) and reports bytes consumed.

Parameter Type Description
data ReadOnlySpan<byte> The span containing the encrypted PKCS #8 DER bytes.
password ReadOnlySpan<byte> The password bytes used to decrypt the key.
bytesRead out int When this method returns, contains the number of bytes read from data.

Returns — A new EcdhPrivateKey instance.

Exceptions

Exception Condition
CryptographicException data is malformed, or the password is incorrect.

Parse(string, IFormatProvider?)

public static EcdhPrivateKey Parse(string value, IFormatProvider? provider = null)

Parses an encoded string into an EcdhPrivateKey. The default encoding is Base16 (hexadecimal, invariant culture); pass an alternative IFormatProvider (such as a Base58 or Base64 provider) to decode other formats.

Parameter Type Description
value string The encoded string representation of the key.
provider IFormatProvider? An optional format provider that controls decoding. Defaults to Base16 invariant if null.

Returns — A new EcdhPrivateKey instance decoded from value.

Exceptions

Exception Condition
FormatException value is not a valid encoded representation of an ECDH private key.

Parse(ReadOnlySpan<char>, IFormatProvider?)

public static EcdhPrivateKey Parse(ReadOnlySpan<char> value, IFormatProvider? provider = null)

Parses an encoded character span into an EcdhPrivateKey. The default encoding is Base16; pass an alternative IFormatProvider to decode other formats.

Parameter Type Description
value ReadOnlySpan<char> The encoded character span representation of the key.
provider IFormatProvider? An optional format provider that controls decoding. Defaults to Base16 invariant if null.

Returns — A new EcdhPrivateKey instance decoded from value.

Exceptions

Exception Condition
FormatException value is not a valid encoded representation of an ECDH private key.

TryParse(string?, IFormatProvider?, out EcdhPrivateKey)

public static bool TryParse(string? value, IFormatProvider? provider, out EcdhPrivateKey result)

Attempts to parse an encoded string into an EcdhPrivateKey without throwing on failure. The default encoding is Base16; pass an alternative IFormatProvider to decode other formats.

Parameter Type Description
value string? The encoded string representation of the key.
provider IFormatProvider? An optional format provider that controls decoding.
result out EcdhPrivateKey When this method returns, contains the parsed EcdhPrivateKey if successful, or a default instance if unsuccessful.

Returns — true if value was parsed successfully; otherwise, false.

TryParse(ReadOnlySpan<char>, IFormatProvider?, out EcdhPrivateKey)

public static bool TryParse(ReadOnlySpan<char> value, IFormatProvider? provider, out EcdhPrivateKey result)

Attempts to parse an encoded character span into an EcdhPrivateKey without throwing on failure.

Parameter Type Description
value ReadOnlySpan<char> The encoded character span representation of the key.
provider IFormatProvider? An optional format provider that controls decoding.
result out EcdhPrivateKey When this method returns, contains the parsed EcdhPrivateKey if successful, or a default instance if unsuccessful.

Returns — true if value was parsed successfully; otherwise, false.

Instance Methods

Method Returns Description
DeriveSharedSecret(IEcdhPublicKey) Secret Derives a cryptographic shared secret from this private key and the specified ECDH public key.
Export() byte[] Exports the key as raw EC private key bytes (SEC 1 / RFC 5915 DER).
ExportPem() string Exports the key as RFC 7468 PEM text with the EC PRIVATE KEY label.
ExportPkcs8() byte[] Exports the key as unencrypted PKCS #8 DER bytes.
ExportPkcs8(ReadOnlySpan<char>, PbeParameters) byte[] Exports the key as encrypted PKCS #8 DER bytes using a character-based password.
ExportPkcs8(ReadOnlySpan<byte>, PbeParameters) byte[] Exports the key as encrypted PKCS #8 DER bytes using a byte-based password.
ExportPkcs8Pem() string Exports the key as unencrypted PKCS #8 RFC 7468 PEM text.
ExportPkcs8Pem(ReadOnlySpan<char>, PbeParameters) string Exports the key as encrypted PKCS #8 RFC 7468 PEM text using a character-based password.
ExportPkcs8Pem(ReadOnlySpan<byte>, PbeParameters) string Exports the key as encrypted PKCS #8 RFC 7468 PEM text using a byte-based password.
GetPublicKey() EcdhPublicKey Derives the corresponding EcdhPublicKey from this private key.
ToNamedPrivateKey() NamedPrivateKey Creates a NamedPrivateKey tagging this instance with the algorithm name "ECDH".

DeriveSharedSecret(IEcdhPublicKey)

public Secret DeriveSharedSecret(IEcdhPublicKey publicKey)

Derives a cryptographic shared secret from this private key and the specified EC Diffie-Hellman public key. The raw key material used during derivation is zeroed from memory immediately after the Secret is constructed. Both parties performing this operation with their respective private keys and each other's public keys independently arrive at the same Secret value.

Parameter Type Description
publicKey IEcdhPublicKey The EC Diffie-Hellman public key of the remote party, used to derive the shared secret.

Returns — A Secret containing the derived shared secret bytes.

Exceptions

Exception Condition
ObjectDisposedException The current EcdhPrivateKey has been disposed.
CryptographicException The key data is malformed or the derivation operation fails.

Export()

public byte[] Export()

Exports the key as a raw EC private key in SEC 1 / RFC 5915 DER format.

Returns — A byte[] containing the raw EC private key bytes.

Exceptions

Exception Condition
ObjectDisposedException The current EcdhPrivateKey has been disposed.
CryptographicException The key data is malformed or the export operation fails.

ExportPem()

public string ExportPem()

Exports the key as RFC 7468 PEM text with the EC PRIVATE KEY label (algorithm-native PEM, not PKCS #8).

Returns — A PEM-encoded string containing the EC private key.

Exceptions

Exception Condition
ObjectDisposedException The current EcdhPrivateKey has been disposed.
CryptographicException The key data is malformed or the export operation fails.

ExportPkcs8()

public byte[] ExportPkcs8()

Exports the key as unencrypted PKCS #8 DER bytes.

Returns — A byte[] containing the PKCS #8 DER-encoded private key.

Exceptions

Exception Condition
ObjectDisposedException The current EcdhPrivateKey has been disposed.
CryptographicException The key data is malformed or the export operation fails.

ExportPkcs8(ReadOnlySpan<char>, PbeParameters)

public byte[] ExportPkcs8(ReadOnlySpan<char> password, PbeParameters parameters)

Exports the key as encrypted PKCS #8 DER bytes protected with a character-based password.

Parameter Type Description
password ReadOnlySpan<char> The password used to encrypt the exported key.
parameters PbeParameters The password-based encryption parameters (algorithm, hash, and iteration count).

Returns — A byte[] containing the encrypted PKCS #8 DER-encoded private key.

Exceptions

Exception Condition
ObjectDisposedException The current EcdhPrivateKey has been disposed.
CryptographicException The key data is malformed or the export operation fails.

ExportPkcs8(ReadOnlySpan<byte>, PbeParameters)

public byte[] ExportPkcs8(ReadOnlySpan<byte> password, PbeParameters parameters)

Exports the key as encrypted PKCS #8 DER bytes protected with a byte-based password.

Parameter Type Description
password ReadOnlySpan<byte> The password bytes used to encrypt the exported key.
parameters PbeParameters The password-based encryption parameters (algorithm, hash, and iteration count).

Returns — A byte[] containing the encrypted PKCS #8 DER-encoded private key.

Exceptions

Exception Condition
ObjectDisposedException The current EcdhPrivateKey has been disposed.
CryptographicException The key data is malformed or the export operation fails.

ExportPkcs8Pem()

public string ExportPkcs8Pem()

Exports the key as unencrypted PKCS #8 RFC 7468 PEM text with the PRIVATE KEY label.

Returns — A PEM-encoded string containing the unencrypted PKCS #8 private key.

Exceptions

Exception Condition
ObjectDisposedException The current EcdhPrivateKey has been disposed.
CryptographicException The key data is malformed or the export operation fails.

ExportPkcs8Pem(ReadOnlySpan<char>, PbeParameters)

public string ExportPkcs8Pem(ReadOnlySpan<char> password, PbeParameters parameters)

Exports the key as encrypted PKCS #8 RFC 7468 PEM text using a character-based password.

Parameter Type Description
password ReadOnlySpan<char> The password used to encrypt the exported key.
parameters PbeParameters The password-based encryption parameters (algorithm, hash, and iteration count).

Returns — A PEM-encoded string containing the encrypted PKCS #8 private key.

Exceptions

Exception Condition
ObjectDisposedException The current EcdhPrivateKey has been disposed.
CryptographicException The key data is malformed or the export operation fails.

ExportPkcs8Pem(ReadOnlySpan<byte>, PbeParameters)

public string ExportPkcs8Pem(ReadOnlySpan<byte> password, PbeParameters parameters)

Exports the key as encrypted PKCS #8 RFC 7468 PEM text using a byte-based password.

Parameter Type Description
password ReadOnlySpan<byte> The password bytes used to encrypt the exported key.
parameters PbeParameters The password-based encryption parameters (algorithm, hash, and iteration count).

Returns — A PEM-encoded string containing the encrypted PKCS #8 private key.

Exceptions

Exception Condition
ObjectDisposedException The current EcdhPrivateKey has been disposed.
CryptographicException The key data is malformed or the export operation fails.

GetPublicKey()

public EcdhPublicKey GetPublicKey()

Derives the corresponding EcdhPublicKey from this private key. The returned public key is encoded as SubjectPublicKeyInfo (X.509 DER) and is safe to share with remote parties.

Returns — The EcdhPublicKey corresponding to this EcdhPrivateKey.

Exceptions

Exception Condition
ObjectDisposedException The current EcdhPrivateKey has been disposed.
CryptographicException The key data is malformed or the derivation operation fails.

ToNamedPrivateKey()

public override NamedPrivateKey ToNamedPrivateKey()

Creates a new NamedPrivateKey pairing this instance with the algorithm name "ECDH".

Returns — A NamedPrivateKey whose AlgorithmName is "ECDH" and whose PrivateKey is this instance.

Implicit Conversion Operators

Operator From Description
implicit operator EcdhPrivateKey ReadOnlySpan<byte> Creates a new EcdhPrivateKey from a span of bytes.
implicit operator EcdhPrivateKey ReadOnlyMemory<byte> Creates a new EcdhPrivateKey from a memory region.
implicit operator EcdhPrivateKey ReadOnlySequence<byte> Creates a new EcdhPrivateKey from a byte sequence.

See Also

ONIXLabs .NET Library


Clone this wiki locally