CryptExportPKCS8Ex function (wincrypt.h)

[The CryptExportPKCS8Ex function is no longer available for use as of Windows Server 2008 and Windows Vista. Instead, use the PFXExportCertStoreEx function.]

The CryptExportPKCS8Ex function exports the private key in PKCS #8 format.This function has no associated import library. You must use the LoadLibrary and GetProcAddress functions to dynamically link to Crypt32.dll.


BOOL CryptExportPKCS8Ex(
  [in]            CRYPT_PKCS8_EXPORT_PARAMS *psExportParams,
  [in]            DWORD                     dwFlags,
  [in, optional]  void                      *pvAuxInfo,
  [out, optional] BYTE                      *pbPrivateKeyBlob,
  [in, out]       DWORD                     *pcbPrivateKeyBlob


[in] psExportParams

A pointer to a CRYPT_PKCS8_EXPORT_PARAMS structure that contains information about the key to export.

[in] dwFlags

This parameter should be zero if pbPrivateKeyBlob is NULL and 0x8000 otherwise.

[in, optional] pvAuxInfo

This parameter must be NULL.

[out, optional] pbPrivateKeyBlob

A pointer to an array of BYTE structures to receive the private key to be exported.

The private key will contain the information in a PKCS #8 PrivateKeyInfo Abstract Syntax Notation One (ASN.1) type found in the PKCS #8 standard.

For memory allocation purposes, you can get the size of the private key to be exported by setting this parameter to NULL. For more information, see Retrieving Data of Unknown Length.

[in, out] pcbPrivateKeyBlob

A pointer to a DWORD that may contain, on input, the size, in bytes, of the memory allocation needed to contain the pbPrivateKeyBlob. If pbPrivateKeyBlob is NULL, this parameter will return the size of the memory allocation needed for a second call to the function. For more information, see Retrieving Data of Unknown Length.

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.

The following error codes are specific to this function.

Return code Description
An export function that can be installed or registered could not be found.
If the buffer specified by the pbPrivateKeyBlob 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 pcbPrivateKeyBlob parameter.

If the function fails, GetLastError returns an ASN.1 encoding/decoding error. For information about these errors, see ASN.1 Encoding/Decoding Return Values.


This function is only supported for asymmetric keys.


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
DLL Crypt32.dll

See also