仅使用Bouncy Castle阅读PEM RSA公钥

我正在尝试使用C#读取仅包含RSA公钥的.pem文件。 我无权访问私钥信息,我的应用程序也不需要它。 文件myprivatekey.pem文件以。开头

-----BEGIN PUBLIC KEY-----并以-----END PUBLIC KEY-----

我目前的代码如下:

  Org.BouncyCastle.Crypto.AsymmetricCipherKeyPair keyPair; using (var reader = File.OpenText(@"c:\keys\myprivatekey.pem")) keyPair = (Org.BouncyCastle.Crypto.AsymmetricCipherKeyPair)new Org.BouncyCastle.OpenSsl.PemReader(reader).ReadObject(); 

但是,代码会抛出带有消息的InvalidCastException

无法将“Org.BouncyCastle.Crypto.Parameters.DsaPublicKeyParameters”类型的对象强制转换为“Org.BouncyCastle.Crypto.AsymmetricCipherKeyPair”。

当没有私钥信息时,如何使用Bouncy Castle的PemReader只读取公钥?

以下代码仅在给定文件名的情况下读取公钥。 应该为任何生产代码更改exception处理。 此方法返回AsymetricKeyParameter

 public Org.BouncyCastle.Crypto.AsymmetricKeyParameter ReadAsymmetricKeyParameter(string pemFilename) { var fileStream = System.IO.File.OpenText (pemFilename); var pemReader = new Org.BouncyCastle.OpenSsl.PemReader (fileStream); var KeyParameter = (Org.BouncyCastle.Crypto.AsymmetricKeyParameter)pemReader.ReadObject (); return KeyParameter; } 

这是一个可能的解决方案,它将公共和私有PEM文件读入RSACryptoServiceProvider:

 public class PemReaderB { public static RSACryptoServiceProvider GetRSAProviderFromPem(String pemstr) { CspParameters cspParameters = new CspParameters(); cspParameters.KeyContainerName = "MyKeyContainer"; RSACryptoServiceProvider rsaKey = new RSACryptoServiceProvider(cspParameters); Func MakePublicRCSP = (RSACryptoServiceProvider rcsp, RsaKeyParameters rkp) => { RSAParameters rsaParameters = DotNetUtilities.ToRSAParameters(rkp); rcsp.ImportParameters(rsaParameters); return rsaKey; }; Func MakePrivateRCSP = (RSACryptoServiceProvider rcsp, RsaPrivateCrtKeyParameters rkp) => { RSAParameters rsaParameters = DotNetUtilities.ToRSAParameters(rkp); rcsp.ImportParameters(rsaParameters); return rsaKey; }; PemReader reader = new PemReader(new StringReader(pemstr)); object kp = reader.ReadObject(); // If object has Private/Public property, we have a Private PEM return (kp.GetType().GetProperty("Private") != null) ? MakePrivateRCSP(rsaKey, (RsaPrivateCrtKeyParameters)(((AsymmetricCipherKeyPair)kp).Private)) : MakePublicRCSP(rsaKey, (RsaKeyParameters)kp); } public static RSACryptoServiceProvider GetRSAProviderFromPemFile(String pemfile) { return GetRSAProviderFromPem(File.ReadAllText(pemfile).Trim()); } } 

希望这有助于某人。

在回答c0d3Junk13时,我对PEM私钥有同样的问题,整个下午我都花了很多时间才找到使用C#BouncyCastle V1.7和Visual Studio 2013 Desktop Express的解决方案。 不要忘记将项目引用添加到BouncyCastle.Crypto.dll

 using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using System.Collections; using System.IO; using Org.BouncyCastle.Asn1.X509; using Org.BouncyCastle.Asn1.Pkcs; using Org.BouncyCastle.Crypto.Digests; using Org.BouncyCastle.Crypto.Parameters; using Org.BouncyCastle.Crypto.Signers; using Org.BouncyCastle.X509; using Org.BouncyCastle.Math; using Org.BouncyCastle.Math.EC; using Org.BouncyCastle.Utilities.Collections; using Org.BouncyCastle.Utilities.Encoders; using Org.BouncyCastle.Crypto; using Org.BouncyCastle.Crypto.Engines; using Org.BouncyCastle.OpenSsl; /* For an Active Directory generated pem, strip out everything in pem file before line: "-----BEGIN PRIVATE KEY-----" and re-save. */ string privateKeyFileName = @"C:\CertificateTest\CS\bccrypto-net-1.7-bin\private_key3.pem"; TextReader reader = File.OpenText(privateKeyFileName); Org.BouncyCastle.Crypto.Parameters.RsaPrivateCrtKeyParameters key; using (reader = File.OpenText(privateKeyFileName)) { key = (Org.BouncyCastle.Crypto.Parameters.RsaPrivateCrtKeyParameters)new PemReader(reader).ReadObject(); } cipher.Init(false, key); //Decrypting the input bytes byte[] decipheredBytes = cipher.ProcessBlock(cipheredBytes, 0, cipheredBytes.Length); MessageBox.Show(Encoding.UTF8.GetString(decipheredBytes));