如何使用c中的bouncycastle将pem公钥转换为rsa公钥

本文关键字:公钥 转换 rsa pem 何使用 中的 bouncycastle | 更新日期: 2023-09-27 18:22:17

我有一个pem公钥,我想转换为xml格式的公钥或AsymmetricKeyParameter。

我可以在C#的bouncyCastle中使用PemReader将pemPrivate密钥转换为Public/Privatexml格式或asymmetricKeyParameter。但在PemReader中使用pemPublic时,我收到错误。

请帮帮我。
我的问题还有什么解决办法?

如何使用c中的bouncycastle将pem公钥转换为rsa公钥

这应该可以满足您使用BouncyCastle的需求。

依赖项:

using Org.BouncyCastle.Crypto;
using Org.BouncyCastle.Crypto.Parameters;
using Org.BouncyCastle.OpenSsl;
using Org.BouncyCastle.Security;

从PEM转换为RSA XML格式的代码:

StreamReader reader = new StreamReader("yourPrivateKey.pem");
PemReader pemReader = new PemReader(reader);
AsymmetricCipherKeyPair keyPair = (AsymmetricCipherKeyPair)pemReader.ReadObject();
AsymmetricKeyParameter privateKey = keyPair.Private;
RSA rsa = DotNetUtilities.ToRSA((RsaPrivateCrtKeyParameters) privateKey);
string xmlRsa = rsa.ToXmlString(true);
Console.WriteLine(xmlRsa);

从Microsoft论坛浏览到Bell_Wang回复,看看这个条目,它指向了一些为您进行转换的代码(代码在这里)