Sample code for 30+ languages & platforms
Zig

JWE using ECDH-ES, BP-256, A256GCM

See more JSON Web Encryption (JWE) Examples

Create a JWE with the following header:
	{
	  "alg": "ECDH-ES",
	  "enc": "A256GCM",
	  "exp": 1621957030,
	  "cty": "NJWT",
	  "epk": {
	    "kty": "EC",
	    "x": "QLpJ_LpFx-6yJhsb4OvHwU1khLnviiOwYOvmf5clK7w"
	    "y": "AJh7pJ3zZKDJkm8rbeG69GBooTosXJgSsvNFH0i3Vxnu"
	    "crv": "BP-256"
	  }
	}

Note: This example requires Chilkat v9.5.0.87 or greater.

Chilkat Zig Downloads

Zig
const std = @import("std");
const chilkat = @import("chilkat");

pub fn main(init: std.process.Init) !void {
    const alloc = init.arena.allocator();

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

    // Load our brainpool BP-256 public key.

    //     {
    //       "use": "enc",
    //       "kid": "puk_idp_enc",
    //       "kty": "EC",
    //       "crv": "BP-256",
    //       "x": "QLpJ_LpFx-6yJhsb4OvHwU1khLnviiOwYOvmf5clK7w",
    //       "y": "AJh7pJ3zZKDJkm8rbeG69GBooTosXJgSsvNFH0i3Vxnu"
    //     }

    const json = try chilkat.JsonObject.init();
    defer json.deinit();
    json.updateString("use", "enc") catch {};
    json.updateString("kid", "puk_idp_enc") catch {};
    json.updateString("kty", "EC") catch {};
    json.updateString("crv", "BP-256") catch {};
    json.updateString("x", "QLpJ_LpFx-6yJhsb4OvHwU1khLnviiOwYOvmf5clK7w") catch {};
    json.updateString("y", "AJh7pJ3zZKDJkm8rbeG69GBooTosXJgSsvNFH0i3Vxnu") catch {};

    const pubkey = try chilkat.PublicKey.init();
    defer pubkey.deinit();

    pubkey.loadFromString(try json.emit(alloc)) catch {
        std.debug.print("{s}\n", .{try pubkey.getLastErrorText(alloc)});
        return;
    };

    // Build our protected header:

    //     {
    //       "alg": "ECDH-ES",
    //       "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.
    const jwt = try chilkat.Jwt.init();
    defer jwt.deinit();

    const jwe_prot_hdr = try chilkat.JsonObject.init();
    defer jwe_prot_hdr.deinit();
    jwe_prot_hdr.updateString("alg", "ECDH-ES") catch {};
    jwe_prot_hdr.updateString("enc", "A256GCM") catch {};
    jwe_prot_hdr.updateInt("exp", jwt.genNumericDate(3600)) catch {};
    jwe_prot_hdr.updateString("cty", "NJWT") catch {};
    jwe_prot_hdr.updateString("epk.kty", "EC") catch {};
    jwe_prot_hdr.updateString("epk.x", "QLpJ_LpFx-6yJhsb4OvHwU1khLnviiOwYOvmf5clK7w") catch {};
    jwe_prot_hdr.updateString("epk.y", "AJh7pJ3zZKDJkm8rbeG69GBooTosXJgSsvNFH0i3Vxnu") catch {};
    jwe_prot_hdr.updateString("epk.crv", "BP-256") catch {};

    const jwe = try chilkat.Jwe.init();
    defer jwe.deinit();
    jwe.setProtectedHeader(jwe_prot_hdr) catch {};
    jwe.setPublicKey(0, pubkey) catch {};

    const plain_text = "This is the text to be encrypted.";
    const str_jwe = jwe.encrypt(alloc, plain_text, "utf-8") catch {
        std.debug.print("{s}\n", .{try jwe.getLastErrorText(alloc)});
        return;
    };

    std.debug.print("{s}\n", .{str_jwe});

    std.debug.print("Success.\n", .{});
}