Sample code for 30+ languages & platforms
PowerBuilder

Convert RSA Private Key to Public Key

See more RSA Examples

Demonstrates how to get a public RSA key from a private RSA key.

Chilkat PowerBuilder Downloads

PowerBuilder
integer li_rc
integer li_Success
oleobject loo_PrivKey
oleobject loo_PubKey
integer li_BPreferPkcs1

li_Success = 0

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

// Step 1: Load the private key from a source.
// (Chilkat can load private keys from all types of formats, and from in-memory bytes or encoded strings.
// see the online reference documentation for more options.)
li_Success = loo_PrivKey.LoadPemFile("qa_data/pem/VP_Private.pem")
if li_Success = 0 then
    Write-Debug loo_PrivKey.LastErrorText
    destroy loo_PrivKey
    return
end if

// Step 2: Get the public key object from the private key object.
loo_PubKey = create oleobject
li_rc = loo_PubKey.ConnectToNewObject("Chilkat.PublicKey")

loo_PrivKey.ToPublicKey(loo_PubKey)

// Step 3: Save the public key in a desired format. 
// (Chilkat can load or save public and private keys in many different formats.  See
// the online reference documentation for more options.)

// Saves to a PKCS8 PEM file.
li_BPreferPkcs1 = 0
li_Success = loo_PubKey.SavePemFile(li_BPreferPkcs1,"qa_data/pem/VP_Public.pem")
if loo_PubKey.LastMethodSuccess = 0 then
    Write-Debug loo_PubKey.LastErrorText
    destroy loo_PrivKey
    destroy loo_PubKey
    return
end if

Write-Debug "Extracted and saved public key from private key."


destroy loo_PrivKey
destroy loo_PubKey