C# Examples

ChilkatHOMEASPVisual BasicVB.NETC#Visual C++CMFCDelphiFoxProJavaPerlPHPPythonRubySQL ServerVBScript

C# Examples

Bounced Mail
Character Encoding
Digital Certificates
Digital Signatures
Email
FTP
HTML to XML
HTTP
IMAP
Encryption
MHT / HTML Email
MIME
RSA Encryption
S/MIME
Socket
Spider
Tar Archive
Upload
XML
XMP
Zip Compression


More Examples...
Email Object
POP3
SMTP
RSS
Atom
String
Byte Array
Self-Extractor

Unreleased...
Service
PPMD
Deflate
Bzip2
LZW
Bz2
DH Key Exchange
DSA
Icon

 

 

 

 

 

 

Extract Public/Private Keys and Certs from PFX into String Variables

Demonstrates how to export certificates and public/private keys from a PFX file into in-memory strings.

Download Chilkat .NET for 2.0 Framework

Download Chilkat .NET for 1.0 / 1.1 Framework

bool success;
Chilkat.CertStore certStore = new Chilkat.CertStore();

//  Load the PFX file into a certificate store object
string password;
password = "*myPassword2*";
success = certStore.LoadPfxFile("chilkat.pfx",password);
if (success != true) {
    MessageBox.Show(certStore.LastErrorText);
    return;
}

int i;
int numCerts;
numCerts = certStore.NumCertificates;

//  Loop over each certificate in the PFX.
Chilkat.Cert cert = null;
string fname;
for (i = 0; i <= numCerts - 1; i++) {

    cert = certStore.GetCertificate(i);

    textBox1.Text += cert.SubjectDN + "\r\n";
    textBox1.Text += "---" + "\r\n";

    string encodedCert;
    encodedCert = cert.GetEncoded();

    //  This string may now be stored in a relational database string field.
    //  To re-create the cert, do this:
    Chilkat.Cert cert2 = new Chilkat.Cert();
    cert2.SetFromEncoded(encodedCert);

    //  Does this cert have a private key?
    if (cert.HasPrivateKey() == true) {

        //  Get the private key.
        Chilkat.PrivateKey pvkey = null;
        pvkey = cert.ExportPrivateKey();

        //  The private key can be exported into
        //  a string in PKCS8, RSA PEM, or XML format:
        string pemPvKey;
        string pkcs8PvKey;
        string xmlPvKey;

        pemPvKey = pvkey.GetRsaPem();
        pkcs8PvKey = pvkey.GetPkcs8Pem();
        xmlPvKey = pvkey.GetXml();

        textBox1.Text += pemPvKey + "\r\n";
        textBox1.Text += pkcs8PvKey + "\r\n";
        textBox1.Text += xmlPvKey + "\r\n";

        //  Any of these formatted strings may
        //  be stored in a relational database field.
        //  to restore, call LoadPem or LoadXml
        //  LoadPem accepts either RSA PEM or
        //  PKCS8 PEM:
        Chilkat.PrivateKey pvKey2 = new Chilkat.PrivateKey();

        pvKey2.LoadPem(pemPvKey);
        pvKey2.LoadPem(pkcs8PvKey);
        pvKey2.LoadXml(xmlPvKey);

    }

    //  Now for the public key:
    Chilkat.PublicKey pubkey = null;
    pubkey = cert.ExportPublicKey();

    //  It can be exported to a string as OpenSSL PEM
    //  or XML:
    string pubKeyPem;
    string pubKeyXml;

    pubKeyPem = pubkey.GetOpenSslPem();
    pubKeyXml = pubkey.GetXml();

    textBox1.Text += pubKeyPem + "\r\n";
    textBox1.Text += pubKeyXml + "\r\n";

    //  To re-load a PublicKey object, call LoadXml
    //  or LoadOpenSslPem:
    Chilkat.PublicKey pubKey2 = new Chilkat.PublicKey();

    pubKey2.LoadOpenSslPem(pubKeyPem);
    pubKey2.LoadXml(pubKeyXml);
    fname = "pubkey" + Convert.ToString(i) + "_openSsl.der";
    pubkey.SaveOpenSslDerFile(fname);

}

//  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.

 

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

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

Email Component · XML Parser