微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

c# 如何从 cngKey

如何解决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 举报,一经查实,本站将立刻删除。