Sample code for 30+ languages & platforms
Zig

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

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

    const json = try chilkat.JsonObject.init();
    defer json.deinit();
    json.updateString("kty", "EC") catch {};
    json.updateString("d", "jZCffzVqJjryBH4EoaN0oD-TyLXrW2XHoDdIuPZnk8c") catch {};
    json.updateString("use", "enc") catch {};
    json.updateString("crv", "P-256") catch {};
    json.updateString("kid", "evEK2thJMsWxBYRivXI8ykUf6n6zizLiLCGH3s58wKs") catch {};
    json.updateString("x", "LOakgGvxWBsWbCPLY6Vq6OuBktIqG8POXFXe7ngQ2oM") catch {};
    json.updateString("y", "voJvS6I-Mc4qqmEA_G2hLQqBck3a3vqaJbmzY7YPUD4") catch {};
    json.updateString("alg", "ECDH-ES+A256KW") 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+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.
    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+A256KW") 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", "LOakgGvxWBsWbCPLY6Vq6OuBktIqG8POXFXe7ngQ2oM") catch {};
    jwe_prot_hdr.updateString("epk.y", "voJvS6I-Mc4qqmEA_G2hLQqBck3a3vqaJbmzY7YPUD4") catch {};
    jwe_prot_hdr.updateString("epk.crv", "P-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});

    // Let's decrypt...
    const privkey = try chilkat.PrivateKey.init();
    defer privkey.deinit();

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

    const jwe2 = try chilkat.Jwe.init();
    defer jwe2.deinit();
    jwe2.loadJwe(str_jwe) catch {
        std.debug.print("{s}\n", .{try jwe2.getLastErrorText(alloc)});
        return;
    };

    jwe2.setPrivateKey(0, privkey) catch {};

    //  Decrypt.
    const decrypted_text = jwe2.decrypt(alloc, 0, "utf-8") catch {
        std.debug.print("{s}\n", .{try jwe2.getLastErrorText(alloc)});
        return;
    };

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