无法将“Org.BouncyCastle.Asn1.DerSequence”类型的对象转换为“Org.BouncyCastle.Asn1.DerInteger”类型
Posted
技术标签:
【中文标题】无法将“Org.BouncyCastle.Asn1.DerSequence”类型的对象转换为“Org.BouncyCastle.Asn1.DerInteger”类型【英文标题】:Unable to cast object of type 'Org.BouncyCastle.Asn1.DerSequence' to type 'Org.BouncyCastle.Asn1.DerInteger' 【发布时间】:2011-08-21 03:09:02 【问题描述】:在我的 c# 项目中,我放了这段代码:
最初,我从使用 bouncy castle 密钥对创建的证书中恢复,然后提取私钥,我的目标是,它是一种格式。佩姆,
AsymmetricKeyParameter private_RSA = keyPair2.Private;
PrivateKeyInfo k_RSA = PrivateKeyInfoFactory.CreatePrivateKeyInfo(private_RSA);
byte[] serializedKey_priv_RSA = k_RSA.ToAsn1Object().GetDerEncoded();
// byte[] clé = Org.BouncyCastle.Utilities.Encoders.Hex.Decode(serializedKey);
string data_priv_RSA = Convert.ToBase64String(serializedKey_priv_RSA);
using (fluxInfos2 = new StreamWriter("myprivatekey.pem"))
string ligne = " -----BEGIN RSA PRIVATE KEY----- ";
fluxInfos2.WriteLine(ligne);
fluxInfos2.WriteLine(data_priv_RSA);
string lige2 = "-----END RSA PRIVATE KEY----- ";
fluxInfos2.WriteLine(lige2);
fluxInfos2.Close();
我的密钥是完美创建的,即文件包含的内容;
-----BEGIN RSA PRIVATE KEY-----
MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQCQBr1EhPxHeVax8QbXQtUwaSyqhZehjxkgVp89Rkn3awfo7f9usQdLSp3tLSwAkHMvqi3UKSwUh8FlzCC0CBZCx1LOY05NS613tU/gaI7r+Zl4Iq+PevetEy8WxViRGoye/A9TU5r+BshT12MWYzVE/BYAb6OcsHZ5QztA+GDXA01YnTpgwOenkJXmrmmkSif80hk+NAWglnCqUzl3apZHhgVLpdkGDJPjhy8l9qOt6lu2gBQVX0AC8MyMt5Uqx9yFcR65+S8v8TV5Bc7Kk+9nkCl503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VqXKkcF6aTXR187NoX/gZTQX6juJNQuCYhpPvNke3YbnkGJGVLnYLnAoGBAL/rT73jvNcr54hhzou8uYlFVGLNbE4IqbBIygTfC4Jd32NSZymcAL1/MLs6RvLj/Cd3DGVikS0y8nH5GGM8qxG0l1n9p96Z9AON+h0Jnen/HJvKPmq/SYkL2NNrg1CUHGL9FMDikMDBqIG/ttGIPCUZHyNAJJsMZJVfZJ9OP7Ru
-----END RSA PRIVATE KEY-----
现在,我必须做相反的事情,即从文件中包含的数据密钥,我必须有一个 RsaPrivateCrtKeyParameters。为此,我编写了这个小代码:
KeyFile string = @ "C: \ Users \ Me \ Documents \ Visual Studio 2010 \ Projects \ PEM \ PEM \ bin \ Debug \ myprivatekey.pem";
RsaPrivateCrtKeyParameters GetPrivateKey privat = (KeyFile);
如GetPrivateKey,如下:
public static RsaPrivateCrtKeyParameters GetPrivateKey(String pemFile)
if (string.IsNullOrEmpty(pemFile)) throw new ArgumentNullException("pemFile");
string privateKey = File.Exists(pemFile) ? File.ReadAllText(pemFile) : pemFile;
var reader = new PemReader(new StringReader(privateKey));
RsaPrivateCrtKeyParameters privkey = null;
Object obj = reader.ReadObject();
if (obj is AsymmetricCipherKeyPair)
privkey = (RsaPrivateCrtKeyParameters)((AsymmetricCipherKeyPair)obj).Private;
return privkey;
问题是我收到一个异常,在:Object obj = reader.ReadObject();
:
Creating RSA private key problem: System.InvalidCastException:
Unable to cast object of type 'Org.BouncyCastle.Asn1.DerSequence' to type 'Org.BouncyCastle.Asn1.DerInteger'.
to Org.BouncyCastle.Asn1.Pkcs.RsaPrivateKeyStructure .. ctor (Asn1Sequence seq)
to Org.BouncyCastle.OpenSsl.PemReader.ReadKeyPair (String type, String endMarker)
你能帮帮我吗,谢谢你的帮助
【问题讨论】:
请清理您的问题并提供一个突出您的问题的最小示例。 【参考方案1】:由于您以 PKCS#8 格式写出私钥,因此适当的 PEM 标头是“BEGIN PRIVATE KEY”(即没有“RSA”)。 PEM数据已经包含了密钥的算法,“BEGIN RSA PRIVATE KEY”有直接编码的RsaPrivateCrtKeyParameters的意思,不是你写出来的。
将标题更改为“BEGIN PRIVATE KEY”后,我能够解析您的示例文件。 ReadObject 调用然后返回一个 RsaPrivateCrtKeyParameters 对象。
【讨论】:
以上是关于无法将“Org.BouncyCastle.Asn1.DerSequence”类型的对象转换为“Org.BouncyCastle.Asn1.DerInteger”类型的主要内容,如果未能解决你的问题,请参考以下文章
java.lang.SecurityException: class “org.bouncycastle.asn1.DERObject“‘s signer information does not m
找不到类 org.bouncycastle.cms.CMSSignedData
Caused by: java.lang.IllegalStateException: 由于StackOverflower错误,无法完成对web应用程序[/lib]的批注的扫描。可能的根本原因包...