The CPGenKey function generates random cryptographic keys.
BOOL CPGenKey(
HCRYPTPROV hProv, // in
ALG_ID Algid, // in
DWORD dwFlags, // in
HCRYPTKEY *phKey // out
);
If a session key is to be generated, this value specifies an encryption algorithm, such as:
If a public/private key pair is to be generated, this value specifies a key pair type, such as:
This flag only applies to the export of session keys and private keys. It does not apply to public keys, which are always exportable.
If this flag is not set, then the session key will be given a salt value of zero.
This flag does not apply to public/private key pairs.
If the function succeeds, TRUE should be returned; otherwise, return FALSE. When FALSE is returned, the appropriate error code (see the following table) must be set via SetLastError.
Error | Description |
---|---|
NTE_BAD_ALGID | The Algid parameter specifies an algorithm that this CSP does not support. |
NTE_BAD_FLAGS | The dwFlags parameter contains an invalid value. |
NTE_FAIL | The function failed in some unexpected way. |
NTE_BAD_UID | The hProv parameter does not contain a valid context handle. |
NTE_PERM | An attempt was made to create a key pair when CRYPT_VERIFYCONTEXT was specified. |
If no key container is currently open (for example, the CRYPT_VERIFYCONTEXT flag was set in the CPAcquireContext call), then applications should not be allowed to create new key pairs. Instead, the NTE_PERM error code should be returned.
The CPGenRandom function is generally used to generate the random key material. It is important that all keys generated be truly random.
When keys are generated for symmetric block ciphers, the key by default will be set up in cipher block chaining (CBC) mode with an initialization vector of zero. This cipher mode provides a good default method for bulk encrypting data. These parameters are changed by using the CPSetKeyParam function.
CPDestroyKey, CPExportKey, CPImportKey, CryptGenKey