Class MLKem
- Namespace
- CryptoHives.Foundation.Security.Cryptography.Kem
- Assembly
- CryptoHives.Foundation.Security.Cryptography.dll
Represents an ML-KEM key (FIPS 203) and provides encapsulation and decapsulation.
public sealed class MLKem : IDisposable
- Inheritance
-
MLKem
- Implements
- Inherited Members
Remarks
The API shape mirrors System.Security.Cryptography.MLKem from .NET 10 so code
written against the in-box type ports directly to older target frameworks, including
.NET Framework and .NET Standard 2.0.
An instance holds either a full key pair (created via GenerateKey(MLKemAlgorithm), ImportPrivateSeed(MLKemAlgorithm, ReadOnlySpan<byte>), or ImportDecapsulationKey(MLKemAlgorithm, ReadOnlySpan<byte>)) or only an encapsulation key (via ImportEncapsulationKey(MLKemAlgorithm, ReadOnlySpan<byte>)). Keys generated from a seed retain the 64-byte (d ‖ z) seed, the storage format recommended by FIPS 203; keys imported from an expanded decapsulation key cannot export a seed.
Imported keys are validated with the FIPS 203 §7.2 modulus check (encapsulation keys) and §7.3 hash check (decapsulation keys). Secret key material is zeroed when the instance is disposed.
Example usage:
using var receiver = MLKem.GenerateKey(MLKemAlgorithm.MLKem768);
byte[] ek = receiver.ExportEncapsulationKey();
// Sender:
using var sender = MLKem.ImportEncapsulationKey(MLKemAlgorithm.MLKem768, ek);
byte[] ciphertext = new byte[sender.Algorithm.CiphertextSizeInBytes];
byte[] secret1 = new byte[sender.Algorithm.SharedSecretSizeInBytes];
sender.Encapsulate(ciphertext, secret1);
// Receiver:
byte[] secret2 = new byte[receiver.Algorithm.SharedSecretSizeInBytes];
receiver.Decapsulate(ciphertext, secret2);
// secret1 and secret2 are identical
Properties
Algorithm
Gets the ML-KEM parameter set of this key.
public MLKemAlgorithm Algorithm { get; }
Property Value
IsSupported
Gets a value indicating whether ML-KEM is supported on the current platform.
public static bool IsSupported { get; }
Property Value
Remarks
Always true. This is a fully managed implementation, so unlike
System.Security.Cryptography.MLKem.IsSupported it never depends on the
operating system providing ML-KEM.
Methods
Decapsulate(byte[])
Recovers the shared secret from a ciphertext using this key's decapsulation key, returning it as a new array.
public byte[] Decapsulate(byte[] ciphertext)
Parameters
ciphertextbyte[]The ciphertext; must be exactly CiphertextSizeInBytes bytes.
Returns
- byte[]
The shared secret.
Remarks
ML-KEM uses implicit rejection: an invalid ciphertext of the correct length does not throw, but yields a pseudorandom secret unrelated to the sender's.
Exceptions
- ArgumentNullException
ciphertextis null.- ArgumentException
ciphertexthas an incorrect length.- ObjectDisposedException
The instance has been disposed.
- CryptographicException
The instance holds no decapsulation key.
Decapsulate(ReadOnlySpan<byte>, Span<byte>)
Recovers the shared secret from a ciphertext using this key's decapsulation key.
public void Decapsulate(ReadOnlySpan<byte> ciphertext, Span<byte> sharedSecret)
Parameters
ciphertextReadOnlySpan<byte>The ciphertext; must be exactly CiphertextSizeInBytes bytes.
sharedSecretSpan<byte>The buffer to receive the shared secret; must be exactly SharedSecretSizeInBytes bytes.
Remarks
ML-KEM uses implicit rejection: an invalid ciphertext of the correct length does not throw, but yields a pseudorandom secret unrelated to the sender's.
Exceptions
- ArgumentException
A buffer has an incorrect length.
- ObjectDisposedException
The instance has been disposed.
- CryptographicException
The instance holds no decapsulation key.
Dispose()
Zeroizes the private seed and decapsulation key and releases the instance.
public void Dispose()
Encapsulate(out byte[], out byte[])
Creates a ciphertext and shared secret for the holder of this key's decapsulation key, returning both as new arrays.
public void Encapsulate(out byte[] ciphertext, out byte[] sharedSecret)
Parameters
ciphertextbyte[]When this method returns, the ciphertext.
sharedSecretbyte[]When this method returns, the shared secret.
Exceptions
- ObjectDisposedException
The instance has been disposed.
Encapsulate(Span<byte>, Span<byte>)
Creates a ciphertext and shared secret for the holder of this key's decapsulation key.
public void Encapsulate(Span<byte> ciphertext, Span<byte> sharedSecret)
Parameters
ciphertextSpan<byte>The buffer to receive the ciphertext; must be exactly CiphertextSizeInBytes bytes.
sharedSecretSpan<byte>The buffer to receive the shared secret; must be exactly SharedSecretSizeInBytes bytes.
Exceptions
- ArgumentException
A buffer has an incorrect length.
- ObjectDisposedException
The instance has been disposed.
- CryptographicException
The buffers overlap.
ExportDecapsulationKey()
Exports the expanded decapsulation (private) key.
public byte[] ExportDecapsulationKey()
Returns
- byte[]
The expanded decapsulation key.
Exceptions
- ObjectDisposedException
The instance has been disposed.
- CryptographicException
The instance holds no decapsulation key.
ExportDecapsulationKey(Span<byte>)
Exports the expanded decapsulation (private) key into a buffer.
public void ExportDecapsulationKey(Span<byte> destination)
Parameters
destinationSpan<byte>The buffer to receive the key; must be exactly DecapsulationKeySizeInBytes bytes.
Exceptions
- ArgumentException
destinationhas an incorrect length.- ObjectDisposedException
The instance has been disposed.
- CryptographicException
The instance holds no decapsulation key.
ExportEncapsulationKey()
Exports the encapsulation (public) key.
public byte[] ExportEncapsulationKey()
Returns
- byte[]
The encapsulation key.
Exceptions
- ObjectDisposedException
The instance has been disposed.
ExportEncapsulationKey(Span<byte>)
Exports the encapsulation (public) key into a buffer.
public void ExportEncapsulationKey(Span<byte> destination)
Parameters
destinationSpan<byte>The buffer to receive the key; must be exactly EncapsulationKeySizeInBytes bytes.
Exceptions
- ArgumentException
destinationhas an incorrect length.- ObjectDisposedException
The instance has been disposed.
ExportEncryptedPkcs8PrivateKey(ReadOnlySpan<byte>, PbeOptions)
Exports this key in the PKCS#8 EncryptedPrivateKeyInfo format.
public byte[] ExportEncryptedPkcs8PrivateKey(ReadOnlySpan<byte> password, PbeOptions pbeOptions)
Parameters
passwordReadOnlySpan<byte>The password, used verbatim as the key derivation input.
pbeOptionsPbeOptionsSelects the cipher, pseudorandom function and iteration count.
Returns
- byte[]
The DER encoding.
Exceptions
- ObjectDisposedException
The instance has been disposed.
- CryptographicException
The instance holds no private key.
ExportEncryptedPkcs8PrivateKey(ReadOnlySpan<char>, PbeOptions)
Exports this key in the PKCS#8 EncryptedPrivateKeyInfo format.
public byte[] ExportEncryptedPkcs8PrivateKey(ReadOnlySpan<char> password, PbeOptions pbeOptions)
Parameters
passwordReadOnlySpan<char>The password.
pbeOptionsPbeOptionsSelects the cipher, pseudorandom function and iteration count.
Returns
- byte[]
The DER encoding.
Exceptions
- ObjectDisposedException
The instance has been disposed.
- CryptographicException
The instance holds no private key.
ExportEncryptedPkcs8PrivateKeyPem(ReadOnlySpan<byte>, PbeOptions)
Exports this key in a PEM-encoded PKCS#8 EncryptedPrivateKeyInfo.
public string ExportEncryptedPkcs8PrivateKeyPem(ReadOnlySpan<byte> password, PbeOptions pbeOptions)
Parameters
passwordReadOnlySpan<byte>The password, used verbatim as the key derivation input.
pbeOptionsPbeOptionsSelects the cipher, pseudorandom function and iteration count.
Returns
- string
The PEM text, labelled
ENCRYPTED PRIVATE KEY.
Exceptions
- ObjectDisposedException
The instance has been disposed.
- CryptographicException
The instance holds no private key.
ExportEncryptedPkcs8PrivateKeyPem(ReadOnlySpan<char>, PbeOptions)
Exports this key in a PEM-encoded PKCS#8 EncryptedPrivateKeyInfo.
public string ExportEncryptedPkcs8PrivateKeyPem(ReadOnlySpan<char> password, PbeOptions pbeOptions)
Parameters
passwordReadOnlySpan<char>The password.
pbeOptionsPbeOptionsSelects the cipher, pseudorandom function and iteration count.
Returns
- string
The PEM text, labelled
ENCRYPTED PRIVATE KEY.
Exceptions
- ObjectDisposedException
The instance has been disposed.
- CryptographicException
The instance holds no private key.
ExportPkcs8PrivateKey()
Exports this key in the PKCS#8 PrivateKeyInfo format.
public byte[] ExportPkcs8PrivateKey()
Returns
- byte[]
The DER encoding.
Exceptions
- ObjectDisposedException
The instance has been disposed.
- CryptographicException
The instance holds no private key.
ExportPrivateSeed()
Exports the 64-byte private seed (d ‖ z).
public byte[] ExportPrivateSeed()
Returns
- byte[]
The private seed.
Exceptions
- ObjectDisposedException
The instance has been disposed.
- CryptographicException
The key was not created from a seed.
ExportPrivateSeed(Span<byte>)
Exports the 64-byte private seed (d ‖ z) into a buffer.
public void ExportPrivateSeed(Span<byte> destination)
Parameters
destinationSpan<byte>The buffer to receive the seed; must be exactly PrivateSeedSizeInBytes bytes.
Exceptions
- ArgumentException
destinationhas an incorrect length.- ObjectDisposedException
The instance has been disposed.
- CryptographicException
The key was not created from a seed.
ExportSubjectPublicKeyInfo()
Exports the encapsulation key in the X.509 SubjectPublicKeyInfo format.
public byte[] ExportSubjectPublicKeyInfo()
Returns
- byte[]
The DER encoding.
Exceptions
- ObjectDisposedException
The instance has been disposed.
ExportSubjectPublicKeyInfoPem()
Exports the encapsulation key in a PEM-encoded SubjectPublicKeyInfo.
public string ExportSubjectPublicKeyInfoPem()
Returns
- string
The PEM text, labelled
PUBLIC KEY.
Exceptions
- ObjectDisposedException
The instance has been disposed.
GenerateKey(MLKemAlgorithm)
Generates a new ML-KEM key pair.
public static MLKem GenerateKey(MLKemAlgorithm algorithm)
Parameters
algorithmMLKemAlgorithmThe parameter set to generate a key for.
Returns
- MLKem
A new instance holding the generated key pair and its private seed.
Exceptions
- ArgumentNullException
algorithmis null.
GenerateKey(MLKemAlgorithm, bool)
Generates a new ML-KEM key pair, optionally skipping the pairwise consistency test.
public static MLKem GenerateKey(MLKemAlgorithm algorithm, bool pairwiseConsistencyTest)
Parameters
algorithmMLKemAlgorithmThe parameter set to generate a key for.
pairwiseConsistencyTestbooltrue to verify the generated key pair with an encapsulate/decapsulate round trip; false to skip it.
Returns
- MLKem
A new instance holding the generated key pair and its private seed.
Remarks
The pairwise consistency test verifies a freshly expanded key pair by running one encapsulate/decapsulate round trip, as FIPS 140-3 IG 10.3.A expects of a validated module. It costs roughly four fifths of key generation, because that round trip is a full encapsulation plus a full decapsulation.
It guards against a fault — bad memory, a bit flip, a miscompiled build — producing a key pair that does not round-trip. It cannot catch an implementation bug, since both halves of the test would be wrong in the same way. Disable it only where that trade is understood and key generation throughput actually matters; the default on the BCL-shaped overloads keeps it enabled.
Exceptions
- ArgumentNullException
algorithmis null.- CryptographicException
The key pair failed the consistency test.
GetPkcs8PrivateKeyPemSize()
Gets the exact number of characters TryExportPkcs8PrivateKeyPem(Span<char>, out int) writes for this key.
public int GetPkcs8PrivateKeyPemSize()
Returns
- int
The required buffer length, in characters.
Remarks
The counterpart of System.Security.Cryptography.PemEncoding.GetEncodedSize, which is
not available on every target framework this library supports. Rent a buffer of this size,
call TryExportPkcs8PrivateKeyPem(Span<char>, out int), and clear the buffer when finished.
Exceptions
- ObjectDisposedException
The instance has been disposed.
- CryptographicException
The instance holds no private key.
GetSubjectPublicKeyInfoPemSize()
Gets the exact number of characters TryExportSubjectPublicKeyInfoPem(Span<char>, out int) writes.
public int GetSubjectPublicKeyInfoPemSize()
Returns
- int
The required buffer length, in characters.
Exceptions
- ObjectDisposedException
The instance has been disposed.
ImportDecapsulationKey(MLKemAlgorithm, byte[])
Imports an expanded ML-KEM decapsulation key.
public static MLKem ImportDecapsulationKey(MLKemAlgorithm algorithm, byte[] source)
Parameters
algorithmMLKemAlgorithmThe parameter set of the key.
sourcebyte[]The expanded decapsulation key.
Returns
- MLKem
A new instance holding the key pair.
Remarks
The key is validated with the FIPS 203 §7.3 hash check. A key imported this way holds no private seed, so ExportPrivateSeed() is unavailable.
Exceptions
- ArgumentNullException
algorithmorsourceis null.- ArgumentException
sourcehas an invalid length.- CryptographicException
The key failed the FIPS 203 §7.3 hash check.
ImportDecapsulationKey(MLKemAlgorithm, ReadOnlySpan<byte>)
Imports an expanded ML-KEM decapsulation key.
public static MLKem ImportDecapsulationKey(MLKemAlgorithm algorithm, ReadOnlySpan<byte> source)
Parameters
algorithmMLKemAlgorithmThe parameter set of the key.
sourceReadOnlySpan<byte>The expanded decapsulation key.
Returns
- MLKem
A new instance holding the key pair.
Remarks
The key is validated with the FIPS 203 §7.3 hash check. A key imported this way holds no private seed, so ExportPrivateSeed() is unavailable.
Exceptions
- ArgumentNullException
algorithmis null.- ArgumentException
sourcehas an invalid length.- CryptographicException
The key failed the FIPS 203 §7.3 hash check.
ImportEncapsulationKey(MLKemAlgorithm, byte[])
Imports an ML-KEM encapsulation (public) key.
public static MLKem ImportEncapsulationKey(MLKemAlgorithm algorithm, byte[] source)
Parameters
algorithmMLKemAlgorithmThe parameter set of the key.
sourcebyte[]The encapsulation key.
Returns
- MLKem
A new instance holding only the public key.
Remarks
The key is validated with the FIPS 203 §7.2 modulus check. The resulting instance can encapsulate but not decapsulate.
Exceptions
- ArgumentNullException
algorithmorsourceis null.- ArgumentException
sourcehas an invalid length.- CryptographicException
The key failed the FIPS 203 §7.2 modulus check.
ImportEncapsulationKey(MLKemAlgorithm, ReadOnlySpan<byte>)
Imports an ML-KEM encapsulation (public) key.
public static MLKem ImportEncapsulationKey(MLKemAlgorithm algorithm, ReadOnlySpan<byte> source)
Parameters
algorithmMLKemAlgorithmThe parameter set of the key.
sourceReadOnlySpan<byte>The encapsulation key.
Returns
- MLKem
A new instance holding only the public key.
Remarks
The key is validated with the FIPS 203 §7.2 modulus check. The resulting instance can encapsulate but not decapsulate.
Exceptions
- ArgumentNullException
algorithmis null.- ArgumentException
sourcehas an invalid length.- CryptographicException
The key failed the FIPS 203 §7.2 modulus check.
ImportEncryptedPkcs8PrivateKey(ReadOnlySpan<byte>, ReadOnlySpan<byte>)
Imports an ML-KEM private key from a PKCS#8 EncryptedPrivateKeyInfo structure.
public static MLKem ImportEncryptedPkcs8PrivateKey(ReadOnlySpan<byte> password, ReadOnlySpan<byte> source)
Parameters
passwordReadOnlySpan<byte>The password, used verbatim as the key derivation input.
sourceReadOnlySpan<byte>The DER-encoded EncryptedPrivateKeyInfo.
Returns
- MLKem
The imported key.
Exceptions
- CryptographicException
The password is wrong, or the structure is malformed or is not ML-KEM.
ImportEncryptedPkcs8PrivateKey(ReadOnlySpan<char>, ReadOnlySpan<byte>)
Imports an ML-KEM private key from a PKCS#8 EncryptedPrivateKeyInfo structure.
public static MLKem ImportEncryptedPkcs8PrivateKey(ReadOnlySpan<char> password, ReadOnlySpan<byte> source)
Parameters
passwordReadOnlySpan<char>The password, encoded per the scheme in the structure.
sourceReadOnlySpan<byte>The DER-encoded EncryptedPrivateKeyInfo.
Returns
- MLKem
The imported key.
Exceptions
- CryptographicException
The password is wrong, or the structure is malformed or is not ML-KEM.
ImportFromEncryptedPem(ReadOnlySpan<char>, ReadOnlySpan<byte>)
Imports an ML-KEM key from an encrypted RFC 7468 PEM-encoded string.
public static MLKem ImportFromEncryptedPem(ReadOnlySpan<char> source, ReadOnlySpan<byte> passwordBytes)
Parameters
sourceReadOnlySpan<char>The PEM text.
passwordBytesReadOnlySpan<byte>The password, used verbatim as the key derivation input.
Returns
- MLKem
The imported key.
Exceptions
- ArgumentException
sourcecontains no encrypted private key, or more than one.
ImportFromEncryptedPem(ReadOnlySpan<char>, ReadOnlySpan<char>)
Imports an ML-KEM key from an encrypted RFC 7468 PEM-encoded string.
public static MLKem ImportFromEncryptedPem(ReadOnlySpan<char> source, ReadOnlySpan<char> password)
Parameters
sourceReadOnlySpan<char>The PEM text.
passwordReadOnlySpan<char>The password.
Returns
- MLKem
The imported key.
Exceptions
- ArgumentException
sourcecontains no encrypted private key, or more than one.
ImportFromEncryptedPem(string, byte[])
Imports an ML-KEM key from an encrypted RFC 7468 PEM-encoded string.
public static MLKem ImportFromEncryptedPem(string source, byte[] passwordBytes)
Parameters
sourcestringThe PEM text.
passwordBytesbyte[]The password, used verbatim as the key derivation input.
Returns
- MLKem
The imported key.
Exceptions
- ArgumentNullException
An argument is null.
- ArgumentException
sourcecontains no encrypted private key, or more than one.
ImportFromPem(ReadOnlySpan<char>)
Imports an ML-KEM key from an RFC 7468 PEM-encoded string.
public static MLKem ImportFromPem(ReadOnlySpan<char> source)
Parameters
sourceReadOnlySpan<char>The PEM text.
Returns
- MLKem
The imported key.
Exceptions
- ArgumentException
sourcecontains no PEM block with a recognized label, or more than one.
ImportPkcs8PrivateKey(byte[])
Imports an ML-KEM private key from a PKCS#8 PrivateKeyInfo structure.
public static MLKem ImportPkcs8PrivateKey(byte[] source)
Parameters
sourcebyte[]The DER-encoded PrivateKeyInfo.
Returns
- MLKem
The imported key.
Exceptions
- ArgumentNullException
sourceis null.- CryptographicException
The structure is malformed or is not ML-KEM.
ImportPkcs8PrivateKey(ReadOnlySpan<byte>)
Imports an ML-KEM private key from a PKCS#8 PrivateKeyInfo structure.
public static MLKem ImportPkcs8PrivateKey(ReadOnlySpan<byte> source)
Parameters
sourceReadOnlySpan<byte>The DER-encoded PrivateKeyInfo.
Returns
- MLKem
The imported key.
Exceptions
- CryptographicException
The structure is malformed or is not ML-KEM.
ImportPrivateSeed(MLKemAlgorithm, byte[])
Imports an ML-KEM private seed (d ‖ z) and expands it into a key pair.
public static MLKem ImportPrivateSeed(MLKemAlgorithm algorithm, byte[] source)
Parameters
algorithmMLKemAlgorithmThe parameter set of the key.
sourcebyte[]The 64-byte private seed.
Returns
- MLKem
A new instance holding the key pair and the seed.
Exceptions
- ArgumentNullException
algorithmorsourceis null.- ArgumentException
sourcehas an invalid length.
ImportPrivateSeed(MLKemAlgorithm, ReadOnlySpan<byte>)
Imports an ML-KEM private seed (d ‖ z) and expands it into a key pair.
public static MLKem ImportPrivateSeed(MLKemAlgorithm algorithm, ReadOnlySpan<byte> source)
Parameters
algorithmMLKemAlgorithmThe parameter set of the key.
sourceReadOnlySpan<byte>The 64-byte private seed.
Returns
- MLKem
A new instance holding the key pair and the seed.
Exceptions
- ArgumentNullException
algorithmis null.- ArgumentException
sourcehas an invalid length.
ImportPrivateSeed(MLKemAlgorithm, ReadOnlySpan<byte>, bool)
Imports an ML-KEM private seed (d ‖ z) and expands it into a key pair, optionally skipping the pairwise consistency test.
public static MLKem ImportPrivateSeed(MLKemAlgorithm algorithm, ReadOnlySpan<byte> source, bool pairwiseConsistencyTest)
Parameters
algorithmMLKemAlgorithmThe parameter set of the key.
sourceReadOnlySpan<byte>The 64-byte private seed.
pairwiseConsistencyTestbool
Returns
- MLKem
A new instance holding the key pair and the seed.
Remarks
The pairwise consistency test verifies a freshly expanded key pair by running one encapsulate/decapsulate round trip, as FIPS 140-3 IG 10.3.A expects of a validated module. It costs roughly four fifths of key generation, because that round trip is a full encapsulation plus a full decapsulation.
It guards against a fault — bad memory, a bit flip, a miscompiled build — producing a key pair that does not round-trip. It cannot catch an implementation bug, since both halves of the test would be wrong in the same way. Disable it only where that trade is understood and key generation throughput actually matters; the default on the BCL-shaped overloads keeps it enabled.
Exceptions
- ArgumentNullException
algorithmis null.- ArgumentException
sourcehas an invalid length.- CryptographicException
The key pair failed the consistency test.
ImportSubjectPublicKeyInfo(byte[])
Imports an ML-KEM encapsulation key from an X.509 SubjectPublicKeyInfo structure.
public static MLKem ImportSubjectPublicKeyInfo(byte[] source)
Parameters
sourcebyte[]The DER-encoded SubjectPublicKeyInfo.
Returns
- MLKem
An encapsulation-only instance.
Exceptions
- ArgumentNullException
sourceis null.- CryptographicException
The structure is malformed or is not ML-KEM.
ImportSubjectPublicKeyInfo(ReadOnlySpan<byte>)
Imports an ML-KEM encapsulation key from an X.509 SubjectPublicKeyInfo structure.
public static MLKem ImportSubjectPublicKeyInfo(ReadOnlySpan<byte> source)
Parameters
sourceReadOnlySpan<byte>The DER-encoded SubjectPublicKeyInfo.
Returns
- MLKem
An encapsulation-only instance.
Exceptions
- CryptographicException
The structure is malformed or is not ML-KEM.
TryExportEncryptedPkcs8PrivateKey(ReadOnlySpan<byte>, PbeOptions, Span<byte>, out int)
Attempts to export this key in the PKCS#8 EncryptedPrivateKeyInfo format.
public bool TryExportEncryptedPkcs8PrivateKey(ReadOnlySpan<byte> password, PbeOptions pbeOptions, Span<byte> destination, out int bytesWritten)
Parameters
passwordReadOnlySpan<byte>The password, used verbatim as the key derivation input.
pbeOptionsPbeOptionsSelects the cipher, pseudorandom function and iteration count.
destinationSpan<byte>The buffer to receive the encoding.
bytesWrittenintThe number of bytes written.
Returns
Exceptions
- ObjectDisposedException
The instance has been disposed.
- CryptographicException
The instance holds no private key.
TryExportEncryptedPkcs8PrivateKey(ReadOnlySpan<char>, PbeOptions, Span<byte>, out int)
Attempts to export this key in the PKCS#8 EncryptedPrivateKeyInfo format.
public bool TryExportEncryptedPkcs8PrivateKey(ReadOnlySpan<char> password, PbeOptions pbeOptions, Span<byte> destination, out int bytesWritten)
Parameters
passwordReadOnlySpan<char>The password.
pbeOptionsPbeOptionsSelects the cipher, pseudorandom function and iteration count.
destinationSpan<byte>The buffer to receive the encoding.
bytesWrittenintThe number of bytes written.
Returns
Exceptions
- ObjectDisposedException
The instance has been disposed.
- CryptographicException
The instance holds no private key.
TryExportEncryptedPkcs8PrivateKeyPem(ReadOnlySpan<byte>, PbeOptions, Span<char>, out int)
Attempts to export this key as a PEM-encoded PKCS#8 EncryptedPrivateKeyInfo.
public bool TryExportEncryptedPkcs8PrivateKeyPem(ReadOnlySpan<byte> password, PbeOptions pbeOptions, Span<char> destination, out int charsWritten)
Parameters
passwordReadOnlySpan<byte>The password, used verbatim as the key derivation input.
pbeOptionsPbeOptionsSelects the cipher, pseudorandom function and iteration count.
destinationSpan<char>The buffer to receive the text.
charsWrittenintThe number of characters written.
Returns
Exceptions
- ObjectDisposedException
The instance has been disposed.
- CryptographicException
The instance holds no private key.
TryExportEncryptedPkcs8PrivateKeyPem(ReadOnlySpan<char>, PbeOptions, Span<char>, out int)
Attempts to export this key as a PEM-encoded PKCS#8 EncryptedPrivateKeyInfo.
public bool TryExportEncryptedPkcs8PrivateKeyPem(ReadOnlySpan<char> password, PbeOptions pbeOptions, Span<char> destination, out int charsWritten)
Parameters
passwordReadOnlySpan<char>The password, encoded per the scheme being written.
pbeOptionsPbeOptionsSelects the cipher, pseudorandom function and iteration count.
destinationSpan<char>The buffer to receive the text.
charsWrittenintThe number of characters written.
Returns
Remarks
There is no size helper for this member: measuring the output means running the key derivation function, which at a realistic iteration count is the whole cost of the export. Use ExportEncryptedPkcs8PrivateKeyPem(ReadOnlySpan<char>, PbeOptions) - safe, because the payload is ciphertext - or grow a buffer until this succeeds.
Exceptions
- ObjectDisposedException
The instance has been disposed.
- CryptographicException
The instance holds no private key.
TryExportPkcs8PrivateKey(Span<byte>, out int)
Attempts to export this key in the PKCS#8 PrivateKeyInfo format.
public bool TryExportPkcs8PrivateKey(Span<byte> destination, out int bytesWritten)
Parameters
destinationSpan<byte>The buffer to receive the encoding.
bytesWrittenintThe number of bytes written.
Returns
Exceptions
- ObjectDisposedException
The instance has been disposed.
- CryptographicException
The instance holds no private key.
TryExportPkcs8PrivateKeyPem(Span<char>, out int)
Attempts to export this key as a PEM-encoded PKCS#8 PrivateKeyInfo, writing into a buffer the caller owns and can erase.
public bool TryExportPkcs8PrivateKeyPem(Span<char> destination, out int charsWritten)
Parameters
destinationSpan<char>The buffer to receive the text, labelled
PRIVATE KEY.charsWrittenintThe number of characters written.
Returns
Remarks
There is no allocating ExportPkcs8PrivateKeyPem(). The text this produces is
the private key, and a string holding it could never be erased - so the only
export this type offers is one that writes where the caller can clear it afterwards. Size
the buffer with GetPkcs8PrivateKeyPemSize().
When the buffer is too small nothing is written and charsWritten is zero.
Exceptions
- ObjectDisposedException
The instance has been disposed.
- CryptographicException
The instance holds no private key.
TryExportSubjectPublicKeyInfo(Span<byte>, out int)
Attempts to export the encapsulation key as a SubjectPublicKeyInfo.
public bool TryExportSubjectPublicKeyInfo(Span<byte> destination, out int bytesWritten)
Parameters
destinationSpan<byte>The buffer to receive the encoding.
bytesWrittenintThe number of bytes written.
Returns
Exceptions
- ObjectDisposedException
The instance has been disposed.
TryExportSubjectPublicKeyInfoPem(Span<char>, out int)
Attempts to export this key as a PEM-encoded SubjectPublicKeyInfo.
public bool TryExportSubjectPublicKeyInfoPem(Span<char> destination, out int charsWritten)
Parameters
destinationSpan<char>The buffer to receive the text, labelled
PUBLIC KEY.charsWrittenintThe number of characters written.
Returns
Remarks
A public key is not secret, so ExportSubjectPublicKeyInfoPem() remains available
and is usually the simpler call. This overload exists for parity with
AsymmetricAlgorithm, which offers the same pair, and for callers avoiding allocation.
Exceptions
- ObjectDisposedException
The instance has been disposed.