Delphi Examples

ChilkatHOMEASPVisual BasicVB.NETC#Visual C++CMFCDelphiFoxProJavaPerlPHPPythonRubySQL ServerVBScript

Delphi Examples

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

More Examples...
Byte Array
Email Object
POP3
SMTP
RSS
Atom
Self-Extractor
Service
PPMD
Deflate
DH Key Exchange
DSA

Unreleased...
Bzip2
LZW
Bz2
Icon

Type Conversion

 

Article: Understanding COM References in Delphi

Export certificates and public/private keys from a PFX

Demonstrates how to export certificates and public/private keys from a PFX file.

uses
    Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
    Dialogs, StdCtrls,
    CHILKATCERTIFICATELib_TLB,
    OleCtrls;

...

procedure TForm1.Button1Click(Sender: TObject);
var
success: Integer;
certStore: CHILKATCERTIFICATELib_TLB.IChilkatCertStore;
password: String;
i: Integer;
numCerts: Integer;
cert: IChilkatCert;
fname: String;
pvkey: IPrivateKey;
pubkey: IPublicKey;

begin

certStore := CoChilkatCertStore.Create();

//  Load the PFX file into a certificate store object

password := 'myPassword';
success := certStore.LoadPfxFile('chilkat.pfx',password);
if (success <> 1) then
  begin
    ShowMessage(certStore.LastErrorText);

  end;

numCerts := certStore.NumCertificates;

for i := 0 to numCerts - 1 do
  begin

    cert := certStore.GetCertificate(i);

    Memo1.Lines.Add(cert.SubjectDN);
    Memo1.Lines.Add('---');

    //  Save the cert in DER format:
    fname := 'cert' + IntToStr(i) + '.der';
    cert.ExportCertDerFile(fname);

    //  Save the cert in PEM format:
    fname := 'cert' + IntToStr(i) + '.pem';
    cert.ExportCertPemFile(fname);

    //  Does this cert have a private key?
    if (cert.HasPrivateKey() = 1) then
      begin

        //  Get the private key.

        pvkey := cert.ExportPrivateKey();

        //  Save the private key to a PKCS8 DER-encoded file
        fname := 'pvkey' + IntToStr(i) + '_pkcs8.der';
        pvkey.SavePkcs8File(fname);

        //  Save the private key to a PKCS8 PEM-encoded file
        fname := 'pvkey' + IntToStr(i) + '_pkcs8.pem';
        pvkey.SavePkcs8PemFile(fname);

        //  Save the private key to a RSA DER-encoded file
        fname := 'pvkey' + IntToStr(i) + '_rsa.der';
        pvkey.SaveRsaDerFile(fname);

        //  Save the private key to a RSA PEM-encoded file
        fname := 'pvkey' + IntToStr(i) + '_rsa.pem';
        pvkey.SaveRsaPemFile(fname);

        //  Save the private key to an XML file
        //  This format is Chilkat-specific, but easily parsed,
        //  making it easy to get the modulus, exponent,
        //  P, Q, DP, DQ, InverseQ, and D.
        fname := 'pvkey' + IntToStr(i) + '.xml';
        pvkey.SaveXmlFile(fname);

      end;

    //  Now get the public key and save it to various file formats:

    pubkey := cert.ExportPublicKey();

    //  Save to an OpenSSL DER format file:
    fname := 'pubkey' + IntToStr(i) + '_openSsl.der';
    pubkey.SaveOpenSslDerFile(fname);

    //  Save to an OpenSSL PEM format file:
    fname := 'pubkey' + IntToStr(i) + '_openSsl.pem';
    pubkey.SaveOpenSslPemFile(fname);

    //  Save to an RSA DER format file:
    fname := 'pubkey' + IntToStr(i) + '_rsa.der';
    pubkey.SaveRsaDerFile(fname);

    //  Save to an XML file:
    //  This format is Chilkat-specific, but easily parsed,
    //  making it easy to get the modulus and exponent.
    fname := 'pubkey' + IntToStr(i) + '.xml';
    pubkey.SaveXmlFile(fname);

  end;

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

end;

 

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

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

Mail Component · .NET Email Component · XML Parser