/// <summary> /// Get a handle to the secret agreement generated between two parties /// </summary> public SafeNCryptSecretHandle DeriveSecretAgreementHandle(ECDiffieHellmanPublicKey otherPartyPublicKey) { ArgumentNullException.ThrowIfNull(otherPartyPublicKey); if (otherPartyPublicKey is ECDiffieHellmanCngPublicKey otherKey) { using (CngKey importedKey = otherKey.Import()) { return(DeriveSecretAgreementHandle(importedKey)); } } ECParameters otherPartyParameters = otherPartyPublicKey.ExportParameters(); using (ECDiffieHellmanCng otherPartyCng = new ECDiffieHellmanCng()) { otherPartyCng.ImportParameters(otherPartyParameters); using (otherKey = (ECDiffieHellmanCngPublicKey)otherPartyCng.PublicKey) using (CngKey importedKey = otherKey.Import()) { return(DeriveSecretAgreementHandle(importedKey)); } } }
public override byte[] DeriveKeyMaterial(ECDiffieHellmanPublicKey otherPartyPublicKey) { ArgumentNullException.ThrowIfNull(otherPartyPublicKey); if (otherPartyPublicKey is ECDiffieHellmanCngPublicKey otherKey) { using (CngKey import = otherKey.Import()) { return(DeriveKeyMaterial(import)); } } // This deviates from the .NET Framework behavior. .NET Framework can't handle unknown public // key types, but on .NET Core there are automatically two: the public class produced by // this class' PublicKey member, and the private class produced by ECDiffieHellman.Create().PublicKey // // So let's just work. ECParameters otherPartyParameters = otherPartyPublicKey.ExportParameters(); using (ECDiffieHellmanCng otherPartyCng = new ECDiffieHellmanCng()) { otherPartyCng.ImportParameters(otherPartyParameters); using (otherKey = (ECDiffieHellmanCngPublicKey)otherPartyCng.PublicKey) using (CngKey importedKey = otherKey.Import()) { return(DeriveKeyMaterial(importedKey)); } } }