Sample code for 30+ languages & platforms
PHP ActiveX

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 PHP ActiveX Downloads

PHP ActiveX
<?php

$success = 0;

// For this example, I have a certificate in raw base64 format (not PEM),
// that looks like this:  "MIIGkDCCBHigAwIBAgIUMDA ... s/iqLsLA=="
$sbCertBase64 = new COM("Chilkat.StringBuilder");
$success = $sbCertBase64->LoadFile('qa_data/certs/base64Cert.txt','utf-8');

$cert = new COM("Chilkat.Cert");
$success = $cert->LoadFromBase64($sbCertBase64->getAsString());
if ($success == 0) {
    print $cert->LastErrorText . "\n";
    exit;
}

// Get the public key.
$pubKey = new COM("Chilkat.PublicKey");
$cert->GetPublicKey($pubKey);

$numBits = $pubKey->KeySize;
print 'Number of bits = ' . $numBits . "\n";

// If using an older version of Chilkat, the key size can be obtained like this:
$xml = new COM("Chilkat.Xml");
$xml->LoadXml($pubKey->getXml());

$binDat = new COM("Chilkat.BinData");
$binDat->AppendEncoded($xml->getChildContent('Modulus'),'base64');

$numBits = 8 * $binDat->NumBytes;
print 'Number of bits = ' . $numBits . "\n";

?>