Skip to content

OnixLabs.Security.Cryptography.EcdhPublicKey

matthew edited this page May 27, 2026 · 2 revisions

EcdhPublicKey Class

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

Namespace OnixLabs.Security.Cryptography
Assembly OnixLabs.Security.Cryptography.dll (NuGet)
Inheritance object → PublicKey → EcdhPublicKey
Implements IEcdhPublicKey, ISpanParsable<EcdhPublicKey>, IPublicKeyImportable<EcdhPublicKey>, IPublicKeyExportable, IBinaryConvertible

Definition

public sealed partial class EcdhPublicKey : PublicKey, IEcdhPublicKey, ISpanParsable<EcdhPublicKey>

Remarks

EcdhPublicKey is the concrete public-key type for the Elliptic Curve Diffie-Hellman (ECDH) key-agreement protocol. An ECDH public key is the value that is shared with the remote party so that both sides can independently derive the same shared secret via EcdhPrivateKey.DeriveSharedSecret. EcdhPublicKey is not used for signature verification; for that purpose, see EcdsaPublicKey or EddsaPublicKey.

Obtaining instances — The standard way to obtain an EcdhPublicKey is by calling GetPublicKey() on the corresponding EcdhPrivateKey. An existing SubjectPublicKeyInfo (X.509 DER) byte array can be loaded via Import, and RFC 7468 PEM text via ImportPem. Hex- or Base64-encoded key strings can be parsed back via Parse / TryParse.

Export formats — EcdhPublicKey supports two export formats:

  • Export() — SubjectPublicKeyInfo (X.509 DER) bytes.
  • ExportPem() — RFC 7468 PEM text with the PUBLIC KEY label.

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

Unlike EcdhPrivateKey, EcdhPublicKey does not implement IDisposable because it contains no sensitive material that requires secure erasure.

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

Examples

Deriving a public key and sharing it

using OnixLabs.Security.Cryptography;

// Generate an ECDH key pair and extract the public key to share.
using EcdhPrivateKey privateKey = EcdhPrivateKey.Create();
EcdhPublicKey publicKey = privateKey.GetPublicKey();

// Export as SubjectPublicKeyInfo bytes to transmit to the remote party.
byte[] spkiBytes = publicKey.Export();

// The remote party imports the bytes and uses them to derive the shared secret.
EcdhPublicKey remoteImported = EcdhPublicKey.Import(spkiBytes);
Console.WriteLine(publicKey == remoteImported); // True

Importing from PEM and exporting back to PEM

using OnixLabs.Security.Cryptography;

// Import from a PEM-encoded SubjectPublicKeyInfo.
EcdhPublicKey publicKey = EcdhPublicKey.ImportPem(pemText);

// Round-trip back to PEM.
string pem = publicKey.ExportPem();
Console.WriteLine(pem); // -----BEGIN PUBLIC KEY-----...

Wrapping in a NamedPublicKey

using OnixLabs.Security.Cryptography;

using EcdhPrivateKey privateKey = EcdhPrivateKey.Create();
EcdhPublicKey publicKey = privateKey.GetPublicKey();

NamedPublicKey named = publicKey.ToNamedPublicKey();

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

Constructors

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

EcdhPublicKey(ReadOnlySpan<byte>)

public EcdhPublicKey(ReadOnlySpan<byte> keyData)

Initializes a new instance of the EcdhPublicKey class. The provided span is copied to an internal array; the span itself is not retained.

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

EcdhPublicKey(ReadOnlyMemory<byte>)

public EcdhPublicKey(ReadOnlyMemory<byte> keyData)

Initializes a new instance of the EcdhPublicKey class. The provided memory region is copied to an internal array; the original memory is not retained.

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

EcdhPublicKey(ReadOnlySequence<byte>)

public EcdhPublicKey(ReadOnlySequence<byte> keyData)

Initializes a new instance of the EcdhPublicKey class. The provided byte sequence is copied to an internal array; the original sequence is not retained.

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

Static Methods

Method Returns Description
Import(IBinaryConvertible) EcdhPublicKey Imports a SubjectPublicKeyInfo public key from an IBinaryConvertible value.
Import(IBinaryConvertible, out int) EcdhPublicKey Imports a SubjectPublicKeyInfo public key from an IBinaryConvertible value and reports bytes consumed.
Import(ReadOnlySpan<byte>) EcdhPublicKey Imports a SubjectPublicKeyInfo public key from a span of bytes.
Import(ReadOnlySpan<byte>, out int) EcdhPublicKey Imports a SubjectPublicKeyInfo public key from a span of bytes and reports bytes consumed.
ImportPem(ReadOnlySpan<char>) EcdhPublicKey Imports a key from RFC 7468 PEM text.
ImportPem(ReadOnlySpan<char>, ReadOnlySpan<char>) EcdhPublicKey Imports a key from RFC 7468 PEM text encrypted with a character-based password.
ImportPem(ReadOnlySpan<char>, ReadOnlySpan<byte>) EcdhPublicKey Imports a key from RFC 7468 PEM text encrypted with a byte-based password.
Parse(string, IFormatProvider?) EcdhPublicKey Parses an encoded string into an EcdhPublicKey.
Parse(ReadOnlySpan<char>, IFormatProvider?) EcdhPublicKey Parses an encoded character span into an EcdhPublicKey.
TryParse(string?, IFormatProvider?, out EcdhPublicKey) bool Attempts to parse an encoded string into an EcdhPublicKey.
TryParse(ReadOnlySpan<char>, IFormatProvider?, out EcdhPublicKey) bool Attempts to parse an encoded character span into an EcdhPublicKey.

