Sample code for 30+ languages & platforms
Visual FoxPro

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 Visual FoxPro Downloads

Visual FoxPro
LOCAL lnSuccess
LOCAL loSbCertBase64
LOCAL loCert
LOCAL loPubKey
LOCAL lnNumBits
LOCAL loXml
LOCAL loBinDat

lnSuccess = 0

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

loCert = CreateObject('Chilkat.Cert')
lnSuccess = loCert.LoadFromBase64(loSbCertBase64.GetAsString())
IF (lnSuccess = 0) THEN
    ? loCert.LastErrorText
    RELEASE loSbCertBase64
    RELEASE loCert
    CANCEL
ENDIF

* Get the public key.
loPubKey = CreateObject('Chilkat.PublicKey')
loCert.GetPublicKey(loPubKey)

lnNumBits = loPubKey.KeySize
? "Number of bits = " + STR(lnNumBits)

* If using an older version of Chilkat, the key size can be obtained like this:
loXml = CreateObject('Chilkat.Xml')
loXml.LoadXml(loPubKey.GetXml())

loBinDat = CreateObject('Chilkat.BinData')
loBinDat.AppendEncoded(loXml.GetChildContent("Modulus"),"base64")

lnNumBits = 8 * loBinDat.NumBytes
? "Number of bits = " + STR(lnNumBits)

RELEASE loSbCertBase64
RELEASE loCert
RELEASE loPubKey
RELEASE loXml
RELEASE loBinDat