MFC Examples

ChilkatHOMEASPVisual BasicVB.NETC#Visual C++CMFCDelphiFoxProJavaPerlPHPPythonRubySQL ServerVBScript

MFC Examples

Bounced Mail
Bz2
Certificates/Keys
Charset
CSV
Diffie-Hellman
DSA
Email Object
Encryption
FileAccess
FTP
HTML-to-XML
HTTP
IMAP
MHT / HTML Email
MIME
POP3
RSA
SMTP
Socket
Spider
SSH Key
SSH
SSH Tunnel
SFTP
Tar
Upload
XML
Zip


 

 

 

 

 

 

 

 

Export Certificate and Private Key to PFX

Demonstrates how to export a digital certificate, it's private key, and potentially all certificate's in the chain of authentication to a PFX file.

Download Chilkat C/C++ Libraries for VC++ 9.0 / Win32

Download Chilkat C/C++ Libraries for VC++ 8.0 / Win32

Download Chilkat C/C++ 64-bit Libraries for VC++ 8.0 / x64

Download Chilkat Visual Studio 2005 C/C++ Libs for Windows Mobile, Pocket PC, SmartPhone, WinCE

Download Chilkat C/C++ Libraries for VC++ 7.0 / Win32

Download Chilkat C/C++ Libraries for VC++ 6.0 / Win32

Download Chilkat C/C++ Libraries for VC++ 6.0, Win 95/98/NT4 Compatible

// Needs #include <CkCreateCS.h>
// Needs #include <CkCertStore.h>
// Needs #include <CkCert.h>

    CkString strOut;

    bool success;

    //  This object is used to create a certificate store object.
    CkCreateCS ccs;

    CkCertStore *certStore = 0;
    //  Open the local machine certificate store read-only.
    ccs.put_ReadOnly(true);
    certStore = ccs.OpenLocalSystemStore();

    //  Can we find a certificate by email address?
    CkCert *cert = 0;
    cert = certStore->FindCertBySubjectE("admin@chilkatsoft.com");
    if (cert == 0 ) {
        //  Open the current-user certificate store and check it instead.
        delete certStore;
        strOut.append("Checking current-user certificate store...");
        strOut.append("\r\n");

        certStore = ccs.OpenCurrentUserStore();

        cert = certStore->FindCertBySubjectE("admin@chilkatsoft.com");
        if (cert == 0 ) {
            strOut.append("Failed to find certificate!");
            strOut.append("\r\n");
            SetDlgItemText(IDC_EDIT1,strOut.getUnicode());
            return;
        }

    }

    delete certStore;

    //  Does this certificate have a private key accessible
    //  to the calling process?  Private keys are *not* stored
    //  within the certificate store.  Private keys are stored
    //  in a key container in a Windows protected store.  It
    //  can be one of two protected stores: the protected store for
    //  the current logged-in user account, or the "machine-key"
    //  protected store.  The private key must both exist in a
    //  protected store, and the process must have permission to
    //  access it...

    //  You can only export to a PFX if the private key exists
    //  and is accessible.

    if (cert->HasPrivateKey() == true) {

        //  Export to a PFX.
        //  Provide a password that will be required whenever the PFX is opened.
        //  Also, include all certs in the chain of authentication.
        bool bIncludeChain;
        bIncludeChain = true;
        success = cert->ExportToPfxFile("myCert.pfx","myPassword",bIncludeChain);
        if (success != true) {
            strOut.append(cert->lastErrorText());
            strOut.append("\r\n");
        }
        else {
            strOut.append("Exported to PFX!");
            strOut.append("\r\n");
        }

    }
    else {
        strOut.append("Certificate does not have a private key available");
        strOut.append("\r\n");
    }

    delete cert;

    //  The Chilkat Certificate, Certificate Store, Private Key,
    //  Public Key, and Key Container classes / objects are freeware.

    //  They are used by and included with the Chilkat Email,
    //  Crypt, S/MIME, and other commercial Chilkat components.


    SetDlgItemText(IDC_EDIT1,strOut.getUnicode());

Need a specific example? Send a request to support@chilkatsoft.com

© 2000-2008 Chilkat Software, Inc. All Rights Reserved.