Sample code for 30+ languages & platforms
Android™

Load a Public Key from a File

Demonstrates the Chilkat PublicKey.LoadFromFile method, which loads a public key from a file. The only argument is the file path. Chilkat examines the file content rather than the extension, accepting binary DER, PEM, Chilkat XML, JWK, and other supported representations.

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: The most convenient loader when a key sits in a file of unknown format — it reads the bytes and detects DER, PEM, XML, or JWK without you having to match a format-specific method. Because it keys off content rather than the filename, a mislabeled extension does not trip it up. This is the pragmatic default for applications that accept public keys from users or varied sources.

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 PublicKey.LoadFromFile method, which loads a public key from a file.  The only
    //  argument is the file path.  Chilkat examines the file content (not the extension), accepting
    //  binary DER, PEM, Chilkat XML, JWK, and other supported representations.

    CkPublicKey pubKey = new CkPublicKey();

    success = pubKey.LoadFromFile("qa_data/public.pem");
    if (success == false) {
        Log.i(TAG, pubKey.lastErrorText());
        return;
        }

    Log.i(TAG, "Loaded a " + pubKey.keyType() + " public 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."
  }
}