// ExportCert.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include #include "Certificate.h" #include "CertificateStore.h" #include "conio.h" #include #include #include #include "WinCrypt.h" using namespace std; const char CONST_CURRENTUSER_CERTSTORE[] = "MY";//certificate const char CONST_VALVE_CYBER_CAFE_PROGRAM[] = "Valve Cyber Cafe Program";//certificate int _tmain(int argc, _TCHAR* argv[]) { CCertificateStore* CertStore = new CCertificateStore(); if(CertStore->Open(CONST_CURRENTUSER_CERTSTORE)) { //_cprintf("CertStore opened.\n"); if(PCCERT_CONTEXT pCertContext = CertStore->FindNewestCert(CONST_VALVE_CYBER_CAFE_PROGRAM, PKCS_7_ASN_ENCODING | X509_ASN_ENCODING, CERT_FIND_ISSUER_STR_A)) { //Certificate Found CCertificate* Cert = new CCertificate(pCertContext); //Login if (Cert->GetUserKey()) { HCRYPTKEY hKey = Cert->hUserKey; LPDWORD pdwBlobLen = NULL; DWORD dwBlobLength; LPBYTE *ppbKeyBlob = NULL; *pdwBlobLen = 0; DWORD dwBlobType = PRIVATEKEYBLOB; // Export the public key. Here the public key is exported to a // PUBLICKEYBLOB. This BLOB can be written to a file and // sent to another user. if(CryptExportKey( hKey, NULL, dwBlobType, 0, NULL, &dwBlobLength)) { printf("Size of the BLOB for the public key determined. \n"); // Allocate memory for the pbKeyBlob. if(*ppbKeyBlob = (LPBYTE)malloc(dwBlobLength)) { printf("Memory has been allocated for the BLOB. \n"); if(CryptExportKey( hKey, NULL, dwBlobType, 0, *ppbKeyBlob, &dwBlobLength)) { printf("Contents have been written to the BLOB. \n"); *pdwBlobLen = dwBlobLength; } else { printf("Error exporting key.\n"); free(*ppbKeyBlob); *ppbKeyBlob = NULL; } } else { printf("Out of memory. \n"); } } else { printf("Error computing BLOB length.\n"); } } } CertStore->Close(); delete CertStore; } return 0; }