[C++] 取得使用者憑證(Certtificate)資訊

利用C++取得使用者憑證(Certtificate)資訊,大部份的程式碼都是參考MSDN,不過上面的範例程式碼排版真的是超難看的Orz。

最近初學C++和MFC,寫的亂七八糟 XD

大部份的程式碼都是參考MSDN,不過上面的範例程式碼排版真的是超難看的Orz。

HANDLE          hStoreHandle;
PCCERT_CONTEXT  pCertContext=NULL;   
PCCERT_CONTEXT  pDupCertContext; 
TCHAR pszNameString[256];

CRYPT_KEY_PROV_INFO *pCryptKeyProvInfo;
DWORD            cbData = 0;
//-------------------------------------------------------------------
// Open a system certificate store.

hStoreHandle = CertOpenSystemStore( NULL, _T("My"));

//-------------------------------------------------------------------
// Find the certificates in the system store. 

while(pCertContext= CertEnumCertificatesInStore( hStoreHandle, pCertContext)) 
// on the first call to the function,
//  this parameter is NULL
// on all subsequent calls, it is the last pointer returned by the function
{
	//-------------------------------------------------------------------
	// Get and display the name of the subject of the certificate.
	if(CertGetNameString( pCertContext, CERT_NAME_FRIENDLY_DISPLAY_TYPE, 0, NULL, pszNameString, 128))
	{
		wprintf(L"Certificate for %s \n", pszNameString);

		// 取得金鑰容器
		if(CertGetCertificateContextProperty( pCertContext, CERT_KEY_PROV_INFO_PROP_ID, NULL, &cbData))
		{

		   pCryptKeyProvInfo = (CRYPT_KEY_PROV_INFO *)malloc(cbData);
		   if(CertGetCertificateContextProperty(pCertContext,CERT_KEY_PROV_INFO_PROP_ID, pCryptKeyProvInfo, &cbData))
		   {
			   wprintf(L"The current key container is %s.\n", pCryptKeyProvInfo->pwszContainerName);		
			   wprintf(L"The CSP is %s.\n", pCryptKeyProvInfo->pwszProvName);
		   }
		   free(pCryptKeyProvInfo);
		}				
	}

}
//-------------------------------------------------------------------
// Clean up.

CertCloseStore( hStoreHandle, 0);

 

 

相關連結:

CertGetCertificateContextProperty Function

CertGetNameString Function

 

Dotblogs 的標籤: ,