Android™
Android™
Load a Public Key from a String
Demonstrates the Chilkat PublicKey.LoadFromString method, which loads a public key from a string. The only argument is the string. Chilkat examines the content and recognizes PEM public keys, unencrypted PEM private keys (taking only the public portion), XML, JWK, and more.
Background: This is the forgiving text loader: rather than requiring you to know the exact format, it inspects whatever string you provide and figures it out. A useful convenience is that it accepts an unencrypted private key too and keeps only the public half — handy when you have a key pair but need just the public key. Use it whenever the textual format is uncertain or varies by source.
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.LoadFromString method, which loads a public key from a string. The
// only argument is the string. Chilkat examines the content and recognizes PEM public keys,
// unencrypted PEM private keys (using only the public portion), XML, JWK, and more.
CkPublicKey pubKey = new CkPublicKey();
// The PEM text of a public key.
String keyText = "-----BEGIN PUBLIC KEY-----\nMIIBIjANBgkq...IDAQAB\n-----END PUBLIC KEY-----";
success = pubKey.LoadFromString(keyText);
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."
}
}