Sample code for 30+ languages & platforms
Zig

Decrypt a JWE to a String

See more JSON Web Encryption (JWE) Examples

Demonstrates Jwe.Decrypt, which decrypts a recipient of a loaded JWE and returns the plaintext, decoded using the given charset. Before decrypting, the example inspects the JWE protected header and, under application policy, accepts only permitted algorithms and selects an acceptable recipient.

Background. A received JWE should never be trusted to specify safe algorithms. The example reads the alg and enc values from the protected header (via GetProtectedHeader) and enforces an allowlist, rejecting anything not permitted. It then selects the recipient the application holds a key for — using FindRecipient to locate a recipient by a per-recipient header value such as kid, falling back to index 0 for a single-recipient compact JWE — and only then supplies the key and decrypts.

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();

    const jwe = try chilkat.Jwe.init();
    defer jwe.deinit();

    // Load the JWE compact serialization to be decrypted.
    const jwe_compact = "eyJhbGciOiJBMjU2S1ciLCJlbmMiOiJBMjU2R0NNIn0.<...>.<iv>.<ciphertext>.<tag>";
    jwe.loadJwe(jwe_compact) catch {
        std.debug.print("{s}\n", .{try jwe.getLastErrorText(alloc)});
        return;
    };

    // Application policy: before decrypting, inspect the JWE header and accept only algorithms the
    // application trusts.  Never assume the algorithms in a received JWE are safe -- a sender could
    // specify a weak or unexpected algorithm.
    const prot_header = try chilkat.JsonObject.init();
    defer prot_header.deinit();
    jwe.getProtectedHeader(prot_header) catch {
        std.debug.print("{s}\n", .{try jwe.getLastErrorText(alloc)});
        return;
    };

    // Read the alg (key-management) and enc (content-encryption) header values into StringBuilder
    // objects so they can be compared.
    const sb_alg = try chilkat.StringBuilder.init();
    defer sb_alg.deinit();
    prot_header.stringOfSb("alg", sb_alg) catch {
        std.debug.print("{s}\n", .{try prot_header.getLastErrorText(alloc)});
        return;
    };

    const sb_enc = try chilkat.StringBuilder.init();
    defer sb_enc.deinit();
    prot_header.stringOfSb("enc", sb_enc) catch {
        std.debug.print("{s}\n", .{try prot_header.getLastErrorText(alloc)});
        return;
    };

    std.debug.print("JWE algorithms: alg={s} enc={s}\n", .{ try sb_alg.getAsString(alloc), try sb_enc.getAsString(alloc) });

    // Enforce an allowlist of acceptable algorithms.  Reject anything not permitted by policy.  String
    // values are compared using StringBuilder.ContentsEqual.
    const b_case_sensitive = true;
    if (!sb_alg.contentsEqual("A256KW", b_case_sensitive)) {
        std.debug.print("Rejecting JWE: key-management algorithm is not permitted by policy.\n", .{});
        return;
    }

    if (!sb_enc.contentsEqual("A256GCM", b_case_sensitive)) {
        std.debug.print("Rejecting JWE: content-encryption algorithm is not permitted by policy.\n", .{});
        return;
    }

    // Select an acceptable recipient.  For a multi-recipient JWE, FindRecipient locates the recipient
    // this application holds a key for by a per-recipient header value such as "kid".  A compact JWE
    // has a single recipient at index 0, so FindRecipient returns -1; fall back to index 0 in that case.
    var recipient_index: i32 = jwe.findRecipient("kid", "recipient-key-1", b_case_sensitive);
    if (recipient_index < 0) {
        recipient_index = 0;
    }

    // Provide the key for the selected recipient.  In production, obtain the key from a secure source
    // rather than hard-coding it.
    const base64_key = "YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4eXowMTIzNDU=";
    jwe.setWrappingKey(recipient_index, base64_key, "base64") catch {
        std.debug.print("{s}\n", .{try jwe.getLastErrorText(alloc)});
        return;
    };

    // Decrypt the selected recipient and return the plaintext, decoded using the given charset.
    const content = jwe.decrypt(alloc, recipient_index, "utf-8") catch {
        std.debug.print("{s}\n", .{try jwe.getLastErrorText(alloc)});
        return;
    };

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