Skip to content

OnixLabs.Security.Cryptography.EcdsaPrivateKey

matthew edited this page May 27, 2026 · 2 revisions

EcdsaPrivateKey Class

Represents an ECDSA (Elliptic Curve Digital Signature Algorithm) cryptographic private key, used to sign data and derive the corresponding public key.

Namespace OnixLabs.Security.Cryptography
Assembly OnixLabs.Security.Cryptography.dll (NuGet)
Inheritance object → PrivateKey → EcdsaPrivateKey
Implements IEcdsaPrivateKey, ISpanParsable<EcdsaPrivateKey>, IPrivateKeyDerivable<EcdsaPublicKey>, IPrivateKeyImportable<EcdsaPrivateKey>, IPrivateKeyExportable, ICryptoPrimitive<PrivateKey>, IValueEquatable<PrivateKey>, IEquatable<PrivateKey>, IBinaryConvertible, IDisposable

Definition

public sealed partial class EcdsaPrivateKey : PrivateKey, IEcdsaPrivateKey, ISpanParsable<EcdsaPrivateKey>

Remarks

EcdsaPrivateKey is the concrete ECDSA private-key type. It stores key material as an RFC 5915 SEC1 ECPrivateKey DER structure in the in-memory ProtectedData wrapper it inherits from PrivateKey. The clear key bytes are only ever decrypted in-process for the duration of a cryptographic operation and are immediately zeroed afterwards.

Signing. Two families of signing operation are available:

  • SignData — hashes the supplied input data and then signs the resulting digest. Overloads accept a ReadOnlySpan<byte>, a byte-range slice (offset/count), a Stream, or an IBinaryConvertible. The hash algorithm may be supplied as a HashAlgorithm instance or as a HashAlgorithmName.
  • SignHash — signs a pre-computed hash value directly. Overloads accept a Hash or a raw ReadOnlySpan<byte>.

All signing overloads accept an optional DSASignatureFormat parameter (defaulting to DSASignatureFormat.IeeeP1363FixedFieldConcatenation). Use DSASignatureFormat.Rfc3279DerSequence to produce DER-encoded signatures compatible with systems that expect ASN.1 encoding.

Obtaining instances. The preferred way to create a fresh key pair is the Create() static factory method, which generates a new key using the platform default elliptic curve (typically NIST P-256). Overloads accept an ECCurve or ECParameters for explicit curve selection. Existing key material can be imported via Import, ImportPkcs8, or ImportPem. A hex-encoded key can be round-tripped using Parse / TryParse.

Deriving the public key. Call GetPublicKey() to derive the corresponding EcdsaPublicKey from this private key. The derived key can then be distributed and used for signature verification.

Export formats. The key can be exported in four formats:

Method Format
Export() Raw SEC1 DER binary (ECPrivateKey)
ExportPem() Algorithm-native PEM (EC PRIVATE KEY)
ExportPkcs8() PKCS #8 DER binary (PrivateKeyInfo)
ExportPkcs8Pem() PKCS #8 PEM (PRIVATE KEY or ENCRYPTED PRIVATE KEY)

Lifecycle. EcdsaPrivateKey implements IDisposable (inherited from PrivateKey). Always dispose instances, or use a using statement, to ensure the in-memory key bytes are zeroed promptly. Calling any method that accesses key material after disposal throws ObjectDisposedException.

Named key. Call ToNamedPrivateKey() to wrap this key in a NamedPrivateKey that carries the algorithm name "ECDSA" alongside the key bytes.

Note EcdsaPrivateKey inherits value semantics from PrivateKey: equality and hash codes are based on the key bytes, not reference identity. Equality comparisons use constant-time byte comparison to prevent timing side-channel attacks.

Examples

Generating a key pair and signing data

using System.Security.Cryptography;
using OnixLabs.Security.Cryptography;

// Generate a new ECDSA private key using the platform default curve.
using EcdsaPrivateKey privateKey = EcdsaPrivateKey.Create();

// Derive the corresponding public key.
EcdsaPublicKey publicKey = privateKey.GetPublicKey();

byte[] data = System.Text.Encoding.UTF8.GetBytes("Hello, ONIXLabs!");

