gpt4 book ai didi

c# - 在 Bouncy CaSTLe C# 中从私钥获取公钥

转载 作者:行者123 更新时间:2023-12-03 18:41:50 25 4
gpt4 key购买 nike

所以我有一个加密的私钥 PEM。我可以阅读它并通过以下方式获取私钥:

AsymmetricKeyParameter key;
using (var sr = new StringReader(pem))
using (var pf = new PassowrdFinder { Password = password })
{
var reader = new PemReader(sr, pf);
key = (AsymmetricKeyParameter)reader.ReadObject();
}

我还需要公钥,以便稍后创建 SPKI。我试过了

var keyPair = new AsymmetricCipherKeyPair(key, key);

失败并显示 System.ArgumentException: Expected a public key Parameter name: publicParameter

我的问题是:如何从私钥中获取公钥?

最佳答案

感谢 James K Polk 的帮助,这是我的想法

    AsymmetricCipherKeyPair GetKeyPairFromPrivateKey(AsymmetricKeyParameter privateKey)
{
AsymmetricCipherKeyPair keyPair = null;
if (privateKey is RsaPrivateCrtKeyParameters rsa)
{
var pub = new RsaKeyParameters(false, rsa.Modulus, rsa.PublicExponent);
keyPair = new AsymmetricCipherKeyPair(pub, privateKey);
}
else if (privateKey is Ed25519PrivateKeyParameters ed)
{
var pub = ed.GeneratePublicKey();
keyPair = new AsymmetricCipherKeyPair(pub, privateKey);
}
else if (privateKey is ECPrivateKeyParameters ec)
{
var q = ec.Parameters.G.Multiply(ec.D);
var pub = new ECPublicKeyParameters(ec.AlgorithmName, q, ec.PublicKeyParamSet);
keyPair = new AsymmetricCipherKeyPair(pub, ec);
}
if (keyPair == null)
throw new NotSupportedException($"The key type {privateKey.GetType().Name} is not supported.");

return keyPair;
}

关于c# - 在 Bouncy CaSTLe C# 中从私钥获取公钥,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48312473/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com