Perl Examples

ChilkatHOMEAndroid™ASPVisual BasicVB.NETC#iOS (IPhone)Objective-CC++CMFCDelphiFoxProJavaPerl
PHP ExtensionPHP ActiveXPythonPowerShellRubySQL ServerVBScript

Perl Examples

Quick Start
Unicode
Byte Array
Bz2
Certificates
CSV
Email
Encryption
FTP
HTML Conversion
HTTP
IMAP
MHT
MIME
POP3
RSA
S/MIME
Signatures
SMTP
Socket / SSL
Spider
SFTP
SSH Key
SSH
SSH Tunnel
Tar
HTTP Upload
XML
XMP
Zip

More Examples...
String
Amazon S3
Email Object
DKIM / DomainKey
NTLM
FileAccess
RSS
Atom
Self-Extractor
Service
PPMD
Deflate
DH Key Exchange
DSA
Bzip2
LZW

 

 

 

 

 

 

 

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.

 Chilkat Perl Module Downloads for Windows, Linux, and MAC OS X

use chilkat();


$certStore = new chilkat::CkCertStore();

#  Load the PFX file into a certificate store object

$password = "*myPassword2*";
$success = $certStore->LoadPfxFile("chilkat.pfx",$password);
if ($success != 1) {
    print $certStore->lastErrorText() . "\n";
    exit;
}

$numCerts = $certStore->get_NumCertificates();

#  Loop over each certificate in the PFX.

for ($i = 0; $i <= $numCerts - 1; $i++) {

    $cert = $certStore->GetCertificate($i);

    print $cert->subjectDN() . "\r\n";
    print "---" . "\r\n";

    $encodedCert = $cert->getEncoded();

    #  This string may now be stored in a relational database string field.
    #  To re-create the cert, do this:
    $cert2 = new chilkat::CkCert();
    $cert2->SetFromEncoded($encodedCert);

    #  Does this cert have a private key?
    if ($cert->HasPrivateKey() == 1) {

        #  Get the private key.

        $pvkey = $cert->ExportPrivateKey();

        #  The private key can be exported into
        #  a string in PKCS8, RSA PEM, or XML format:

        $pemPvKey = $pvkey->getRsaPem();
        $pkcs8PvKey = $pvkey->getPkcs8Pem();
        $xmlPvKey = $pvkey->getXml();

        print $pemPvKey . "\r\n";
        print $pkcs8PvKey . "\r\n";
        print $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:
        $pvKey2 = new chilkat::CkPrivateKey();

        $pvKey2->LoadPem($pemPvKey);
        $pvKey2->LoadPem($pkcs8PvKey);
        $pvKey2->LoadXml($xmlPvKey);

    }

    #  Now for the public key:

    $pubkey = $cert->ExportPublicKey();

    #  It can be exported to a string as OpenSSL PEM
    #  or XML:

    $pubKeyPem = $pubkey->getOpenSslPem();
    $pubKeyXml = $pubkey->getXml();

    print $pubKeyPem . "\r\n";
    print $pubKeyXml . "\r\n";

    #  To re-load a PublicKey object, call LoadXml
    #  or LoadOpenSslPem:
    $pubKey2 = new chilkat::CkPublicKey();

    $pubKey2->LoadOpenSslPem($pubKeyPem);
    $pubKey2->LoadXml($pubKeyXml);
    $fname = "pubkey" . $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.

 

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