Xbase++
Xbase++
Get Public Key from CSR
See more CSR Examples
Demonstrates how to get the public key from a CSR.Chilkat Xbase++ Downloads
LOCAL nSuccess
LOCAL oPem
LOCAL cNoPassword
LOCAL cStrBase64
LOCAL oAsn
LOCAL oXml
LOCAL cStrModulusHex
LOCAL oBd
LOCAL cModulus64
LOCAL oXmlPubKey
LOCAL oPubkey
LOCAL nPreferPkcs1
nSuccess := 0
// This requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
oPem := CreateObject("Chilkat.Pem")
// No password is required. Pass an empty password string..
cNoPassword := ""
nSuccess := oPem:LoadPemFile("qa_data/csr/csr2.pem", cNoPassword)
IF (nSuccess != 1)
? oPem:LastErrorText
oPem:destroy()
RETURN
ENDIF
cStrBase64 := oPem:GetEncodedItem("csr", "", "base64", 0)
oAsn := CreateObject("Chilkat.Asn")
nSuccess := oAsn:LoadEncoded(cStrBase64, "base64")
IF (nSuccess != 1)
? oAsn:LastErrorText
oPem:destroy()
oAsn:destroy()
RETURN
ENDIF
// Convert the ASN.1 to XML.
oXml := CreateObject("Chilkat.Xml")
nSuccess := oXml:LoadXml(oAsn:AsnToXml())
? oXml:GetXml()
? "----"
cStrModulusHex := oXml:GetChildContent("bits")
? "strModulusHex = " + cStrModulusHex
? "----"
// We need the modulus as base64.
oBd := CreateObject("Chilkat.BinData")
oBd:AppendEncoded(cStrModulusHex, "hex")
cModulus64 := oBd:GetEncoded("base64")
? "modulus64 = " + cModulus64
? "----"
// Build the XML for the public key.
oXmlPubKey := CreateObject("Chilkat.Xml")
oXmlPubKey:Tag := "RSAPublicKey"
oXmlPubKey:UpdateChildContent("Modulus", cModulus64)
// The RSA exponent will always be decimal 65537 (base64 = AQAB)
oXmlPubKey:UpdateChildContent("Exponent", "AQAB")
? "RSA public key as XML:"
? oXmlPubKey:GetXml()
? "----"
// Load the XML into a Chilkat public key object.
oPubkey := CreateObject("Chilkat.PublicKey")
nSuccess := oPubkey:LoadFromString(oXmlPubKey:GetXml())
IF (nSuccess != 1)
? oPubkey:LastErrorText
oPem:destroy()
oAsn:destroy()
oXml:destroy()
oBd:destroy()
oXmlPubKey:destroy()
oPubkey:destroy()
RETURN
ENDIF
// Show the public key as PEM.
nPreferPkcs1 := 1
? oPubkey:GetPem(nPreferPkcs1)
oPem:destroy()
oAsn:destroy()
oXml:destroy()
oBd:destroy()
oXmlPubKey:destroy()
oPubkey:destroy()