Perl Requires Chilkat v11.0.0+
Chilkat2 Perl
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 Perl Downloads
use strict;
use warnings;
use Chilkat2;
# For this example, I have a certificate in raw base64 format (not PEM),
# that looks like this: "MIIGkDCCBHigAwIBAgIUMDA ... s/iqLsLA=="
my $sbCertBase64 = Chilkat2::StringBuilder->new;
my $success = $sbCertBase64->LoadFile('qa_data/certs/base64Cert.txt', 'utf-8');
my $cert = Chilkat2::Cert->new;
$success = $cert->LoadFromBase64($sbCertBase64->GetAsString());
if (!$success) {
print $cert->LastErrorText, "\n";
exit;
}
# Get the public key.
my $pubKey = Chilkat2::PublicKey->new;
$cert->GetPublicKey($pubKey);
my $numBits = $pubKey->KeySize;
print 'Number of bits = ', $numBits, "\n";
# If using an older version of Chilkat, the key size can be obtained like this:
my $xml = Chilkat2::Xml->new;
$xml->LoadXml($pubKey->GetXml());
my $binDat = Chilkat2::BinData->new;
$binDat->AppendEncoded($xml->GetChildContent('Modulus'), 'base64');
$numBits = 8 * $binDat->NumBytes;
print 'Number of bits = ', $numBits, "\n";