Sample code for 30+ languages & platforms
Android™

Load a Private Key from PEM Text

See more Private Key Examples

Demonstrates the Chilkat PrivateKey.LoadPem method, which loads a private key from PEM text. The only argument is the PEM string. It has no password argument, so it cannot load an encrypted PEM — use LoadEncryptedPem for that.

Background: PEM is the Base64 text format (delimited by -----BEGIN----- lines) that most tooling uses for keys. This method is for the plain, unencrypted case where the key material sits in the clear inside the PEM — convenient, but it means the file itself is the only thing protecting the key. When the PEM is password-protected (a BEGIN ENCRYPTED PRIVATE KEY block or a legacy encrypted header), reach for the Encrypted loader instead.

Chilkat Android™ Downloads

Android™
// Important: Don't forget to include the call to System.loadLibrary
// as shown at the bottom of this code sample.
package com.test;

import android.app.Activity;
import com.chilkatsoft.*;

import android.widget.TextView;
import android.os.Bundle;

public class SimpleActivity extends Activity {

  private static final String TAG = "Chilkat";

  // Called when the activity is first created.
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    boolean success = false;

    //  Demonstrates the PrivateKey.LoadPem method, which loads a private key from PEM text.  The only
    //  argument is the PEM string.  This method has no password argument, so it cannot load an
    //  encrypted PEM -- use LoadEncryptedPem for that.

    CkPrivateKey privKey = new CkPrivateKey();

    //  The PEM text of an unencrypted private key.
    String pemText = "-----BEGIN PRIVATE KEY-----\nMIIEvAIBADANBgkq...\n-----END PRIVATE KEY-----";

    success = privKey.LoadPem(pemText);
    if (success == false) {
        Log.i(TAG, privKey.lastErrorText());
        return;
        }

    Log.i(TAG, "Loaded a " + privKey.keyType() + " private key.");

  }

  static {
      System.loadLibrary("chilkat");

      // Note: If the incorrect library name is passed to System.loadLibrary,
      // then you will see the following error message at application startup:
      //"The application <your-application-name> has stopped unexpectedly. Please try again."
  }
}