Sample code for 30+ languages & platforms
Android™

Set the JWE Protected Header

See more JSON Web Encryption (JWE) Examples

Demonstrates Jwe.SetProtectedHeader, which sets the integrity-protected JWE header from a JsonObject. The header specifies the key-management algorithm (alg) and content-encryption algorithm (enc).

Background. The protected header is integrity-protected and applies to all recipients. It is configured before encrypting.

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;

    CkJwe jwe = new CkJwe();

    //  Build the JWE protected header.  The "alg" member names the key-management algorithm and the "enc"
    //  member names the content-encryption algorithm.
    CkJsonObject header = new CkJsonObject();
    header.UpdateString("alg","A256KW");
    header.UpdateString("enc","A256GCM");

    //  Set the protected header on the JWE.  The protected header is integrity-protected and applies to
    //  all recipients.
    success = jwe.SetProtectedHeader(header);
    if (success == false) {
        Log.i(TAG, jwe.lastErrorText());
        return;
        }

    Log.i(TAG, "Protected header set.");

  }

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