Sample code for 30+ languages & platforms
Java Requires Chilkat v11.0.0+

Export Digital Certificate's Public Key

See more Certificates Examples

The ExportPublicKey method can be called to get a certificate's public key. It can then be saved to any of a number of formats: (1) OpenSSL DER, (2) OpenSSL PEM, (3) RSA DER, (4) XML.

Chilkat Java Downloads

Chilkat2 Java
import com.chilkatsoft.chilkat2.Cert;
import com.chilkatsoft.chilkat2.ChilkatException;
import com.chilkatsoft.chilkat2.PublicKey;

public class ChilkatExample {

    public static void main(String[] args) {
        Cert cert = new Cert();

        // LoadFromFile will load virtually any certificate format file.
        // It will auto-recognize the format and load appropiately.
        try {
            cert.loadFromFile("/Users/chilkat/testData/cer/chilkat.cer");
        } catch (ChilkatException e) {
            System.out.println(e.getLastErrorText());
            return;
        }

        // Get the public key:
        PublicKey pubkey = new PublicKey();
        cert.getPublicKey(pubkey);

        // Save to various formats:
        try {
            pubkey.saveDerFile(false, "/Users/chilkat/testData/pubkeys/chilkat_pkcs8.der");

            pubkey.savePemFile(false, "/Users/chilkat/testData/pubkeys/chilkat.pem");

            pubkey.saveDerFile(true, "/Users/chilkat/testData/pubkeys/chilkat_pkcs1.der");

            pubkey.saveXmlFile("/Users/chilkat/testData/pubkeys/chilkat.xml");
        } catch (ChilkatException e) {
            System.out.println(e.getLastErrorText());
            return;
        }

        System.out.println("Public key exported to all file formats.");
    }
}