// Sign the data using SHA-256.
byte[] signature = privateKey.SignData(data, HashAlgorithmName.SHA256);

// Verify the signature using the public key.
bool valid = publicKey.IsDataValid(signature, data, HashAlgorithmName.SHA256);
Console.WriteLine(valid); // True

Creating a key on a specific curve

using System.Security.Cryptography;
using OnixLabs.Security.Cryptography;

// Create a key on NIST P-384.
using EcdsaPrivateKey privateKey = EcdsaPrivateKey.Create(ECCurve.NamedCurves.nistP384);

Exporting and importing via PKCS #8 PEM

using OnixLabs.Security.Cryptography;

using EcdsaPrivateKey original = EcdsaPrivateKey.Create();

// Export as PKCS #8 PEM.
string pem = original.ExportPkcs8Pem();

// Re-import.
EcdsaPublicKey imported = EcdsaPrivateKey.ImportPem(pem).GetPublicKey();

Wrapping in a NamedPrivateKey

using OnixLabs.Security.Cryptography;

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

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

Constructors

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

EcdsaPrivateKey(ReadOnlySpan<byte>)

public EcdsaPrivateKey(ReadOnlySpan<byte> keyData)

Initializes a new instance of the EcdsaPrivateKey class from a span of bytes. The provided span is copied into an internal ProtectedData wrapper; the span itself is not retained.

Parameter Type Description
keyData ReadOnlySpan<byte> The underlying key data of the ECDSA cryptographic private key.

EcdsaPrivateKey(ReadOnlyMemory<byte>)

public EcdsaPrivateKey(ReadOnlyMemory<byte> keyData)

Initializes a new instance of the EcdsaPrivateKey class from a memory region. The provided memory region is copied into an internal ProtectedData wrapper; the original memory is not retained.

Parameter Type Description
keyData ReadOnlyMemory<byte> The underlying key data of the ECDSA cryptographic private key.

EcdsaPrivateKey(ReadOnlySequence<byte>)

public EcdsaPrivateKey(ReadOnlySequence<byte> keyData)

Initializes a new instance of the EcdsaPrivateKey class from a byte sequence. The provided byte sequence is copied into an internal ProtectedData wrapper; the original sequence is not retained.

Parameter Type Description
keyData ReadOnlySequence<byte> The underlying key data of the ECDSA cryptographic private key.

Static Methods

