【问题标题】:how can i convert pem public key to rsa public key with bouncycastle in c#?如何在 c# 中使用 bouncycastle 将 pem 公钥转换为 rsa 公钥?
【发布时间】:2012-02-23 21:38:18
【问题描述】:
我有一个 pem public 密钥,我想转换为 xml 格式的公钥或 AsymmetricKeyParameter。
我可以在 C# 的 bouncyCastle 中使用 PemReader 将 pem Private 密钥转换为 Public/Private xml 格式或 asymmetricKeyParameter。但是当使用 Pem Public键入 PemReader ,我收到错误。
请帮帮我。
我的问题还有什么解决方案?
【问题讨论】:
标签:
c#
rsa
bouncycastle
public-key
pem
【解决方案2】:
这应该可以满足您使用 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);