Sample code for 30+ languages & platforms
Zig

JWE using PBES2 Key Wrapping

See more JSON Web Encryption (JWE) Examples

Demonstrates how to create and decrypt a JWE that using PBES2 key wrapping.

This example demonstrates PBES2 with HMAC SHA-256 and A128KW wrapping. It is also possible to do the following by simply changing the "alg" parameter:

  • PBES2 with HMAC SHA-384 and A192KW wrapping
  • PBES2 with HMAC SHA-512 and A256KW wrapping

Note: This example requires Chilkat v9.5.0.66 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.

    // Note: This example requires Chilkat v9.5.0.66 or greater.

    const plaintext = "Live long and prosper.";

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

    // First build the JWE Protected Header..
    const jwe_prot_hdr = try chilkat.JsonObject.init();
    defer jwe_prot_hdr.deinit();
    jwe_prot_hdr.appendString("alg", "PBES2-HS256+A128KW") catch {};
    jwe_prot_hdr.appendString("enc", "A128GCM") catch {};

    // PBES2 requires two additional parameters:
    // 1) A random salt parameter ("p2s") containing 8 or more bytes in base64url format.
    // 2) An iteration count parameter ("p2c").  A minimum count of 1000 is recommended.
    //    The iteration count is intended to make the PBES2 computation more expensive (time consuming)
    //    to prevent brute-force attacks.
    const prng = try chilkat.Prng.init();
    defer prng.deinit();
    jwe_prot_hdr.appendString("p2s", try prng.genRandom(alloc, 16, "base64url")) catch {};
    jwe_prot_hdr.appendString("p2c", "1000") catch {};

    std.debug.print("JWE Protected Header: {s}\n", .{try jwe_prot_hdr.emit(alloc)});
    std.debug.print("--\n", .{});

    // Don't forget to actually provide the protected header to the JWE object:
    jwe.setProtectedHeader(jwe_prot_hdr) catch {};

    // Set the PBES2 password
    const recipient_index = 0;
    jwe.setPassword(recipient_index, "top secret") catch {};

    // Encrypt and return the JWE:
    const str_jwe = jwe.encrypt(alloc, plaintext, "utf-8") catch {
        std.debug.print("{s}\n", .{try jwe.getLastErrorText(alloc)});
        return;
    };

    // Show the JWE we just created:
    std.debug.print("{s}\n", .{str_jwe});

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

    // Set the PBES2 password
    jwe2.setPassword(recipient_index, "top secret") catch {};

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

    std.debug.print("original text: \n", .{});
    std.debug.print("{s}\n", .{original_plaintext});

    // Sample output:

    // JWE Protected Header: {"alg":"PBES2-HS256+A128KW","enc":"A128GCM","p2s":"z39rTEfRy1T1Yn_D1mZRlg","p2c":"1000"}
    // --
    // eyJhbGciOiJQQkVTMi1IUzI1NitBMTI4S1ciLCJlbmMiOiJBMTI4R0NNIiwicDJzIjoiejM5clRFZlJ5MVQxWW5fRDFtWlJsZyIsInAyYyI6IjEwMDAifQ.koYt6PrFmYwcwdcT7ZcvXHA1d-Xez5h4.luGlbvEnZp-7IsBOj42Yhw.YMTcfLf8Qe4zazozGV2OAu3cUdQ8Kg.rWub47ESWkc6IqZJTvSTmg
    // original text:
    // Live long and prosper.
}