Method Returns Description
Create() EcdsaPrivateKey Generates a new ECDSA private key using the platform default curve.
Create(ECCurve) EcdsaPrivateKey Generates a new ECDSA private key on the specified elliptic curve.
Create(ECParameters) EcdsaPrivateKey Generates a new ECDSA private key from the specified elliptic curve parameters.
Import(ReadOnlySpan<byte>) EcdsaPrivateKey Imports a key from raw SEC1 DER bytes.
Import(ReadOnlySpan<byte>, out int) EcdsaPrivateKey Imports a key from raw SEC1 DER bytes; reports bytes read.
Import(IBinaryConvertible) EcdsaPrivateKey Imports a key from an IBinaryConvertible.
Import(IBinaryConvertible, out int) EcdsaPrivateKey Imports a key from an IBinaryConvertible; reports bytes read.
ImportPkcs8(ReadOnlySpan<byte>) EcdsaPrivateKey Imports a key from unencrypted PKCS #8 DER bytes.
ImportPkcs8(ReadOnlySpan<byte>, out int) EcdsaPrivateKey Imports a key from unencrypted PKCS #8 DER bytes; reports bytes read.
ImportPkcs8(ReadOnlySpan<byte>, ReadOnlySpan<char>) EcdsaPrivateKey Imports a key from encrypted PKCS #8 DER bytes using a char-based password.
ImportPkcs8(ReadOnlySpan<byte>, ReadOnlySpan<char>, out int) EcdsaPrivateKey Imports a key from encrypted PKCS #8 DER bytes using a char-based password; reports bytes read.
ImportPkcs8(ReadOnlySpan<byte>, ReadOnlySpan<byte>) EcdsaPrivateKey Imports a key from encrypted PKCS #8 DER bytes using a byte-based password.
ImportPkcs8(ReadOnlySpan<byte>, ReadOnlySpan<byte>, out int) EcdsaPrivateKey Imports a key from encrypted PKCS #8 DER bytes using a byte-based password; reports bytes read.
ImportPkcs8(IBinaryConvertible) EcdsaPrivateKey Imports a key from an unencrypted PKCS #8 IBinaryConvertible.
ImportPkcs8(IBinaryConvertible, out int) EcdsaPrivateKey Imports a key from an unencrypted PKCS #8 IBinaryConvertible; reports bytes read.
ImportPkcs8(IBinaryConvertible, ReadOnlySpan<char>) EcdsaPrivateKey Imports a key from an encrypted PKCS #8 IBinaryConvertible using a char-based password.
ImportPkcs8(IBinaryConvertible, ReadOnlySpan<char>, out int) EcdsaPrivateKey Imports a key from an encrypted PKCS #8 IBinaryConvertible using a char-based password; reports bytes read.
ImportPkcs8(IBinaryConvertible, ReadOnlySpan<byte>) EcdsaPrivateKey Imports a key from an encrypted PKCS #8 IBinaryConvertible using a byte-based password.
ImportPkcs8(IBinaryConvertible, ReadOnlySpan<byte>, out int) EcdsaPrivateKey Imports a key from an encrypted PKCS #8 IBinaryConvertible using a byte-based password; reports bytes read.
ImportPem(ReadOnlySpan<char>) EcdsaPrivateKey Imports a key from unencrypted RFC 7468 PEM text.
ImportPem(ReadOnlySpan<char>, ReadOnlySpan<char>) EcdsaPrivateKey Imports a key from encrypted PEM text using a char-based password.
ImportPem(ReadOnlySpan<char>, ReadOnlySpan<byte>) EcdsaPrivateKey Imports a key from encrypted PEM text using a byte-based password.
Parse(string, IFormatProvider?) EcdsaPrivateKey Parses a base-encoded string into an EcdsaPrivateKey.
Parse(ReadOnlySpan<char>, IFormatProvider?) EcdsaPrivateKey Parses a base-encoded character span into an EcdsaPrivateKey.
TryParse(string?, IFormatProvider?, out EcdsaPrivateKey) bool Attempts to parse a base-encoded string into an EcdsaPrivateKey.
TryParse(ReadOnlySpan<char>, IFormatProvider?, out EcdsaPrivateKey) bool Attempts to parse a base-encoded character span into an EcdsaPrivateKey.

Create()

public static EcdsaPrivateKey Create()

Generates a new EcdsaPrivateKey instance using the platform default elliptic curve (typically NIST P-256). The raw key bytes are zeroed immediately after the EcdsaPrivateKey is constructed.

Returns — A new EcdsaPrivateKey instance.

Create(ECCurve)

public static EcdsaPrivateKey Create(ECCurve curve)

Generates a new EcdsaPrivateKey instance on the specified elliptic curve.

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

Returns — A new EcdsaPrivateKey instance.

Exceptions

Exception Condition
CryptographicException curve does not represent a valid elliptic curve, or the current platform does not support it.

Create(ECParameters)

public static EcdsaPrivateKey Create(ECParameters parameters)

Generates a new EcdsaPrivateKey instance from the specified elliptic curve parameters.

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

Returns — A new EcdsaPrivateKey instance.

Exceptions

Exception Condition
CryptographicException parameters does not represent a valid ECDSA private key.

Import overloads

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

Imports an ECDSA private key from raw SEC1 DER-encoded (ECPrivateKey) binary data.

Parameter Type Description
data ReadOnlySpan<byte> or IBinaryConvertible The raw SEC1 DER-encoded private key data to import.
bytesRead out int (bytesRead overloads) Receives the number of bytes consumed from data.

Returns — A new EcdsaPrivateKey instance.

Exceptions

Exception Condition
CryptographicException data does not contain a valid SEC1 ECDSA private key.

ImportPkcs8 overloads

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

