【问题标题】: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 ,我收到错误。

请帮帮我。
我的问题还有什么解决方案?

【问题讨论】:

  • 有人知道这个问题吗?
  • 你试过用openssl来做转换吗?

标签: c# rsa bouncycastle public-key pem


【解决方案1】:

查看此条目from Microsoft forums 向下浏览至 Bell_Wang 回复,它指向一些代码,可以为您进行转换 (code is here)

【讨论】:

    【解决方案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);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-09-22
      • 1970-01-01
      • 2014-11-27
      • 2017-10-14
      • 2011-04-03
      • 2015-10-19
      • 1970-01-01
      • 2017-11-11
      相关资源
      最近更新 更多