Android™
Android™
Load a Public Key from Base64 DER
Demonstrates the Chilkat PublicKey.LoadBase64 method, which loads a public key from standard Base64-encoded DER. The only argument is the Base64 string; whitespace within it is permitted.
Background: A public key in DER (binary) form is often carried as Base64 text so it can travel through channels that expect a string — a config value, a JSON field, a certificate export. This loader decodes that text and inspects the DER to determine the key type and structure automatically. It is the plain-Base64 counterpart to loading a full PEM block, which additionally has the
BEGIN/END armor.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;
// Demonstrates the PublicKey.LoadBase64 method, which loads a public key from standard
// Base64-encoded DER. The only argument is the Base64 string (whitespace is allowed).
CkPublicKey pubKey = new CkPublicKey();
// The Base64 of the DER-encoded public key.
String keyStr = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA...IDAQAB";
success = pubKey.LoadBase64(keyStr);
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."
}
}