从证书库导出CER格式的证书

直接上代码

// 证书提取.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include 
#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;
HCERTSTORE  hCollectionStore;           // 集合证书库句柄

PCCERT_CONTEXT  pDesiredCert = NULL;  // 证书句柄 
char pszNameString[256];//证书名字


HANDLE hStoreFileHandle;


char s1[100]; // 证书路径
int count = 0, index;
//错误提示
void HandleError(char *s)
{
	printf("error.\n");
	printf("%s\n", s);
	printf("error %x.\n", GetLastError());
	printf("error.\n");
	system("pause");
	exit(1);
}


//循环打印导出证书
void XunhuanDaYin(HCERTSTORE hSystemStore, char* pzName)
{
	
	while (pDesiredCert = CertEnumCertificatesInStore(hSystemStore, pDesiredCert))
	{
		// 打印证书名称 
		memset(pszNameString, 0, 256);
		if (CertGetNameString(pDesiredCert, CERT_NAME_RDN_TYPE, 0, NULL, pszNameString, 128))
		{
			//printf("证书被找到.%s \n", pszNameString);
		}

		CRYPTUI_WIZ_EXPORT_INFO ExportInfo;
		CRYPTUI_WIZ_EXPORT_CERTCONTEXT_INFO ContextInfo;
		ZeroMemory(&ExportInfo, sizeof(CRYPTUI_WIZ_EXPORT_INFO));
		ZeroMemory(&ContextInfo, sizeof(CRYPTUI_WIZ_EXPORT_CERTCONTEXT_INFO));
		ExportInfo.dwSize = sizeof(CRYPTUI_WIZ_EXPORT_INFO);

		//要导出保存后的文件路径
		memset(s1, 0, 100);
		count++;
		index = count;
		strcpy_s(s1, pzName);
		strcat_s(s1, "\\");
		sprintf_s(s1, "%s%ld.cer", s1, index);

		CString cerPath;
		cerPath = s1;

		USES_CONVERSION;
		LPWSTR pwStr = new wchar_t[cerPath.GetLength() + 1];
		wcscpy(pwStr, T2W((LPCTSTR)cerPath));

		ExportInfo.pwszExportFileName = pwStr;

		ExportInfo.dwSubjectChoice = CRYPTUI_WIZ_EXPORT_CERT_CONTEXT;

		//要导出的证书上下文
		ExportInfo.pCertContext = pDesiredCert;

		ContextInfo.dwSize = sizeof(CRYPTUI_WIZ_EXPORT_CERTCONTEXT_INFO);

		//以base64的方式导出
		ContextInfo.dwExportFormat = CRYPTUI_WIZ_EXPORT_FORMAT_BASE64;
		ContextInfo.fExportChain = FALSE;
		ContextInfo.fExportPrivateKeys = FALSE;
		CryptUIWizExport(CRYPTUI_WIZ_NO_UI, 0, NULL, &ExportInfo, &ContextInfo);


 	}

	CertCloseStore(hSystemStore, CERT_CLOSE_STORE_CHECK_FLAG);
	count = 0;
}


int _tmain(int argc, _TCHAR* argv[])
{

	char szDirName[MAX_PATH] = {0};

	if (argv[1] == NULL)
	{
		strcat_s(szDirName, "C:\\certificate");
	}
	else
	{
		strcpy_s(szDirName, argv[1]);
	}

	if (CreateDirectory(szDirName, NULL)) //判断是否存在,否则创建
	{
		printf("Create Successed!\r\n");
	}


	char szDirName1[MAX_PATH] = {0};

	strcpy_s(szDirName1, szDirName);
	strcat_s(szDirName1, "\\CER");

	if (CreateDirectory(szDirName1, NULL)) //判断是否存在,否则创建
	{
		printf("Create Successed!\r\n");
	}

	char szDirName2[MAX_PATH] = { 0 };

	strcpy_s(szDirName2, szDirName);
	strcat_s(szDirName2, "\\CER");
	strcat_s(szDirName2, "\\MY");

	if (CreateDirectory(szDirName2, NULL)) //判断是否存在,否则创建
	{
		printf("Create Successed!\r\n");
	}
	char szDirName3[MAX_PATH] = { 0 };

	strcpy_s(szDirName3, szDirName);
	strcat_s(szDirName3, "\\CER");
	strcat_s(szDirName3, "\\CA");


	if (CreateDirectory(szDirName3, NULL)) //判断是否存在,否则创建
	{
		printf("Create Successed!\r\n");
	}
	char szDirName4[MAX_PATH] = { 0 };

	strcpy_s(szDirName4, szDirName);
	strcat_s(szDirName4, "\\CER");
	strcat_s(szDirName4, "\\ROOT");

	if (CreateDirectory(szDirName4, NULL)) //判断是否存在,否则创建
	{
		printf("Create Successed!\r\n");
	}
	char szDirName5[MAX_PATH] = { 0 };

	strcpy_s(szDirName5, szDirName);
	strcat_s(szDirName5, "\\CER");
	strcat_s(szDirName5, "\\SPC");

	if (CreateDirectory(szDirName5, NULL)) //判断是否存在,否则创建
	{
		printf("Create Successed!\r\n");
	}

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

		XunhuanDaYin(hSystemStoreMY, szDirName2);
	// 打开系统证书库 “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, szDirName3);
	// 打开系统证书库 “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, szDirName4);

	// 打开系统证书库 “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, szDirName5);
	// 释放内存 

	if (pDesiredCert)
		CertFreeCertificateContext(pDesiredCert);

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

你可能感兴趣的:(从证书库导出CER格式的证书)