(C#) Get a Certificate's Key Size
Demonstrates how to get the RSA key size of a certificate (for example, 1024-bit, 2048-bit, etc.) Note: This example requires Chilkat v11.0.0 or greater.
bool success = false;
// For this example, I have a certificate in raw base64 format (not PEM),
// that looks like this: "MIIGkDCCBHigAwIBAgIUMDA ... s/iqLsLA=="
Chilkat.StringBuilder sbCertBase64 = new Chilkat.StringBuilder();
success = sbCertBase64.LoadFile("qa_data/certs/base64Cert.txt","utf-8");
Chilkat.Cert cert = new Chilkat.Cert();
success = cert.LoadFromBase64(sbCertBase64.GetAsString());
if (success == false) {
Debug.WriteLine(cert.LastErrorText);
return;
}
// Get the public key.
Chilkat.PublicKey pubKey = new Chilkat.PublicKey();
cert.GetPublicKey(pubKey);
int numBits = pubKey.KeySize;
Debug.WriteLine("Number of bits = " + Convert.ToString(numBits));
// If using an older version of Chilkat, the key size can be obtained like this:
Chilkat.Xml xml = new Chilkat.Xml();
xml.LoadXml(pubKey.GetXml());
Chilkat.BinData binDat = new Chilkat.BinData();
binDat.AppendEncoded(xml.GetChildContent("Modulus"),"base64");
numBits = 8 * binDat.NumBytes;
Debug.WriteLine("Number of bits = " + Convert.ToString(numBits));
|