我有一个 pem公钥,我想转换为 xml 格式的公钥或 AsymmetricKeyParameter。
我可以在 C# 中使用 bouncyCastle 中的 PemReader将 pem私钥转换为Public/Private xml 格式或非对称密钥参数。但是当在 PemReader 中使用 Pem公钥时,我收到错误。
请帮我。
我的问题还有什么解决方案?
我有一个 pem公钥,我想转换为 xml 格式的公钥或 AsymmetricKeyParameter。
我可以在 C# 中使用 bouncyCastle 中的 PemReader将 pem私钥转换为Public/Private xml 格式或非对称密钥参数。但是当在 PemReader 中使用 Pem公钥时,我收到错误。
请帮我。
我的问题还有什么解决方案?
这应该可以满足您使用 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);