Ruby Examples

ChilkatHOMEASPVisual BasicVB.NETC#Visual C++CMFCDelphiFoxProJavaPerlPHPPythonRubySQL ServerVBScript

Ruby
Examples

Quick Start
Ruby Unicode
Ruby Byte Array
Ruby Certs
Ruby Email
Ruby Encryption
Ruby FTP
HTML-to-XML
Ruby HTTP
Ruby IMAP
Ruby MHT
Ruby MIME
Ruby S/MIME
Ruby Signatures
Ruby RSA
Ruby Socket
Ruby Spider
Ruby Tar
Ruby Upload
Ruby XML
Ruby XMP
Ruby Zip

More Examples...
String
Email Object
POP3
SMTP
RSS
Atom
Self-Extractor
Service
PPMD
Deflate
Bzip2
DH Key Exchange
DSA
SSH Key
SSH
SSH Tunnel
SFTP

Unreleased...
LZW
Bz2
Icon

 

 

 

 

 

 

 

Export certificates and public/private keys from a PFX

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

Download Chilkat Ruby Library

require 'chilkat'


certStore = Chilkat::CkCertStore.new()

#  Load the PFX file into a certificate store object

password = "myPassword"
success = certStore.LoadPfxFile("chilkat.pfx",password)
if (success != true)
    print certStore.lastErrorText() + "\n"
    exit
end

numCerts = certStore.get_NumCertificates()

for i in 0 .. numCerts - 1

    cert = certStore.GetCertificate(i)

    print cert.subjectDN() + "\n";
    print "---" + "\n";

    #  Save the cert in DER format:
    fname = "cert" + i.to_s() + ".der"
    cert.ExportCertDerFile(fname)

    #  Save the cert in PEM format:
    fname = "cert" + i.to_s() + ".pem"
    cert.ExportCertPemFile(fname)

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

        #  Get the private key.

        pvkey = cert.ExportPrivateKey()

        #  Save the private key to a PKCS8 DER-encoded file
        fname = "pvkey" + i.to_s() + "_pkcs8.der"
        pvkey.SavePkcs8File(fname)

        #  Save the private key to a PKCS8 PEM-encoded file
        fname = "pvkey" + i.to_s() + "_pkcs8.pem"
        pvkey.SavePkcs8PemFile(fname)

        #  Save the private key to a RSA DER-encoded file
        fname = "pvkey" + i.to_s() + "_rsa.der"
        pvkey.SaveRsaDerFile(fname)

        #  Save the private key to a RSA PEM-encoded file
        fname = "pvkey" + i.to_s() + "_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" + i.to_s() + ".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" + i.to_s() + "_openSsl.der"
    pubkey.SaveOpenSslDerFile(fname)

    #  Save to an OpenSSL PEM format file:
    fname = "pubkey" + i.to_s() + "_openSsl.pem"
    pubkey.SaveOpenSslPemFile(fname)

    #  Save to an RSA DER format file:
    fname = "pubkey" + i.to_s() + "_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" + i.to_s() + ".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.

 

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

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