Sample code for 30+ languages & platforms
PowerBuilder

Export a Private Key as DER into a BinData

See more Private Key Examples

Demonstrates the Chilkat PrivateKey.GetPkcsBd method, which exports the key as DER into a BinData. The first argument selects PKCS #1 (true) versus PKCS #8 (false), the second is the password (empty for unencrypted), and the third is the BinData that receives the bytes.

Note: The file paths are relative to the application's current working directory. Absolute paths may also be used. Supply the paths appropriate to your own environment.

Background: This is the in-memory binary export — the raw DER bytes land in a BinData ready to hash, encrypt, or hand to another API without a temporary file. One method covers several cases: the boolean picks the traditional (PKCS #1) versus modern (PKCS #8) structure, and a nonempty password produces an encrypted PKCS #8 export (with the cipher from Pkcs8EncryptAlg). Source any password securely.

Chilkat PowerBuilder Downloads

PowerBuilder
integer li_rc
integer li_Success
oleobject loo_PrivKey
string ls_Password
integer li_UsePkcs1
oleobject loo_Bd

li_Success = 0

//  Load a private key to export.
loo_PrivKey = create oleobject
li_rc = loo_PrivKey.ConnectToNewObject("Chilkat.PrivateKey")
if li_rc < 0 then
    destroy loo_PrivKey
    MessageBox("Error","Connecting to COM object failed")
    return
end if
li_Success = loo_PrivKey.LoadPemFile("qa_data/private.pem")
if li_Success = 0 then
    Write-Debug loo_PrivKey.LastErrorText
    destroy loo_PrivKey
    return
end if

//  The key password is not hard-coded in a real application; obtain it from an interactive
//  prompt, environment variable, or a secrets vault.
ls_Password = "myKeyPassword"

//  Export the key as DER into a BinData.  The 1st argument selects PKCS #1 (1) versus PKCS #8
//  (0); the 2nd is the password (empty string for an unencrypted export); the 3rd is the
//  BinData that receives the DER bytes.
li_UsePkcs1 = 0
loo_Bd = create oleobject
li_rc = loo_Bd.ConnectToNewObject("Chilkat.BinData")

li_Success = loo_PrivKey.GetPkcsBd(li_UsePkcs1,ls_Password,loo_Bd)
if li_Success = 0 then
    Write-Debug loo_PrivKey.LastErrorText
    destroy loo_PrivKey
    destroy loo_Bd
    return
end if

Write-Debug "Exported " + string(loo_Bd.NumBytes) + " DER bytes."


destroy loo_PrivKey
destroy loo_Bd