The CryptImportPublicKeyInfoEx function converts and imports the public key information into the CSP and returns a handle to the public key. Additional parameters (over those specified by CryptImportPublicKeyInfo) that can be used to override defaults are provided to supplement the CERT_PUBLIC_KEY_INFO.
#include <wincrypt.h>
BOOL WINAPI CryptImportPublicKeyInfoEx(
HCRYPTPROV hCryptProv, // in
DWORD dwCertEncodingType, // in
PCERT_PUBLIC_KEY_INFO pInfo, // in
ALG_ID aiKeyAlg, // in
DWORD dwFlags, // in
void *pvAuxInfo, // in, optional
HCRYPTKEY *phKey // out
);
Encoding type | Value |
---|---|
X509_ASN_ENCODING | 0x00000001 |
TRUE if the function succeeded, FALSE if the function failed.
Call GetLastError to see the reason for any failures. Note that errors from the called functions CryptGetUserKey and CryptExportKey may be propagated to this function. This function has the following error codes.
Error code | Description |
---|---|
CRYPT_E_OSS_ERROR | Public key ASN.1 encoding error. Note, to get the OSS error subtract CRYPT_E_OSS_ERROR from the returned error and see asn1code.h for details on the error. |
ERROR_FILE_NOT_FOUND | An installable or registerable import function could not be found for the specified dwCertEncodingType and pInfo->Algorithm.pszObjId. |
// EXAMPLE CODE FOR USING CryptImportPublicKeyInfoEx().
// Convert and import the public key information and return
// a handle to the public key.
// Assume that a pointer to the public key (pInfo) has
// already been defined.
// Set up the variables.
HCRYPTPROV hCryptProv = 0; // Service Provider handle
DWORD dwCertEncodingType = X509_ASN_ENCODING;
// Type of encoding
PCERT_PUBLIC_KEY_INFO pInfo; // Initialized elsewhere
ALG_ID aiKeyAlg = CALG_RSA_KEYX; // CSP specific algorithm
DWORD dwFlags = 0; // Flag value
void *pvAuxInfo = NULL; // Reserved
HCRYPTKEY *phKey;
BOOL fResult; // Return TRUE if function succeeded
// FALSE if function failed
fResult= CryptImportPublicKeyInfoEx(
hCryptProv, // in - 0 is default RSA or DSS provider
dwCertEncodingType,
// in - X509_ASN_ENCODING
pInfo, // in - Pointer to the public key to import
aiKeyAlg, // in - Reserved for future use - set to 0
0, // in - Reserved for future use - set to NULL
NULL, // in/optional
phKey); // out- Pointer to the handle to the public key
if (!fResult) { // FALSE
cout<< "Function failed"<< endl
<< "error code = "<< GetLastError()<< endl;
}
else { // TRUE
cout<< "Function succeeded"<< endl;
}
Windows NT: Requires version 4.0 SP3 or later. Available also in IE 3.02 and later.
Windows: Requires Windows 98 (or Windows 95 with IE 3.02 or later).
Windows CE: Unsupported.
Header: Declared in wincrypt.h.
Import Library: Use crypt32.lib.