Android™
Android™
Export a Private Key as PKCS #8 PEM
See more Private Key Examples
Demonstrates the Chilkat PrivateKey.GetPkcs8Pem method, which exports the loaded key as unencrypted PKCS #8 PEM (-----BEGIN PRIVATE KEY-----). It takes no arguments.
Note: The file paths are relative to the application's current working directory. Absolute paths may also be used. Supply the paths appropriate to your own environment.
Background: PKCS #8 is the modern, algorithm-independent container — the same
PRIVATE KEY PEM wrapper holds RSA, EC, or Ed25519 keys alike, which is why it is the format most current tooling expects. Being unencrypted, the PEM carries the key material in the clear, so treat the output as sensitive. Use GetPkcs8EncryptedPem when the exported key needs a passphrase.Chilkat Android™ Downloads
// 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;
// Load a private key to export.
CkPrivateKey privKey = new CkPrivateKey();
success = privKey.LoadPemFile("qa_data/private.pem");
if (success == false) {
Log.i(TAG, privKey.lastErrorText());
return;
}
// Export as unencrypted PKCS #8 PEM ("-----BEGIN PRIVATE KEY-----"). PKCS #8 is an
// algorithm-independent container that works for RSA, EC, and Ed25519.
String pem = privKey.getPkcs8Pem();
if (privKey.get_LastMethodSuccess() == false) {
Log.i(TAG, privKey.lastErrorText());
return;
}
Log.i(TAG, pem);
}
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."
}
}