title: Why can't Bouncycastle's PEMReader read this M2Crypto-generated PEM?
body: <p>I use the following Python code, using M2Crypto, to generate an RSA key pair in PEM format:</p>
  
  <pre><code>bio = BIO.MemoryBuffer()
  key_pair = RSA.gen_key(1024, 65537)
  key_pair.save_key_bio(bio, cipher=None)
  return bio.read()
  </code></pre>
  
  <p>And the following Java code, using Bouncycastle, to try to read it:</p>
  
  <pre><code>String signPem = ...;
  PEMReader pemReader = new PEMReader(new StringReader(signPem));
  Object signingKey = pemReader.readObject();
  </code></pre>
  
  <p>The <code>signPem</code> string is the same as what <code>bio.read()</code> returned; no data that I can see is getting munged between the two programs; it's <code>-----BEGIN RSA PRIVATE KEY-----\n</code> etc.</p>
  
  <p>However, the readObject() call throws a ClassCastException:</p>
  
  <pre><code>java.lang.ClassCastException: org.bouncycastle.asn1.DERSequence
      at org.bouncycastle.asn1.ASN1Object.fromByteArray(Unknown Source)
      at org.bouncycastle.openssl.PEMReader.readKeyPair(Unknown Source)
      at org.bouncycastle.openssl.PEMReader.readObject(Unknown Source)
  </code></pre>
  
  <p>So obviously BC misidentifies the data in the PEM is not a key pair for some reason - but why?</p>