Sample code for 30+ languages & platforms
Java

Load Certificate from .cer and Private Key from .pem

See more Certificates Examples

Load a certificate from a .cer and its associated private key from a .pem.

Chilkat Java Downloads

Java
import com.chilkatsoft.*;

public class ChilkatExample {

  static {
    try {
        System.loadLibrary("chilkat");
    } catch (UnsatisfiedLinkError e) {
      System.err.println("Native code library failed to load.\n" + e);
      System.exit(1);
    }
  }

  public static void main(String argv[])
  {
    boolean success = false;

    CkCert cert = new CkCert();
    success = cert.LoadFromFile("C:/certs_and_keys/Certificate.cer");
    if (success == false) {
        System.out.println(cert.lastErrorText());
        return;
        }

    CkStringBuilder sbPem = new CkStringBuilder();
    success = sbPem.LoadFile("C:/certs_and_keys/PrivateKey.pem","utf-8");
    if (success == false) {
        System.out.println("Failed to load private key PEM");
        return;
        }

    success = cert.SetPrivateKeyPem(sbPem.getAsString());
    if (success == false) {
        System.out.println(cert.lastErrorText());
        return;
        }

    System.out.println("The certificate and associated private key are now loaded and ready for signing.");
  }
}