Sample code for 30+ languages & platforms
PowerBuilder

Export a Public Key as DER into a BinData

Demonstrates the Chilkat PublicKey.GetDerBd method, which writes the public key as binary DER into a BinData. The first argument selects the representation (as for GetPem) and the second is the BinData that receives the bytes (its contents are replaced).

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: The in-memory binary export: the raw DER lands in a BinData ready to hash, transmit, or hand to another API without a temporary file. It is the BinData counterpart to the text exporters, and the first argument picks the same PKCS #1 versus SubjectPublicKeyInfo structure.

Chilkat PowerBuilder Downloads

PowerBuilder
integer li_rc
integer li_Success
oleobject loo_PubKey
integer li_PreferPkcs1
oleobject loo_Bd

li_Success = 0

//  Load a public key to export.  LoadFromFile inspects the content, so it accepts DER, PEM, XML,
//  JWK, and other supported representations.
loo_PubKey = create oleobject
li_rc = loo_PubKey.ConnectToNewObject("Chilkat.PublicKey")
if li_rc < 0 then
    destroy loo_PubKey
    MessageBox("Error","Connecting to COM object failed")
    return
end if
li_Success = loo_PubKey.LoadFromFile("qa_data/public.pem")
if li_Success = 0 then
    Write-Debug loo_PubKey.LastErrorText
    destroy loo_PubKey
    return
end if

//  Write the public key as binary DER into a BinData.  The 1st argument selects the representation
//  (as for GetPem) and the 2nd is the BinData that receives the bytes (its contents are replaced).
li_PreferPkcs1 = 0
loo_Bd = create oleobject
li_rc = loo_Bd.ConnectToNewObject("Chilkat.BinData")

li_Success = loo_PubKey.GetDerBd(li_PreferPkcs1,loo_Bd)
if li_Success = 0 then
    Write-Debug loo_PubKey.LastErrorText
    destroy loo_PubKey
    destroy loo_Bd
    return
end if

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


destroy loo_PubKey
destroy loo_Bd