Sample code for 30+ languages & platforms
Java

Load a Private Key from a PEM File

See more Private Key Examples

Demonstrates the Chilkat PrivateKey.LoadPemFile method, which loads a private key from a PEM file. The only argument is the file path. It cannot load a password-protected key — use LoadEncryptedPemFile for that.

Note: The file path is relative to the application's current working directory. Absolute paths may also be used. Supply the path to your own key file.

Background: The file-based counterpart to LoadPem, for the common case where the key lives in a .pem file. Like LoadPem it handles only unencrypted PEM; if the file is encrypted the load fails, signaling that you need the password-aware loader. When the exact on-disk format is uncertain, LoadAnyFormatFile auto-detects it.

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;

    //  Demonstrates the PrivateKey.LoadPemFile method, which loads a private key from a PEM file.  The
    //  only argument is the file path.  It cannot load a password-protected key -- use
    //  LoadEncryptedPemFile for that.

    CkPrivateKey privKey = new CkPrivateKey();

    success = privKey.LoadPemFile("qa_data/private_unencrypted.pem");
    if (success == false) {
        System.out.println(privKey.lastErrorText());
        return;
        }

    System.out.println("Loaded a " + privKey.keyType() + " private key.");
  }
}