Import(IBinaryConvertible)

public static EcdhPublicKey Import(IBinaryConvertible data)

Imports a SubjectPublicKeyInfo (X.509 DER format) public key from an IBinaryConvertible value.

Parameter Type Description
data IBinaryConvertible The binary-convertible value containing the SubjectPublicKeyInfo bytes to import.

Returns — A new EcdhPublicKey instance.

Exceptions

Exception Condition
CryptographicException data does not represent a valid ECDH SubjectPublicKeyInfo.

Import(IBinaryConvertible, out int)

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

Imports a SubjectPublicKeyInfo public key from an IBinaryConvertible value and reports the number of bytes consumed.

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

Returns — A new EcdhPublicKey instance.

Exceptions

Exception Condition
CryptographicException data does not represent a valid ECDH SubjectPublicKeyInfo.

Import(ReadOnlySpan<byte>)

public static EcdhPublicKey Import(ReadOnlySpan<byte> data)

Imports a SubjectPublicKeyInfo (X.509 DER format) public key from a span of bytes.

Parameter Type Description
data ReadOnlySpan<byte> The span containing the SubjectPublicKeyInfo bytes to import.

Returns — A new EcdhPublicKey instance.

Exceptions

Exception Condition
CryptographicException data does not represent a valid ECDH SubjectPublicKeyInfo.

Import(ReadOnlySpan<byte>, out int)

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

Imports a SubjectPublicKeyInfo public key from a span of bytes and reports the number of bytes consumed.

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

Returns — A new EcdhPublicKey instance.

Exceptions

Exception Condition
CryptographicException data does not represent a valid ECDH SubjectPublicKeyInfo.

ImportPem(ReadOnlySpan<char>)

public static EcdhPublicKey ImportPem(ReadOnlySpan<char> data)

Imports a key from RFC 7468 PEM-encoded text. Supports the PUBLIC KEY label (SubjectPublicKeyInfo).

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

Returns — A new EcdhPublicKey instance.

Exceptions

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

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

public static EcdhPublicKey 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 EcdhPublicKey instance.

Exceptions

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

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

public static EcdhPublicKey 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 EcdhPublicKey instance.

Exceptions

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

Parse(string, IFormatProvider?)

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

Parses an encoded string into an EcdhPublicKey. 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 EcdhPublicKey instance decoded from value.

Exceptions

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

Parse(ReadOnlySpan<char>, IFormatProvider?)

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

Parses an encoded character span into an EcdhPublicKey. 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 EcdhPublicKey instance decoded from value.

Exceptions

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

TryParse(string?, IFormatProvider?, out EcdhPublicKey)

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

Attempts to parse an encoded string into an EcdhPublicKey without throwing on failure.

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

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

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

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

Attempts to parse an encoded character span into an EcdhPublicKey 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 EcdhPublicKey When this method returns, contains the parsed EcdhPublicKey if successful, or a default instance if unsuccessful.

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

Instance Methods

Method Returns Description
Export() byte[] Exports the key as SubjectPublicKeyInfo (X.509 DER) bytes.
ExportPem() string Exports the key as RFC 7468 PEM text with the PUBLIC KEY label.
ToNamedPublicKey() NamedPublicKey Creates a NamedPublicKey tagging this instance with the algorithm name "ECDH".

Export()

public byte[] Export()

Exports the key as SubjectPublicKeyInfo bytes in X.509 DER format. This is the standard wire format for sharing an ECDH public key with a remote party.

Returns — A byte[] containing the SubjectPublicKeyInfo-encoded public key.

Exceptions

Exception Condition
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 PUBLIC KEY label (SubjectPublicKeyInfo).

Returns — A PEM-encoded string containing the public key.

Exceptions

Exception Condition
CryptographicException The key data is malformed or the export operation fails.

ToNamedPublicKey()

public override NamedPublicKey ToNamedPublicKey()

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

Returns — A NamedPublicKey whose AlgorithmName is "ECDH" and whose PublicKey is this instance.

Implicit Conversion Operators

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

See Also

ONIXLabs .NET Library


Clone this wiki locally