c# 如何从 cngKey

问题描述

我使用的是 C#、.NET Core 3.1(如果需要,也可以使用 BouncyCastle)。

我有我的 EC 私钥(我称之为 bobKey),我有一个由第三方发送的临时公钥(我称之为 aliceKey)。请问如何获取 aliceKey 的 X 和 Y?

using var ecdh = new ECDiffieHellmanCng();
ecdh.KeyDerivationFunction = ECDiffieHellmanKeyDerivationFunction.Hash; // Note sure if I need this but I have it in third party too.
ecdh.HashAlgorithm = Cngalgorithm.Sha256; // Note sure if I need this but I have it in third party too.

ecdh.ImportECPrivateKey(bobKey,out var _);

CngKey importedKey = ImportEcpublicKey(aliceKey);

var derivedSymmetricKey= ecdh.DeriveKeyMaterial(importedKey);

MyLogger.LogIt("derivedSymmetricKey:" + PrintByteArray(derivedSymmetricKey));  // I can see that derivedSymmetricKey is the same when observing from third party code!

// I have realised that these 2 lines are actually showing bobs public key X and Y which is not what I want.
var x = ecdh.PublicKey.ExportParameters().Q.X;
var y = ecdh.PublicKey.ExportParameters().Q.Y;

MyLogger.LogIt("x:" + PrintByteArray(x));
MyLogger.LogIt("y:" + PrintByteArray(y));

我需要 X 和 Y 的是 aliceKey / importedKey 但我如何从变量类型 CngKey获取它?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)