Sample code for 30+ languages & platforms
PowerBuilder

Get Public Key from CSR

See more CSR Examples

Demonstrates how to get the public key from a CSR.

Chilkat PowerBuilder Downloads

PowerBuilder
integer li_rc
integer li_Success
oleobject loo_Pem
string ls_NoPassword
string ls_StrBase64
oleobject loo_Asn
oleobject loo_Xml
string ls_StrModulusHex
oleobject loo_Bd
string ls_Modulus64
oleobject loo_XmlPubKey
oleobject loo_Pubkey
integer li_PreferPkcs1

li_Success = 0

// This requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.

loo_Pem = create oleobject
li_rc = loo_Pem.ConnectToNewObject("Chilkat.Pem")
if li_rc < 0 then
    destroy loo_Pem
    MessageBox("Error","Connecting to COM object failed")
    return
end if

// No password is required.  Pass an empty password string..
ls_NoPassword = ""
li_Success = loo_Pem.LoadPemFile("qa_data/csr/csr2.pem",ls_NoPassword)
if li_Success <> 1 then
    Write-Debug loo_Pem.LastErrorText
    destroy loo_Pem
    return
end if

ls_StrBase64 = loo_Pem.GetEncodedItem("csr","","base64",0)

loo_Asn = create oleobject
li_rc = loo_Asn.ConnectToNewObject("Chilkat.Asn")

li_Success = loo_Asn.LoadEncoded(ls_StrBase64,"base64")
if li_Success <> 1 then
    Write-Debug loo_Asn.LastErrorText
    destroy loo_Pem
    destroy loo_Asn
    return
end if

// Convert the ASN.1 to XML.
loo_Xml = create oleobject
li_rc = loo_Xml.ConnectToNewObject("Chilkat.Xml")

li_Success = loo_Xml.LoadXml(loo_Asn.AsnToXml())
Write-Debug loo_Xml.GetXml()
Write-Debug "----"

ls_StrModulusHex = loo_Xml.GetChildContent("bits")
Write-Debug "strModulusHex = " + ls_StrModulusHex
Write-Debug "----"

// We need the modulus as base64.
loo_Bd = create oleobject
li_rc = loo_Bd.ConnectToNewObject("Chilkat.BinData")

loo_Bd.AppendEncoded(ls_StrModulusHex,"hex")
ls_Modulus64 = loo_Bd.GetEncoded("base64")
Write-Debug "modulus64 = " + ls_Modulus64
Write-Debug "----"

// Build the XML for the public key.
loo_XmlPubKey = create oleobject
li_rc = loo_XmlPubKey.ConnectToNewObject("Chilkat.Xml")

loo_XmlPubKey.Tag = "RSAPublicKey"
loo_XmlPubKey.UpdateChildContent("Modulus",ls_Modulus64)
// The RSA exponent will always be decimal 65537 (base64 = AQAB)
loo_XmlPubKey.UpdateChildContent("Exponent","AQAB")

Write-Debug "RSA public key as XML:"
Write-Debug loo_XmlPubKey.GetXml()
Write-Debug "----"

// Load the XML into a Chilkat public key object.
loo_Pubkey = create oleobject
li_rc = loo_Pubkey.ConnectToNewObject("Chilkat.PublicKey")

li_Success = loo_Pubkey.LoadFromString(loo_XmlPubKey.GetXml())
if li_Success <> 1 then
    Write-Debug loo_Pubkey.LastErrorText
    destroy loo_Pem
    destroy loo_Asn
    destroy loo_Xml
    destroy loo_Bd
    destroy loo_XmlPubKey
    destroy loo_Pubkey
    return
end if

// Show the public key as PEM.
li_PreferPkcs1 = 1
Write-Debug loo_Pubkey.GetPem(li_PreferPkcs1)


destroy loo_Pem
destroy loo_Asn
destroy loo_Xml
destroy loo_Bd
destroy loo_XmlPubKey
destroy loo_Pubkey