Unable to cast object of type 'Org.BouncyCastle.Asn1.DerSequence' to type 'Org.BouncyCastle.Asn1.DerInteger'

前提是你 提交于 2019-12-07 21:26:18

问题


in my c # project, I put this piece of code :

initially, I recovered from the certificate that I created with bouncy castle, the key pair, then I extract the private key, my goal is that , it is in a format. 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();

my key is created perfectly, that is, what the file contains;

-----BEGIN RSA PRIVATE KEY-----

MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQCQBr1EhPxHeVax8QbXQtUwaSyqhZehjxkgVp89Rkn3awfo7f9usQdLSp3tLSwAkHMvqi3UKSwUh8FlzCC0CBZCx1LOY05NS613tU/gaI7r+Zl4Iq+PevetEy8WxViRGoye/A9TU5r+BshT12MWYzVE/BYAb6OcsHZ5QztA+GDXA01YnTpgwOenkJXmrmmkSif80hk+NAWglnCqUzl3apZHhgVLpdkGDJPjhy8l9qOt6lu2gBQVX0AC8MyMt5Uqx9yFcR65+S8v8TV5Bc7Kk+9nkCl503k8Sn1fw4OWc6cMhK9E4/LF5MUIMO/UnIPQKeLbTlY8hAYG0sN0AJT/3IfxAgMBAAECggEAAyACT1BLYQmi9e1y5ozOr2Q40eJgLeM6+lam0d+Kj1HFNCql1jMFI38U+iZ2w/Rea3RX1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0l1n9p96Z9AON+h0Jnen/HJvKPmq/SYkL2NNrg1CUHGL9FMDikMDBqIG/ttGIPCUZHyNAJJsMZJVfZJ9OP7Ru

-----END RSA PRIVATE KEY----- 

Now, I must do the opposite, ie, from a data key contained in the file, I must have a RsaPrivateCrtKeyParameters. to do this, I wrote this little code:

KeyFile string = @ "C: \ Users \ Me \ Documents \ Visual Studio 2010 \ Projects \ PEM \ PEM \ bin \ Debug \ myprivatekey.pem";

RsaPrivateCrtKeyParameters GetPrivateKey privat = (KeyFile);

such as GetPrivateKey, is as follows:

 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;
    } 

the problem is that I receive an exception , in : 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)

could you help me please, thank you for any help


回答1:


Since you are writing the private key out in PKCS#8 format, the appropriate PEM header is "BEGIN PRIVATE KEY" (i.e. without the "RSA"). The PEM data already contains the algorithm for the key, and "BEGIN RSA PRIVATE KEY" has the meaning of a directly encoded RsaPrivateCrtKeyParameters, which is not what you are writing out.

I was able to parse your example file after changing the headers to "BEGIN PRIVATE KEY". ReadObject call then returns an RsaPrivateCrtKeyParameters object.



来源:https://stackoverflow.com/questions/5927791/unable-to-cast-object-of-type-org-bouncycastle-asn1-dersequence-to-type-org-b

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!