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

Dart
import 'package:chilkat/chilkat.dart';

void main() {
  // For this example, I have a certificate in raw base64 format (not PEM),
  // that looks like this:  "MIIGkDCCBHigAwIBAgIUMDA ... s/iqLsLA=="
  final sbCertBase64 = CkStringBuilder();
  sbCertBase64.loadFile('qa_data/certs/base64Cert.txt', 'utf-8');

  final cert = CkCert();
  try {
    cert.loadFromBase64(sbCertBase64.getAsString());
  } on ChilkatException catch (e) {
    print(e.lastErrorText);
    return;
  }

  // Get the public key.
  final pubKey = CkPublicKey();
  cert.getPublicKey(pubKey);

  var numBits = pubKey.keySize;
  print('Number of bits = $numBits');

  // If using an older version of Chilkat, the key size can be obtained like this:
  final xml = CkXml();
  xml.loadXml(pubKey.getXml());

  final binDat = CkBinData();
  binDat.appendEncoded(xml.getChildContent('Modulus'), 'base64');

  numBits = 8 * binDat.numBytes;
  print('Number of bits = $numBits');
}