Sample code for 30+ languages & platforms
Android™

JWE using ECDH-ES+A256KW

See more JSON Web Encryption (JWE) Examples

Create a JWE with the following public/private key pair:
{
    "kty": "EC",
    "d": "jZCffzVqJjryBH4EoaN0oD-TyLXrW2XHoDdIuPZnk8c",
    "use": "enc",
    "crv": "P-256",
    "kid": "evEK2thJMsWxBYRivXI8ykUf6n6zizLiLCGH3s58wKs",
    "x": "LOakgGvxWBsWbCPLY6Vq6OuBktIqG8POXFXe7ngQ2oM",
    "y": "voJvS6I-Mc4qqmEA_G2hLQqBck3a3vqaJbmzY7YPUD4",
    "alg": "ECDH-ES+A256KW"
}

Also shows how to decrypt.

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;

    // This requires the Chilkat API to have been previously unlocked.
    // See Global Unlock Sample for sample code.

    // Create the following JSON:
    // {
    //     "kty": "EC",
    //     "d": "jZCffzVqJjryBH4EoaN0oD-TyLXrW2XHoDdIuPZnk8c",
    //     "use": "enc",
    //     "crv": "P-256",
    //     "kid": "evEK2thJMsWxBYRivXI8ykUf6n6zizLiLCGH3s58wKs",
    //     "x": "LOakgGvxWBsWbCPLY6Vq6OuBktIqG8POXFXe7ngQ2oM",
    //     "y": "voJvS6I-Mc4qqmEA_G2hLQqBck3a3vqaJbmzY7YPUD4",
    //     "alg": "ECDH-ES+A256KW"
    // }

    CkJsonObject json = new CkJsonObject();
    json.UpdateString("kty","EC");
    json.UpdateString("d","jZCffzVqJjryBH4EoaN0oD-TyLXrW2XHoDdIuPZnk8c");
    json.UpdateString("use","enc");
    json.UpdateString("crv","P-256");
    json.UpdateString("kid","evEK2thJMsWxBYRivXI8ykUf6n6zizLiLCGH3s58wKs");
    json.UpdateString("x","LOakgGvxWBsWbCPLY6Vq6OuBktIqG8POXFXe7ngQ2oM");
    json.UpdateString("y","voJvS6I-Mc4qqmEA_G2hLQqBck3a3vqaJbmzY7YPUD4");
    json.UpdateString("alg","ECDH-ES+A256KW");

    CkPublicKey pubkey = new CkPublicKey();

    success = pubkey.LoadFromString(json.emit());
    if (success == false) {
        Log.i(TAG, pubkey.lastErrorText());
        return;
        }

    // Build our protected header:

    // 	{
    // 	  "alg": "ECDH-ES+A256KW",
    // 	  "enc": "A256GCM",
    // 	  "exp": 1621957030,
    // 	  "cty": "NJWT",
    // 	  "epk": {
    // 	    "kty": "EC",
    // 	    "x": "QLpJ_LpFx-6yJhsb4OvHwU1khLnviiOwYOvmf5clK7w"
    // 	    "y": "AJh7pJ3zZKDJkm8rbeG69GBooTosXJgSsvNFH0i3Vxnu"
    // 	    "crv": "BP-256"
    // 	  }
    // 	}

    // Use jwt only for getting the current date/time + 3600 seconds.
    CkJwt jwt = new CkJwt();

    CkJsonObject jweProtHdr = new CkJsonObject();
    jweProtHdr.UpdateString("alg","ECDH-ES+A256KW");
    jweProtHdr.UpdateString("enc","A256GCM");
    jweProtHdr.UpdateInt("exp",jwt.GenNumericDate(3600));
    jweProtHdr.UpdateString("cty","NJWT");
    jweProtHdr.UpdateString("epk.kty","EC");
    jweProtHdr.UpdateString("epk.x","LOakgGvxWBsWbCPLY6Vq6OuBktIqG8POXFXe7ngQ2oM");
    jweProtHdr.UpdateString("epk.y","voJvS6I-Mc4qqmEA_G2hLQqBck3a3vqaJbmzY7YPUD4");
    jweProtHdr.UpdateString("epk.crv","P-256");

    CkJwe jwe = new CkJwe();
    jwe.SetProtectedHeader(jweProtHdr);
    jwe.SetPublicKey(0,pubkey);

    String plainText = "This is the text to be encrypted.";
    String strJwe = jwe.encrypt(plainText,"utf-8");
    if (jwe.get_LastMethodSuccess() != true) {
        Log.i(TAG, jwe.lastErrorText());
        return;
        }

    Log.i(TAG, strJwe);

    // Let's decrypt...
    CkPrivateKey privkey = new CkPrivateKey();

    success = privkey.LoadJwk(json.emit());
    if (success == false) {
        Log.i(TAG, privkey.lastErrorText());
        return;
        }

    CkJwe jwe2 = new CkJwe();
    success = jwe2.LoadJwe(strJwe);
    if (success == false) {
        Log.i(TAG, jwe2.lastErrorText());
        return;
        }

    jwe2.SetPrivateKey(0,privkey);

    //  Decrypt.
    String decryptedText = jwe2.decrypt(0,"utf-8");
    if (jwe2.get_LastMethodSuccess() != true) {
        Log.i(TAG, jwe2.lastErrorText());
        return;
        }

    Log.i(TAG, decryptedText);

  }

  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."
  }
}