Sample code for 30+ languages & platforms
Android™

Load a Private Key from a JWK

See more Private Key Examples

Demonstrates the Chilkat PrivateKey.LoadJwk method, which loads a private key from JWK (JSON Web Key) JSON. The only argument is the JWK JSON string. Supported key types are RSA, EC, and Ed25519 (kty=OKP).

Background: JWK is the JSON key format used throughout modern web security — JWT signing, OAuth, OpenID Connect — so this loader is what you use to consume a key from a JWK Set or an identity provider. A private JWK includes the private parameters (d and, for RSA, the CRT values); a public-only JWK omits them. As JSON in the clear, a private JWK must be handled as sensitive 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.LoadJwk method, which loads a private key from JWK (JSON Web Key)
    //  JSON.  The only argument is the JWK JSON string.  Supported key types are RSA, EC, and
    //  Ed25519 (kty=OKP).

    CkPrivateKey privKey = new CkPrivateKey();

    //  A private JWK.  For a real key, "d" (and the other private parameters) would be present.
    String jwk = "{\"kty\":\"EC\",\"crv\":\"P-256\",\"x\":\"...\",\"y\":\"...\",\"d\":\"...\"}";

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