Java Requires Chilkat v11.0.0+
Chilkat2 Java
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 Java Downloads
import com.chilkatsoft.chilkat2.BinData;
import com.chilkatsoft.chilkat2.Cert;
import com.chilkatsoft.chilkat2.ChilkatException;
import com.chilkatsoft.chilkat2.PublicKey;
import com.chilkatsoft.chilkat2.StringBuilder;
import com.chilkatsoft.chilkat2.Xml;
public class ChilkatExample {
public static void main(String[] args) {
// For this example, I have a certificate in raw base64 format (not PEM),
// that looks like this: "MIIGkDCCBHigAwIBAgIUMDA ... s/iqLsLA=="
StringBuilder sbCertBase64 = new StringBuilder();
sbCertBase64.loadFile("qa_data/certs/base64Cert.txt", "utf-8");
Cert cert = new Cert();
try {
cert.loadFromBase64(sbCertBase64.getAsString());
} catch (ChilkatException e) {
System.out.println(e.getLastErrorText());
return;
}
// Get the public key.
PublicKey pubKey = new PublicKey();
cert.getPublicKey(pubKey);
int numBits = pubKey.getKeySize();
System.out.println("Number of bits = " + numBits);
// If using an older version of Chilkat, the key size can be obtained like this:
Xml xml = new Xml();
xml.loadXml(pubKey.getXml());
BinData binDat = new BinData();
binDat.appendEncoded(xml.getChildContent("Modulus"), "base64");
numBits = 8 * binDat.getNumBytes();
System.out.println("Number of bits = " + numBits);
}
}