Imports an ECDSA private key from PKCS #8 DER-encoded data. Overloads that accept a password expect an encrypted PKCS #8 (EncryptedPrivateKeyInfo) structure; the passwordless overloads expect an unencrypted PrivateKeyInfo.

Parameter Type Description
data ReadOnlySpan<byte> or IBinaryConvertible The PKCS #8 DER-encoded key data to import.
password ReadOnlySpan<char> or ReadOnlySpan<byte> (encrypted overloads) The password required for password-based decryption.
bytesRead out int (bytesRead overloads) Receives the number of bytes consumed from data.

Returns — A new EcdsaPrivateKey instance.

Exceptions

Exception Condition
CryptographicException data is not a valid PKCS #8 ECDSA private key, or password is incorrect.

ImportPem overloads

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

Imports an ECDSA private key from RFC 7468 PEM-encoded text. The PEM label determines the encoding format; supported labels include EC PRIVATE KEY (SEC1), PRIVATE KEY (PKCS #8), and ENCRYPTED PRIVATE KEY (encrypted PKCS #8). The two-argument overloads additionally decrypt an ENCRYPTED PRIVATE KEY PEM block.

Parameter Type Description
data ReadOnlySpan<char> The RFC 7468 PEM-encoded private key text to import.
password ReadOnlySpan<char> or ReadOnlySpan<byte> (encrypted overloads) The password required for password-based decryption.

Returns — A new EcdsaPrivateKey instance.

Exceptions

Exception Condition
CryptographicException data is not a valid PEM-encoded ECDSA private key, or password is incorrect.

Parse(string, IFormatProvider?)

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

Parses a base-encoded string representation of an EcdsaPrivateKey. When provider is null, Base16FormatProvider.Invariant (hexadecimal) is used.

Parameter Type Description
value string The base-encoded string to parse.
provider IFormatProvider? The format provider that determines the encoding. Defaults to hexadecimal when null.

Returns — A new EcdsaPrivateKey instance.

Exceptions

Exception Condition
FormatException value is not a valid base-encoded string for the specified provider.

Parse(ReadOnlySpan<char>, IFormatProvider?)

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

Parses a base-encoded character span representation of an EcdsaPrivateKey. When provider is null, Base16FormatProvider.Invariant (hexadecimal) is used.

Parameter Type Description
value ReadOnlySpan<char> The base-encoded character span to parse.
provider IFormatProvider? The format provider that determines the encoding. Defaults to hexadecimal when null.

Returns — A new EcdsaPrivateKey instance.

Exceptions

Exception Condition
FormatException value is not a valid base-encoded span for the specified provider.

TryParse(string?, IFormatProvider?, out EcdsaPrivateKey)

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

Attempts to parse a base-encoded string representation of an EcdsaPrivateKey without throwing on failure. When provider is null, Base16FormatProvider.Invariant is used.

Parameter Type Description
value string? The base-encoded string to parse.
provider IFormatProvider? The format provider that determines the encoding.
result out EcdsaPrivateKey When this method returns, contains the parsed EcdsaPrivateKey, or an instance wrapping the decoded bytes on partial failure.

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

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

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

Attempts to parse a base-encoded character span representation of an EcdsaPrivateKey without throwing on failure. When provider is null, Base16FormatProvider.Invariant is used.

Parameter Type Description
value ReadOnlySpan<char> The base-encoded character span to parse.
provider IFormatProvider? The format provider that determines the encoding.
result out EcdsaPrivateKey When this method returns, contains the parsed EcdsaPrivateKey, or an instance wrapping the decoded bytes on partial failure.

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

Instance Methods

Method Returns Description
GetPublicKey() EcdsaPublicKey Derives the corresponding ECDSA public key from this private key.
SignData(ReadOnlySpan<byte>, HashAlgorithm, DSASignatureFormat) byte[] Hashes and signs a span of bytes using a HashAlgorithm instance.
SignData(ReadOnlySpan<byte>, int, int, HashAlgorithm, DSASignatureFormat) byte[] Hashes and signs a byte-range slice using a HashAlgorithm instance.
SignData(Stream, HashAlgorithm, DSASignatureFormat) byte[] Hashes and signs a Stream using a HashAlgorithm instance.
SignData(IBinaryConvertible, HashAlgorithm, DSASignatureFormat) byte[] Hashes and signs an IBinaryConvertible using a HashAlgorithm instance.
SignData(ReadOnlySpan<byte>, HashAlgorithmName, DSASignatureFormat) byte[] Hashes and signs a span of bytes using a HashAlgorithmName.
SignData(ReadOnlySpan<byte>, int, int, HashAlgorithmName, DSASignatureFormat) byte[] Hashes and signs a byte-range slice using a HashAlgorithmName.
SignData(Stream, HashAlgorithmName, DSASignatureFormat) byte[] Hashes and signs a Stream using a HashAlgorithmName.
SignData(IBinaryConvertible, HashAlgorithmName, DSASignatureFormat) byte[] Hashes and signs an IBinaryConvertible using a HashAlgorithmName.
SignHash(Hash, DSASignatureFormat) byte[] Signs a pre-computed Hash.
SignHash(ReadOnlySpan<byte>, DSASignatureFormat) byte[] Signs a pre-computed hash supplied as a raw span of bytes.
Export() byte[] Exports the key in raw SEC1 DER binary format.
ExportPem() string Exports the key in algorithm-native RFC 7468 PEM format (EC PRIVATE KEY).
ExportPkcs8() byte[] Exports the key in PKCS #8 DER format.
ExportPkcs8(ReadOnlySpan<char>, PbeParameters) byte[] Exports the key in encrypted PKCS #8 DER format using a char-based password.
ExportPkcs8(ReadOnlySpan<byte>, PbeParameters) byte[] Exports the key in encrypted PKCS #8 DER format using a byte-based password.
ExportPkcs8Pem() string Exports the key in PKCS #8 RFC 7468 PEM format (PRIVATE KEY).
ExportPkcs8Pem(ReadOnlySpan<char>, PbeParameters) string Exports the key in encrypted PKCS #8 RFC 7468 PEM format using a char-based password.
ExportPkcs8Pem(ReadOnlySpan<byte>, PbeParameters) string Exports the key in encrypted PKCS #8 RFC 7468 PEM format using a byte-based password.
ToNamedPrivateKey() NamedPrivateKey Creates a NamedPrivateKey with algorithm name "ECDSA".

For inherited members (AsReadOnlyMemory, AsReadOnlySpan, Dispose, Equals, GetHashCode, ToString), see PrivateKey.


GetPublicKey()

public EcdsaPublicKey GetPublicKey()

Derives the corresponding EcdsaPublicKey from this private key. The public key is exported in SubjectPublicKeyInfo (SPKI) DER format and wrapped in a new EcdsaPublicKey instance.

Returns — The EcdsaPublicKey corresponding to this private key.

Exceptions

Exception Condition
ObjectDisposedException The current EcdsaPrivateKey has been disposed.
CryptographicException The underlying key data is not a valid ECDSA private key.

SignData overloads

public byte[] SignData(ReadOnlySpan<byte> data, HashAlgorithm algorithm, DSASignatureFormat format = default)
public byte[] SignData(ReadOnlySpan<byte> data, int offset, int count, HashAlgorithm algorithm, DSASignatureFormat format = default)
public byte[] SignData(Stream data, HashAlgorithm algorithm, DSASignatureFormat format = default)
public byte[] SignData(IBinaryConvertible data, HashAlgorithm algorithm, DSASignatureFormat format = default)
public byte[] SignData(ReadOnlySpan<byte> data, HashAlgorithmName algorithm, DSASignatureFormat format = default)
public byte[] SignData(ReadOnlySpan<byte> data, int offset, int count, HashAlgorithmName algorithm, DSASignatureFormat format = default)
public byte[] SignData(Stream data, HashAlgorithmName algorithm, DSASignatureFormat format = default)
public byte[] SignData(IBinaryConvertible data, HashAlgorithmName algorithm, DSASignatureFormat format = default)

Hashes the specified input data and signs the resulting digest using this ECDSA private key.

Parameter Type Description
data ReadOnlySpan<byte>, Stream, or IBinaryConvertible The input data to hash and sign.
offset int (slice overloads) The offset into the span from which to begin reading.
count int (slice overloads) The number of bytes to read from the span.
algorithm HashAlgorithm or HashAlgorithmName The hash algorithm used to hash the input data before signing.
format DSASignatureFormat The digital signature format. Defaults to DSASignatureFormat.IeeeP1363FixedFieldConcatenation.

Returns — A byte[] containing the cryptographic digital signature.

Exceptions

Exception Condition
ObjectDisposedException The current EcdsaPrivateKey has been disposed.
CryptographicException The signing operation failed.

SignHash overloads

public byte[] SignHash(Hash hash, DSASignatureFormat format = default)
public byte[] SignHash(ReadOnlySpan<byte> hash, DSASignatureFormat format = default)

Signs a pre-computed hash value using this ECDSA private key without performing an additional hash operation.

Parameter Type Description
hash Hash or ReadOnlySpan<byte> The pre-computed hash value to sign.
format DSASignatureFormat The digital signature format. Defaults to DSASignatureFormat.IeeeP1363FixedFieldConcatenation.

Returns — A byte[] containing the cryptographic digital signature.

Exceptions

Exception Condition
ObjectDisposedException The current EcdsaPrivateKey has been disposed.
CryptographicException The signing operation failed.

Export()

public byte[] Export()

Exports the ECDSA private key in its raw SEC1 DER binary format (ECPrivateKey). This format is the same as that produced by OpenSSL's ec command and is importable via Import.

Returns — A byte[] containing the SEC1 DER-encoded private key.

Exceptions

Exception Condition
ObjectDisposedException The current EcdsaPrivateKey has been disposed.
CryptographicException The key could not be exported.

ExportPem()

public string ExportPem()

Exports the ECDSA private key in its algorithm-native RFC 7468 PEM format, producing an EC PRIVATE KEY PEM block (SEC1 encoding). This format is the same as that produced by OpenSSL's -text output.

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

Exceptions

Exception Condition
ObjectDisposedException The current EcdsaPrivateKey has been disposed.
CryptographicException The key could not be exported.

ExportPkcs8 overloads

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

Exports the ECDSA private key in PKCS #8 DER binary format. The passwordless overload produces an unencrypted PrivateKeyInfo; the two-argument overloads produce an encrypted EncryptedPrivateKeyInfo.

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

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

Exceptions

Exception Condition
ObjectDisposedException The current EcdsaPrivateKey has been disposed.
CryptographicException The key could not be exported.

ExportPkcs8Pem overloads

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

Exports the ECDSA private key in PKCS #8 RFC 7468 PEM format. The passwordless overload produces a PRIVATE KEY PEM block; the two-argument overloads produce an ENCRYPTED PRIVATE KEY PEM block.

Parameter Type Description
password ReadOnlySpan<char> or ReadOnlySpan<byte> (encrypted overloads) The password used to encrypt the exported key.
parameters PbeParameters (encrypted overloads) The password-based encryption parameters.

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

Exceptions

Exception Condition
ObjectDisposedException The current EcdsaPrivateKey has been disposed.
CryptographicException The key could not be exported.

ToNamedPrivateKey()

public override NamedPrivateKey ToNamedPrivateKey()

Creates a new NamedPrivateKey from this instance with the algorithm name "ECDSA". The named key carries both the raw key bytes and the algorithm identifier, making it self-describing for storage and transmission.

Returns — A NamedPrivateKey pairing this instance with the name "ECDSA".

Exceptions

Exception Condition
ObjectDisposedException The current EcdsaPrivateKey has been disposed.

Conversion Operators

Operator From Description
implicit operator EcdsaPrivateKey ReadOnlySpan<byte> Creates a new EcdsaPrivateKey from a ReadOnlySpan<byte>.
implicit operator EcdsaPrivateKey ReadOnlyMemory<byte> Creates a new EcdsaPrivateKey from a ReadOnlyMemory<byte>.
implicit operator EcdsaPrivateKey ReadOnlySequence<byte> Creates a new EcdsaPrivateKey from a ReadOnlySequence<byte>.

See Also

ONIXLabs .NET Library


Clone this wiki locally