CertGetPublicKeyLength

The CertGetPublicKeyLength function gets the public/private key's bit length from the public key blob.

#include <wincrypt.h>
DWORD WINAPI CertGetPublicKeyLength(
  DWORD dwCertEncodingType,         // in
  PCERT_PUBLIC_KEY_INFO pPublicKey  // in
);
 

Parameters

dwCertEncodingType
The type of encoding used on the certificate. Currently defined certificate encoding types are shown in the following table:
Encoding type Value
X509_ASN_ENCODING 0x00000001

pPublicKey
A pointer to the public key blob holding the keys for which the length is being retrieved.

Return Values

Returns the length of the public/private keys in bits. If unable to determine the key's length, returns zero.

Call GetLastError to see the reason for any failures.

Example

// EXAMPLE CODE FOR USING CertGetPublicKeyLength.
// Gets the public/private key's bit length from the public key blob.
// Assume that a pointer to the public key blob (pPublicKey is already
// defined.

// Set up the variables.
DWORD dwCertEncodingType = X509_ASN_ENCODING;
                                  // Type of encoding
PCERT_PUBLIC_KEY_INFO pPublicKey; // Initialized elsewhere
DWORD dwResult;                   // Returns the length of
                                  //   public/private keys in bits

dwResult = CertGetPublicKeyLength(
            dwCertEncodingType,   // in
            pPublicKey);          // in

if (dwResult == 0) {
  cout<< "function unable to determine "<< endl
      << "key's length - 0 returned- "<< endl
      << "error code = "<< GetLastError()<< endl;
}
else {
  cout<< "function succeeded"<< endl
      << "length = " << dwResult<< endl;
}
 

QuickInfo

  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.