Sample code for 30+ languages & platforms
Java

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 Java Downloads

Chilkat2 Java
import com.chilkatsoft.chilkat2.ChilkatException;
import com.chilkatsoft.chilkat2.JsonObject;
import com.chilkatsoft.chilkat2.Jwe;

public class ChilkatExample {

    public static void main(String[] args) {
        Jwe jwe = new Jwe();

        // Build the JWE protected header.  The "alg" member names the key-management algorithm and the "enc"
        // member names the content-encryption algorithm.
        JsonObject header = new JsonObject();
        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.
        try {
            jwe.setProtectedHeader(header);
        } catch (ChilkatException e) {
            System.out.println(e.getLastErrorText());
            return;
        }

        System.out.println("Protected header set.");
    }
}