title: Microsoft CryptoAPI: how to convert PUBLICKEYBLOB to DER/PEM?
body: <p>I have a generated RSA key pair stored as PRIVATEKEYBLOB and PUBLICKEYBLOB, and I need to be able to convert these keys to DER or PEM formats so I could use it in PHP or Python. I figured out that I could use CryptEncodeObject function to convert my PRIVATEKEYBLOB to DER. In order to do that I need to use PKCS_RSA_PRIVATE_KEY encoding flag. But I couldn't find any clue on how to convert PUBLICKEYBLOB to DER.</p>
  
  <p>Here is my code for PRIVATEKEYBLOB convertion:</p>
  
  <pre><code>LPCSTR type = PKCS_RSA_PRIVATE_KEY;
  DWORD  encd = X509_ASN_ENCODING | PKCS_7_ASN_ENCODING;
  
  DWORD dlen = 0;
  if(!CryptEncodeObject(encd, type, key, null, &amp;dlen))
  { LOG_ERROR(); return false; }
  
  // Buffer allocation (der variable)
  
  if(!CryptEncodeObject(encd, type, key, der, &amp;dlen))
  { LOG_ERROR(); return false; }
  </code></pre>
  
  <p>I test my keys by comparing them to the output of openssl tool:</p>
  
  <pre><code>openssl rsa -pubin -inform MS\ PUBLICKEYBLOB -in pub.ms -outform DER -out pub.der
  openssl rsa -inform MS\ PRIVATEKEYBLOB -in pri.ms -outform DER -out pri.der
  </code></pre>
  
  <p>ADDED: I tried RSA_CSP_PUBLICKEYBLOB with X509_ASN_ENCODING, but the result is different to the output of openssl tool, and the key import failes. The openssl's exported DER is 25 bytes longer, and only first 3 bytes are equal in both keys. Here is the picture of key comparison:</p>
  
  <p><a href="https://i.stack.imgur.com/BJ29N.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/BJ29N.jpg" alt="Side-by-side key comparison"></a></p>
  
  <p>If we look closely at this picture, we can see that openssl's key version has some kind of additional 24 bytes header after the 3rd byte. Haven't figured out what is it as of yet, but if I concatinate this hardcoded header with the output I get from CryptEncodeObject with RSA_CSP_PUBLICKEYBLOB it all works fine. Not sure if that header is always the same or not though.</p>