从证书库导出PFX证书

首先了解下思路: 先 CertOpenStore 打开系统证书库 , 然后 CertEnumCertificateContextProperties 判断带私钥的证书 , 再打开一个临时证书库把找到的证书放进去 CertAddCertificateContextToStore , 然后用 PFXExportCertStoreEx 导出证书.
需要包含的头文件,以及变量

#include "stdafx.h"
#include 
#include


// Link with the Crypt32.lib file.
#pragma comment(lib, "Crypt32")
#pragma comment(lib, "comsuppw.lib")
#pragma comment(lib, "Cryptui.lib")

#define MY_ENCODING_TYPE (PKCS_7_ASN_ENCODING | X509_ASN_ENCODING)

HCERTSTORE  hSystemStoreCA;             // 系统证书库句柄 CA
HCERTSTORE  hSystemStoreMY;             // 
HCERTSTORE  hSystemStoreROOT;
HCERTSTORE  hSystemStoreSPC;
HCERTSTORE  hMemoryStore;
PCCERT_CONTEXT  pDesiredCert = NULL;  // 证书句柄 
char pszNameString[256];//证书名字
char s1[100]; // 证书路径
int count = 0, index;
int _tmain(int argc, _TCHAR* argv[])
{

	system("md C:\\证书导出\\PFX");
	// 打开系统证书库 “MY” 
	if (hSystemStoreMY = CertOpenStore(CERT_STORE_PROV_SYSTEM, 0, NULL, CERT_SYSTEM_STORE_CURRENT_USER, L"MY"))
	{
		printf("打开证书库MY. \n");
	}
	else
	{
		HandleError("不能打开MY 系统证书库.");
	}
	char sss[] = "C:\\证书导出\\PFX\\";

		XunhuanDaYin(hSystemStoreMY, sss);
	// 打开系统证书库 “CA” 
		if (hSystemStoreCA = CertOpenStore(CERT_STORE_PROV_SYSTEM, 0, NULL,CERT_SYSTEM_STORE_CURRENT_USER, L"CA"))                
		{
			printf("打开证书库 CA. \n");
		}
		else
		{
			HandleError("不能打开 CA 系统证书库.");
		}

		XunhuanDaYin(hSystemStoreCA, sss);
	// 打开系统证书库 “ROOT” 
		if (hSystemStoreROOT = CertOpenStore(CERT_STORE_PROV_SYSTEM, 0, NULL, CERT_SYSTEM_STORE_CURRENT_USER, L"ROOT"))
		{
			printf("打开证书库 ROOT. \n");
		}
		else
		{
			HandleError("不能打开 ROOT 系统证书库.");
		}
		XunhuanDaYin(hSystemStoreROOT, sss);

	// 打开系统证书库 “SPC” 
		if (hSystemStoreSPC = CertOpenStore(CERT_STORE_PROV_SYSTEM, 0, NULL, CERT_SYSTEM_STORE_CURRENT_USER, L"SPC"))
		{
			printf("打开证书库SPC. \n");
		}
		else
		{
			HandleError("不能打开SPC 系统证书库.");
		}
		XunhuanDaYin(hSystemStoreSPC, sss);
	// 释放内存 

	if (pDesiredCert)
		CertFreeCertificateContext(pDesiredCert);

	if (hMemoryStore)
		CertCloseStore(
		hMemoryStore,
		CERT_CLOSE_STORE_CHECK_FLAG);
	system("pause");
	return 0;
}

循环打印输出文件的代码如下:

//循环打印导出证书
void XunhuanDaYin(HCERTSTORE hSystemStore, char* pzName)
{
	
	while (pDesiredCert = CertEnumCertificatesInStore(hSystemStore, pDesiredCert))
	{
		// 打印证书名称 

		if (CertGetNameString(pDesiredCert, CERT_NAME_RDN_TYPE, 0, NULL, (LP)pszNameString, 128))

			printf("\n 证书被找到. \n");
		DWORD dwPropid =0;
		
	
		while (dwPropid = CertEnumCertificateContextProperties(
			pDesiredCert, dwPropid))

		{
			if (dwPropid == 2)
			{
				goto aaaaa;
			}
			else
			{
				
				continue;
			}
			

		}
		printf("非私钥证书\n");
		continue;
aaaaa:	
		//路径
		count++;
		index = count;
		strcpy_s(s1, pzName);
		sprintf_s(s1, "%s%ld.pfx", s1, index);


		//打开临时证书库
		hMemoryStore = CertOpenStore(CERT_STORE_PROV_MEMORY, 0, NULL, CERT_SYSTEM_STORE_CURRENT_USER, NULL);

		//添加找到的证书到临时证书库
		if (CertAddCertificateContextToStore(
			hMemoryStore,                // 证书库句柄 
			pDesiredCert,                // 欲添加的证书结构指针 
			CERT_STORE_ADD_USE_EXISTING, // 当证书库中存在有和此证书匹配的证书或链接时 
			// 不添加证书,但会把新证书的属性添加到匹配证书中 
			NULL))                       // 添加的证书结构指针的拷贝 
		{
			printf("所找到的证书添加到了内存证书库中. \n");
		}
		else
		{
			HandleError("未能将此证书添加到内存证书库中.");
		}

		char msg[100];

		CRYPT_DATA_BLOB BlobInfo;

		ZeroMemory(&BlobInfo, sizeof(BlobInfo));


		BlobInfo.cbData = sizeof(CRYPT_DATA_BLOB);

		BlobInfo.pbData = NULL;

		PFXExportCertStoreEx(hMemoryStore, &BlobInfo, L"123456", NULL, EXPORT_PRIVATE_KEYS);

		sprintf_s(msg, "%d", BlobInfo.cbData);

		BlobInfo.pbData = (PBYTE)malloc(BlobInfo.cbData);

		PFXExportCertStoreEx(hMemoryStore, &BlobInfo, L"123456", NULL, EXPORT_PRIVATE_KEYS);
		printf("已导出,密码123456\n");

		FILE *f;

		fopen_s(&f, s1, "wb");

		fwrite(BlobInfo.pbData, BlobInfo.cbData, 1, f);
		fclose(f);
	}
}

错误输出代码:

//错误提示
void HandleError(char *s)
{
	printf("程序运行出错.\n");
	printf("%s\n", s);
	printf("错误代码 %x.\n", GetLastError());
	printf("程序终止.\n");
	system("pause");
	exit(1);
}

你可能感兴趣的:(windows核心,windows)