title: Unable to cast object of type 'Org.BouncyCastle.Asn1.DerSequence' to type 'Org.BouncyCastle.Asn1.DerInteger'
body: <p>in my c # project, I put this piece of code :</p>
  
  <p>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,</p>
  
  <pre><code> 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();
  </code></pre>
  
  <p>my key is created perfectly, that is, what the file contains;</p>
  
  <pre><code>-----BEGIN RSA PRIVATE KEY-----
  
  MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQCQBr1EhPxHeVax8QbXQtUwaSyqhZehjxkgVp89Rkn3awfo7f9usQdLSp3tLSwAkHMvqi3UKSwUh8FlzCC0CBZCx1LOY05NS613tU/gaI7r+Zl4Iq+PevetEy8WxViRGoye/A9TU5r+BshT12MWYzVE/BYAb6OcsHZ5QztA+GDXA01YnTpgwOenkJXmrmmkSif80hk+NAWglnCqUzl3apZHhgVLpdkGDJPjhy8l9qOt6lu2gBQVX0AC8MyMt5Uqx9yFcR65+S8v8TV5Bc7Kk+9nkCl503k8Sn1fw4OWc6cMhK9E4/LF5MUIMO/UnIPQKeLbTlY8hAYG0sN0AJT/3IfxAgMBAAECggEAAyACT1BLYQmi9e1y5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IygTfC4Jd32NSZymcAL1/MLs6RvLj/Cd3DGVikS0y8nH5GGM8qxG0l1n9p96Z9AON+h0Jnen/HJvKPmq/SYkL2NNrg1CUHGL9FMDikMDBqIG/ttGIPCUZHyNAJJsMZJVfZJ9OP7Ru
  
  -----END RSA PRIVATE KEY----- 
  </code></pre>
  
  <p>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:</p>
  
  <pre><code>KeyFile string = @ "C: \ Users \ Me \ Documents \ Visual Studio 2010 \ Projects \ PEM \ PEM \ bin \ Debug \ myprivatekey.pem";
  
  RsaPrivateCrtKeyParameters GetPrivateKey privat = (KeyFile);
  </code></pre>
  
  <p>such as  GetPrivateKey, is as follows:</p>
  
  <pre><code> 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;
      } 
  </code></pre>
  
  <p>the problem is that I receive an exception , in : <code>Object obj = reader.ReadObject();</code>
  :</p>
  
  <pre><code> 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)
  </code></pre>
  
  <p>could you help me please, thank you for any help</p>