#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include "eboy_wincrypt.h"
void main()
{
// Declare and initialize variables.
HCRYPTPROV hCryptProv; // Handle for a cryptographic
// provider context.
LPCSTR UserName = "MyKeyContainer"; // Name of the key container
// to be used.
//--------------------------------------------------------------------
// Attempt to acquire a context and a key
// container. The context will use the default CSP
// for the RSA_FULL provider type. DwFlags is set to 0
// to attempt to open an existing key container.
if(CryptAcquireContext(
&hCryptProv, // Handle to the CSP
UserName, // Container name
NULL, // Use the default provider
PROV_RSA_FULL, // Provider type
0)) // Flag values
{
printf("A crypto context with the %s key container \n", UserName);
printf("has been acquired.\n\n");
}
else
{
//--------------------------------------------------------------------
// An error occurred in acquiring the context. This could mean
// that the key container requested does not exist. In this case,
// the function can be called again to attempt to create a new key
// container.
if (GetLastError() == 0x80090016 )
{
if(CryptAcquireContext(
&hCryptProv,
UserName,
NULL,
PROV_RSA_FULL,
CRYPT_NEWKEYSET))
{
printf("A new key container has been created.\n");
}
else
{
printf("Could not create a new key container.\n");
exit(1);
}
}
else
{
printf("A cryptographic service handle could not be acquired.\n");
exit(1);
}
} // End of else
//--------------------------------------------------------------------
// A cryptographic context and a key container is available. Perform
// any functions that require a Cryptographic provider handle.
//--------------------------------------------------------------------
// When the handle is no longer needed, it should be released.
CryptReleaseContext(hCryptProv,0);
}
连接CSP,创建CSP句柄
最新推荐文章于 2020-12-04 09:24:48 发布