title: How to import private key in PEM format using WinCrypt and C++?
body: <p>I'm trying to use the <a href="http://msdn.microsoft.com/en-us/library/ms867086.aspx" rel="nofollow noreferrer">WinCrypt API</a> in C++.</p>
  
  <p>My application need to cipher, decipher, sign and verify files, and I know how to do that once I have the correct keys. But my problem is actually that that is NOT the same application which generates those keys.</p>
  
  <p>What I have is public and private keys in files in PEM format :</p>
  
  <pre><code>-----BEGIN RSA PRIVATE KEY-----
  [Base64 encoded]
  -----END RSA PRIVATE KEY-----
  </code></pre>
  
  <p>And :</p>
  
  <pre><code>-----BEGIN RSA PUBLIC KEY-----
  [Base64 encoded]
  -----END RSA PUBLIC KEY-----
  </code></pre>
  
  <p>After some research, I have found how to import the public key : <a href="https://stackoverflow.com/questions/1231178/load-an-x509-pem-file-into-windows-cryptoapi">here</a> and <a href="https://stackoverflow.com/questions/7573754/how-to-use-cryptoapi-and-cryptimportkey-with-a-asn-1-pem-openssl-public-key">here</a>, using the following methods :</p>
  
  <ul>
  <li><strong>CreateFile</strong> &amp; <strong>ReadFile</strong> to read the file content</li>
  <li><strong>CryptStringToBinary</strong>, with <strong>CRYPT_STRING_BASE64HEADER</strong> to convert from PEM format to DER format (remove header and footer and decode from base64)</li>
  <li><strong>CryptDecodeObjectEx</strong> with <strong>X509_PUBLIC_KEY_INFO</strong></li>
  <li><strong>CryptImportPublicKeyInfo</strong>, to import the key</li>
  </ul>
  
  <p>But now, my problem is to do the same thing whith the <strong>private key</strong>.
  Any help would be really really appreciated :)
  Thank you.</p>