CryptExportPublicKeyInfoEx function

The CryptExportPublicKeyInfoEx function exports the public key information associated with the provider's corresponding private key. This function allows the application to specify the public key algorithm, overriding the default provided by the cryptographic service provider (CSP).


BOOL WINAPI CryptExportPublicKeyInfoEx(
  _In_    DWORD                           dwKeySpec,
  _In_    DWORD                           dwCertEncodingType,
  _In_    LPSTR                           pszPublicKeyObjId,
  _In_    DWORD                           dwFlags,
  _In_    void                            *pvAuxInfo,
  _Out_   PCERT_PUBLIC_KEY_INFO           pInfo,
  _Inout_ DWORD                           *pcbInfo


hCryptProvOrNCryptKey [in]

A handle of the CSP to use when exporting the public key information. This handle must be an HCRYPTPROV handle that has been created by using the CryptAcquireContext function or an NCRYPT_KEY_HANDLE handle that has been created by using the NCryptOpenKey function. New applications should always pass in the NCRYPT_KEY_HANDLE handle of a CNG CSP.

dwKeySpec [in]

Identifies the private key to use from the provider's container. It can be AT_KEYEXCHANGE or AT_SIGNATURE. This parameter is ignored if an NCRYPT_KEY_HANDLE is used in the hCryptProvOrNCryptKey parameter.

dwCertEncodingType [in]

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:

pszPublicKeyObjId [in]

Specifies the public key algorithm.

Note  pszPublicKeyObjId and dwCertEncodingType are used together to determine the installable CRYPT_OID_EXPORT_PUBLIC_KEY_INFO_FUNC to call. If an installable function was not found for the pszPublicKeyObjId parameter, an attempt is made to export the key as an RSA Public Key (szOID_RSA_RSA).
dwFlags [in]

A DWORD flag value that indicates how the public key information is exported. The flag value is passed directly to the CryptFindOIDInfo function when mapping the public key object identifier to the corresponding CNG public key algorithm Unicode string. The following flag values can be set.


Skips public keys in the CRYPT_PUBKEY_ALG_OID_GROUP_ID group explicitly flagged with the CRYPT_OID_PUBKEY_ENCRYPT_ONLY_FLAG flag.


Skips public keys in the CRYPT_PUBKEY_ALG_OID_GROUP_ID group explicitly flagged with the CRYPT_OID_PUBKEY_SIGN_ONLY_FLAG flag.


pvAuxInfo [in]

This parameter is reserved for future use and must be set to NULL.

pInfo [out]

A pointer to a CERT_PUBLIC_KEY_INFO structure to receive the public key information to be exported.

This parameter can be NULL to set the size of this information for memory allocation purposes. For more information, see Retrieving Data of Unknown Length.

pcbInfo [in, out]

A pointer to a DWORD that contains the size, in bytes, of the buffer pointed to by the pInfo parameter. When the function returns, the DWORD contains the number of bytes stored in the buffer.

Note  When processing the data returned in the buffer, applications need to use the actual size of the data returned. The actual size can be slightly smaller than the size of the buffer specified on input. (On input, buffer sizes are usually specified large enough to ensure that the largest possible output data will fit in the buffer.) On output, the variable pointed to by this parameter is updated to reflect the actual size of the data copied to the buffer.

Return value

If the function succeeds, the return value is nonzero (TRUE).

If the function fails, the return value is zero (FALSE). For extended error information, call GetLastError.

Note  Errors from the called functions CryptGetUserKey and CryptExportKey can be propagated to this function.

This function has the following error codes.

Return codeDescription

An export function that can be installed or registered could not be found for the specified dwCertEncodingType and pszPublicKeyObjId parameters.


If the buffer specified by the pInfo parameter is not large enough to hold the returned data, the function sets the ERROR_MORE_DATA code and stores the required buffer size, in bytes, in the variable pointed to by the pcbInfo parameter.


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.


Minimum supported client

Windows XP [desktop apps only]

Minimum supported server

Windows Server 2003 [desktop apps only]







See also

Data Management Functions