Sample code for 30+ languages & platforms
PowerBuilder

Get the Public Key from a Private Key

See more Private Key Examples

Demonstrates the Chilkat PrivateKey.ToPublicKey method, which extracts the public portion of the loaded private key into an independent PublicKey object. The only argument is the PublicKey that receives the copy.

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: A key pair's public half is derived from the private half, and this is how you obtain it — the public key you distribute for others to verify signatures or encrypt to you, while the private key stays secret. The resulting PublicKey is an independent copy, so exporting or sharing it never risks exposing the private material. From there, export it as PEM, JWK, or XML as needed.

Chilkat PowerBuilder Downloads

PowerBuilder
integer li_rc
integer li_Success
oleobject loo_PrivKey
oleobject loo_PubKey
integer li_PreferPkcs1
string ls_PubPem

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

//  Extract the public portion of the private key into an independent PublicKey object.  The
//  PublicKey is a separate copy, unaffected by later changes to the PrivateKey.
loo_PubKey = create oleobject
li_rc = loo_PubKey.ConnectToNewObject("Chilkat.PublicKey")

li_Success = loo_PrivKey.ToPublicKey(loo_PubKey)
if li_Success = 0 then
    Write-Debug loo_PrivKey.LastErrorText
    destroy loo_PrivKey
    destroy loo_PubKey
    return
end if

//  The PublicKey can now be exported or shared -- for example as PEM.  The argument prefers the
//  traditional PKCS #1 PEM when a traditional representation exists.
li_PreferPkcs1 = 1
ls_PubPem = loo_PubKey.GetPem(li_PreferPkcs1)
if loo_PubKey.LastMethodSuccess = 0 then
    Write-Debug loo_PubKey.LastErrorText
    destroy loo_PrivKey
    destroy loo_PubKey
    return
end if

Write-Debug ls_PubPem


destroy loo_PrivKey
destroy loo_PubKey