无法将“Org.BouncyCastle.Asn1.DerSequence”类型的对象强制转换为“Org.BouncyCastle.Asn1.DerInteger”

时间:2011-05-08 13:45:35

标签: c# bouncycastle

在我的c#项目中,我放了这段代码:

最初,我从我用弹性城堡创建的证书中恢复了密钥对,然后我提取了私钥,我的目标是,它是一种格式。 PEM,

 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+9nkCl503k8Sn1fw4OWc6cMhK9E4/LF5MUIMO/UnIPQKeLbTlY8hAYG0sN0AJT/3IfxAgMBAAECggEAAyACT1BLYQmi9e1y5ozOr2Q40eJgLeM6+lam0d+Kj1HFNCql1jMFI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VikS0y8nH5GGM8qxG0l1n9p96Z9AON+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 个答案:

答案 0 :(得分:0)

由于您正在以PKCS#8格式写出私钥,因此适当的PEM标题是“BEGIN PRIVATE KEY”(即没有“RSA”)。 PEM数据已包含密钥的算法,“BEGIN RSA PRIVATE KEY”具有直接编码的RsaPrivateCrtKeyParameters的含义,这不是您要写的。

我可以在将标题更改为“BEGIN PRIVATE KEY”后解析您的示例文件。 ReadObject调用然后返回一个RsaPrivateCrtKeyParameters对象。