Sample code for 30+ languages & platforms
PHP 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 PHP Downloads

Chilkat2 PHP
<?php

use Chilkat2\BinData;
use Chilkat2\Cert;
use Chilkat2\PublicKey;
use Chilkat2\StringBuilder;
use Chilkat2\Xml;

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

$cert = new Cert();
try {
    $cert->LoadFromBase64($sbCertBase64->GetAsString());
} catch (\Chilkat2\Exception $e) {
    echo $e->getMessage(), "\n";
    exit;
}

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

$numBits = $pubKey->KeySize;
echo 'Number of bits = ', $numBits, "\n";

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

$binDat = new BinData();
$binDat->AppendEncoded($xml->GetChildContent('Modulus'), 'base64');

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