Sample code for 30+ languages & platforms
Xbase++ Requires Chilkat v11.0.0+

Get a Certificate's Key Size

See more Certificates Examples

Demonstrates how to get the RSA key size of a certificate (for example, 1024-bit, 2048-bit, etc.)

Chilkat Xbase++ Downloads

Xbase++
LOCAL nSuccess
LOCAL oSbCertBase64
LOCAL oCert
LOCAL oPubKey
LOCAL nNumBits
LOCAL oXml
LOCAL oBinDat

nSuccess := 0

//  For this example, I have a certificate in raw base64 format (not PEM),
//  that looks like this:  "MIIGkDCCBHigAwIBAgIUMDA ... s/iqLsLA=="
oSbCertBase64 := CreateObject("Chilkat.StringBuilder")
nSuccess := oSbCertBase64:LoadFile("qa_data/certs/base64Cert.txt", "utf-8")

oCert := CreateObject("Chilkat.Cert")
nSuccess := oCert:LoadFromBase64(oSbCertBase64:GetAsString())
IF (nSuccess == 0)
    ? oCert:LastErrorText
    oSbCertBase64:destroy()
    oCert:destroy()
    RETURN
ENDIF

//  Get the public key.
oPubKey := CreateObject("Chilkat.PublicKey")
oCert:GetPublicKey(oPubKey)

nNumBits := oPubKey:KeySize
? "Number of bits = " + Str(nNumBits)

//  If using an older version of Chilkat, the key size can be obtained like this:
oXml := CreateObject("Chilkat.Xml")
oXml:LoadXml(oPubKey:GetXml())

oBinDat := CreateObject("Chilkat.BinData")
oBinDat:AppendEncoded(oXml:GetChildContent("Modulus"), "base64")

nNumBits := 8 * oBinDat:NumBytes
? "Number of bits = " + Str(nNumBits)

oSbCertBase64:destroy()
oCert:destroy()
oPubKey:destroy()
oXml:destroy()
oBinDat:destroy()