Sample code for 30+ languages & platforms
Android™

Load a Private Key from XML Text

See more Private Key Examples

Demonstrates the Chilkat PrivateKey.LoadXml method, which loads a private key from XML text. The only argument is the XML string. Supported roots include RSAKeyValue, DSAKeyValue, ECCKeyValue, and Ed25519KeyValue.

Background: XML key representations such as RSAKeyValue come from the .NET/XML-DSIG world, where keys are exchanged as XML elements. This loads that form — the inverse of GetXml — which is handy for interoperating with .NET code or systems built around XML signatures. It is a plaintext format, so protect the XML like any unencrypted key material.

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.LoadXml method, which loads a private key from XML text.  The only
    //  argument is the XML string.  Supported roots include RSAKeyValue, DSAKeyValue, ECCKeyValue,
    //  and Ed25519KeyValue.

    CkPrivateKey privKey = new CkPrivateKey();

    //  The XML text of a private key (for example as produced by GetXml).
    String xmlText = "<RSAKeyValue><Modulus>...</Modulus><Exponent>AQAB</Exponent>...</RSAKeyValue>";

    success = privKey.LoadXml(xmlText);
    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."
  }
}