CryptImportPublicKeyInfoEx function (wincrypt.h)

Important  This API is deprecated. New and existing software should start using Cryptography Next Generation APIs. Microsoft may remove this API in future releases.
The CryptImportPublicKeyInfoEx function imports public key information into the cryptographic service provider (CSP) and returns a handle of the public key. Additional parameters to override defaults are provided to supplement those in CERT_PUBLIC_KEY_INFO.


BOOL CryptImportPublicKeyInfoEx(
  [in]  HCRYPTPROV            hCryptProv,
  [in]  DWORD                 dwCertEncodingType,
  [in]  ALG_ID                aiKeyAlg,
  [in]  DWORD                 dwFlags,
  [in]  void                  *pvAuxInfo,
  [out] HCRYPTKEY             *phKey


[in] hCryptProv

The handle of the CSP to receive the imported public key. This handle must have already been created using CryptAcquireContext.

[in] dwCertEncodingType

Specifies the encoding type used. It is always acceptable to specify both the certificate and message encoding types by combining them with a bitwise-OR operation as shown in the following example:


Currently defined encoding types are:


[in] pInfo

the address of a CERT_PUBLIC_KEY_INFO structure that contains the public key to import into the provider.

Note  The pzObjId member of the Algorithm member pointed to by the pInfo and dwCertEncodingType parameters determine an installable CRYPT_OID_IMPORT_PUBLIC_KEY_INFO_FUNC callback function. If an installable function is not found, an attempt is made to import the key as an RSA Public Key (szOID_RSA_RSA).

[in] aiKeyAlg

An ALG_ID structure that contains a CSP-specific algorithm to override the CALG_RSA_KEYX default algorithm.

[in] dwFlags

Reserved for future use and must be zero.

[in] pvAuxInfo

Reserved for future use and must be NULL.

[out] phKey

The address of an HCRYPTKEY variable that receives the handle of the imported public key. When you have finished using the public key, release the handle by calling the CryptDestroyKey function.

Return value

If the function succeeds, the function returns nonzero (TRUE).

If the function fails, it returns zero (FALSE). For extended error information, call GetLastError.

Note  Errors from the called functions CryptGetUserKey and CryptExportKey might be propagated to this function. This function has the following error code.
Value Description
An import function that can be installed or registered could not be found for the specified dwCertEncodingType and pInfo parameters.

If the function fails, GetLastError may return an Abstract Syntax Notation One (ASN.1) encoding/decoding error. For information about these errors, see ASN.1 Encoding/Decoding Return Values.


This function is normally used to retrieve the public key from a certificate. This is done by passing the CERT_PUBLIC_KEY_INFO structure from a filled-in certificate structure as shown in the following pseudocode.


// Get the certificate context structure from a certificate.
pCertContext = CertCreateCertificateContext(...)
    HCRYPTKEY hCertPubKey

    // Get the public key information for the certificate.



Requirement Value
Minimum supported client Windows XP [desktop apps only]
Minimum supported server Windows Server 2003 [desktop apps only]
Target Platform Windows
Header wincrypt.h
Library Crypt32.lib
DLL Crypt32.dll

See also


Data Management Functions