title: Printing .pem key DER form in python
body: <p>I would like to print out the binary form (not sure if this is how I would refer to it) of a .pem key using python. To clarify, I want to do in python what this unix command would print out:</p>
  
  <pre><code>cat privateKey.pem | openssl rsa -pubout -outform DER
  </code></pre>
  
  <p>I can't just call this command using subprocess because I want it to work on Windows. I've looked at the M2Crypto and PyCrypto libraries, and with the M2Crypto library I am able to load the key using</p>
  
  <pre><code>from M2Crypto import RSA
  rsaKey = RSA.load_key('privateKey.pem')
  </code></pre>
  
  <p>But I don't see any methods of rsaKey that print out the binary form.</p>
  
  <p>Edit:</p>
  
  <p>Here's what I have so far:</p>
  
  <pre><code>import M2Crypto
  key = M2Crypto.RSA.load_key('key.pem')
  bio = M2Crypto.BIO.MemoryBuffer()
  
  key.save_key_der_bio(bio)
  
  der = bio.read()
  </code></pre>
  
  <p>But <code>der</code> isn't the same as what openssl printed out. I piped the output of openssl into hexdump to compare them.</p>