title: How to get a CNG key handle to a public certificate previously imported to windows Cert: store (in Delphi)?
body: <p>I need to use <strong>CNG</strong> to <strong>verify a message signature</strong> <em>(RSA</em>). The only problem is how to <strong>get the CNG key handle</strong> (<em>NCRYPT_KEY_HANDLE or BCRYPT_KEY_HANDLE</em>) if I have the <strong>public certificate stored in windows certs</strong> (Cert:\CurrentUser\My).
  I am using a </p>
  
  <pre><code>NCryptOpenStorageProvider({out}hProv, MS_KEY_STORAGE_PROVIDER, 0)
  </code></pre>
  
  <p>and I tried to get the <strong>public key</strong> handle using</p>
  
  <pre><code>NCryptOpenKey(hProv, {out}hKey, PWideChar('my.test.com'), AT_KEYEXCHANGE, 0) 
  </code></pre>
  
  <p>but it seems that <strong>NCryptOpenKey</strong>() can only open the certificates that also have <strong>private keys</strong>.</p>
  
  <p>I was also looking at <strong>BCryptImportKeyPair</strong>() but this requires to have the public key in <strong>BCRYPT_RSAKEY_BLOB</strong> structure which I don't know how to achieve. </p>
  
  <p>The last function I was looking at was <strong>NCryptImportKey</strong>() but this again only works with <strong>private keys</strong>.</p>
  
  <p>Anyone knows how to get the <strong>public key handle using CNG</strong>?
  I have the public key in a file (cer/pem) and I imported it to windows <code>Cert:</code> store but if you know how to load it directly from a file to CNG key handle I would be happy as well.</p>