连接CSP,创建CSP句柄

#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); 

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值