Jwe C# Reference Documentation

Jwe

Current Version: 11.5.0

Chilkat.Jwe

Create and decrypt JSON Web Encryption payloads in compact or JSON form.

Chilkat.Jwe is the Chilkat class for creating and decrypting JSON Web Encryption objects. It supports text and binary payloads, compact serialization, flattened JSON serialization, general JSON serialization, protected and unprotected headers, Additional Authenticated Data, single or multiple recipients, and several key-management approaches including RSA, AES key wrapping, direct symmetric encryption, AES-GCM key wrapping, and PBES2 password-based key wrapping.

Encrypt text or binary data

Protect string or binary payloads using JWE content-encryption algorithms and the key-management method required by the recipient.

Decrypt JWE messages

Load a compact, flattened, or general JWE object and decrypt the payload using the appropriate private key, symmetric key, or password.

Compact and JSON formats

Produce or consume compact serialization for single-recipient workflows, or JSON serialization when richer header or multi-recipient structure is needed.

Single or multiple recipients

Create JWE data for one recipient or use general JSON serialization when the same encrypted payload must be decryptable by multiple recipients.

Headers and AAD

Configure protected headers, unprotected headers, recipient-specific headers, and Additional Authenticated Data for protocol-specific JWE requirements.

Flexible key management

Use RSA key management, AES key wrapping, direct symmetric keys, AES-GCM key wrapping, or PBES2 password-based key wrapping.

Common pattern: Choose the JWE serialization format, set the protected header values such as alg and enc, add the recipient key information, provide the plaintext payload, and encrypt. To decrypt, load the JWE, provide the matching key or password, and retrieve the plaintext as text, bytes, or a Chilkat binary buffer.

Object Creation

Chilkat.Jwe obj = new Chilkat.Jwe();

Properties

DebugLogFilePath
public string DebugLogFilePath {get; set; }

If set to a file path, this property logs the LastErrorText of each Chilkat method or property call to the specified file. This logging helps identify the context and history of Chilkat calls leading up to any crash or hang, aiding in debugging.

Enabling the VerboseLogging property provides more detailed information. This property is mainly used for debugging rare instances where a Chilkat method call causes a hang or crash, which should generally not happen.

Possible causes of hangs include:

  • A timeout property set to 0, indicating an infinite timeout.
  • A hang occurring within an event callback in the application code.
  • An internal bug in the Chilkat code causing the hang.

top
LastErrorHtml
public string LastErrorHtml {get; }

Provides HTML-formatted information about the last called method or property. If a method call fails or behaves unexpectedly, check this property for details. Note that information is available regardless of the method call's success.

top
LastErrorText
public string LastErrorText {get; }

Provides plain text information about the last called method or property. If a method call fails or behaves unexpectedly, check this property for details. Note that information is available regardless of the method call's success.

top
LastErrorXml
public string LastErrorXml {get; }

Provides XML-formatted information about the last called method or property. If a method call fails or behaves unexpectedly, check this property for details. Note that information is available regardless of the method call's success.

top
LastMethodSuccess
public bool LastMethodSuccess {get; set; }

Indicates the success or failure of the most recent method call: true means success, false means failure. This property remains unchanged by property setters or getters. This method is present to address challenges in checking for null or Nothing returns in certain programming languages. Note: This property does not apply to methods that return integer values or to boolean-returning methods where the boolean does not indicate success or failure.

top
NumRecipients
public int NumRecipients {get; }
Introduced in version 9.5.0.66

Returns the number of recipients represented by the currently loaded or configured JWE. Recipient indexes are zero-based, from 0 through NumRecipients - 1.

Compact Serialization and Flattened JWE JSON Serialization represent one recipient. General JWE JSON Serialization can represent one or more recipients that share the same encrypted payload while using separate recipient key-management information.

top
PreferCompact
public bool PreferCompact {get; set; }
Introduced in version 9.5.0.66

Controls whether encryption prefers JWE Compact Serialization. The default is true. When true, compact form is used whenever the configured JWE can be represented by its five period-separated base64url segments.

JWE JSON Serialization is required instead when the message has multiple recipients, a shared unprotected header, a recipient-specific unprotected header, or external Additional Authenticated Data. Setting this property to false requests JSON serialization even when compact form would otherwise be possible.

Compact tradeoff: Compact Serialization is convenient for HTTP headers and token strings, but JSON Serialization is required for richer header structures, external AAD, and multiple recipients.

top
PreferFlattened
public bool PreferFlattened {get; set; }
Introduced in version 9.5.0.66

Controls which JWE JSON Serialization is preferred when JSON form is used. The default is true.

  • When true and there is exactly one recipient, Chilkat prefers Flattened JWE JSON Serialization.
  • When false, Chilkat may use General JWE JSON Serialization even for one recipient.
  • When multiple recipients exist, General JWE JSON Serialization is required regardless of this setting.

This property has no effect when Compact Serialization is selected.

top
UncommonOptions
public string UncommonOptions {get; set; }
Introduced in version 10.1.0

Provides a catch-all string for specialized compatibility options that are not exposed as ordinary properties. The default is the empty string, which is appropriate for normal JWE creation and decryption.

Leave empty unless instructed: Set this property only when a Chilkat example, release note, or support response documents a specific option required for an unusual interoperability case.

top
VerboseLogging
public bool VerboseLogging {get; set; }

If set to true, then the contents of LastErrorText (or LastErrorXml, or LastErrorHtml) may contain more verbose information. The default value is false. Verbose logging should only be used for debugging. The potentially large quantity of logged information may adversely affect peformance.

top
Version
public string Version {get; }

Version of the component/library, such as "10.1.0"

top

Methods

Decrypt
public string Decrypt(int index, string charset);
Introduced in version 9.5.0.66

Decrypts the previously loaded JWE for recipient index index and returns the plaintext as a string. charset specifies how the authenticated plaintext bytes are decoded into characters, such as utf-8. Use DecryptBd when the plaintext is binary or when no character decoding is desired.

Call LoadJwe or LoadJweSb first, then provide the key material required by the selected recipient's alg value through SetPrivateKey, SetWrappingKey, or SetPassword. For a single-recipient JWE, index is normally 0.

Authenticated decryption: Success means both key management and content authentication succeeded. A wrong key, modified protected header, modified ciphertext, incorrect AAD, or invalid authentication tag causes the method to fail rather than returning unauthenticated plaintext.
Validate allowed algorithms: Loading a JWE does not mean its alg and enc choices are acceptable to the application. Inspect the header and enforce the application's algorithm policy before accepting the plaintext.

Returns null on failure

top
DecryptBd
public bool DecryptBd(int index, BinData bd);
Introduced in version 9.5.0.66

Decrypts the previously loaded JWE for recipient index index and appends the authenticated plaintext bytes to the BinData object in bd. No character-set conversion is performed.

This is the preferred decryption method for arbitrary binary content. For a typical single-recipient JWE, index is 0. Set the corresponding private key, symmetric key, or PBES2 password before calling this method.

Append behavior: Existing bytes in bd are preserved. Clear the destination first when it should contain only the current plaintext.

Returns true for success, false for failure.

top
DecryptSb
public bool DecryptSb(int index, string charset, StringBuilder contentSb);
Introduced in version 9.5.0.66

Decrypts the previously loaded JWE for recipient index index, decodes the authenticated plaintext bytes using the character encoding in charset, and appends the resulting text to contentSb. For a typical single-recipient JWE, index is 0.

Set the required private key, wrapping key, direct key, or password at the same recipient index before calling this method.

Append behavior: Existing text in contentSb is preserved. Use the method's return value before consuming the appended result, and clear the destination first when only the current plaintext is wanted.

Returns true for success, false for failure.

top
Encrypt
public string Encrypt(string content, string charset);
Introduced in version 9.5.0.66

Encrypts the string in content and returns the resulting JWE serialization. charset specifies the character encoding used to convert the string to plaintext bytes, such as utf-8. Use EncryptBd when the plaintext is already binary data.

Before calling this method, set a protected header containing the required alg and enc values, then provide the corresponding recipient key or password. The output format is selected by PreferCompact and PreferFlattened, subject to the structural requirements of the JWE.

Header parameterSupported families
alg — key managementRSA: RSA1_5, RSA-OAEP, RSA-OAEP-256, RSA-OAEP-384, RSA-OAEP-512
ECDH: ECDH-ES, ECDH-ES+A128KW, ECDH-ES+A192KW, ECDH-ES+A256KW
Symmetric: dir, A128KW, A192KW, A256KW, A128GCMKW, A192GCMKW, A256GCMKW
Password: PBES2-HS256+A128KW, PBES2-HS384+A192KW, PBES2-HS512+A256KW
enc — content encryptionA128CBC-HS256, A192CBC-HS384, A256CBC-HS512, A128GCM, A192GCM, A256GCM
Two different algorithm choices: alg determines how the CEK is established or delivered to a recipient. enc determines how the plaintext is encrypted and authenticated with that CEK.
Encryption is not sender authentication: JWE provides confidentiality and authenticated encryption, but it does not by itself prove who created the message. Use an appropriate JWS or nested sign-and-encrypt design when origin authentication is required.

Returns null on failure

top
EncryptBd
public bool EncryptBd(BinData contentBd, StringBuilder jweSb);
Introduced in version 9.5.0.66

Encrypts the exact bytes contained in contentBd and appends the resulting JWE text to jweSb. This method performs no character-set conversion and is therefore appropriate for files, compressed data, serialized objects, or any other binary plaintext.

Configure the protected header and the recipient key-management material before calling this method. The JWE ciphertext is base64url-encoded as required by the selected serialization.

Append behavior: Existing text in jweSb is preserved. Clear the destination first when it should contain only one JWE.

Returns true for success, false for failure.

top
EncryptSb
public bool EncryptSb(StringBuilder contentSb, string charset, StringBuilder jweSb);
Introduced in version 9.5.0.66

Encrypts the text currently contained in contentSb. charset specifies the character encoding used to convert that text to plaintext bytes. The resulting JWE text is appended to jweSb; existing content in jweSb is preserved.

Configure the protected header, recipient keys, optional unprotected headers, and optional Additional Authenticated Data before calling this method. Serialization selection follows PreferCompact and PreferFlattened.

Append behavior: Clear jweSb first when the destination should contain only the newly generated JWE.

Returns true for success, false for failure.

top
FindRecipient
public int FindRecipient(string paramName, string paramValue, bool caseSensitive);
Introduced in version 9.5.0.66

Searches the loaded JWE's recipient header information for a parameter named paramName whose value equals paramValue. Returns the zero-based recipient index of the first match, or -1 when no match is found. caseSensitive controls whether both the parameter-name and parameter-value comparisons are case-sensitive.

The most common lookup parameter is kid, an application-defined key identifier. A typical multi-recipient workflow is:

  1. Load the JWE.
  2. Call this method to locate the recipient whose kid identifies an available key.
  3. Set that key or password at the returned index.
  4. Decrypt using the same index.
A key identifier is only a hint: Header values can be read before authentication and may be attacker-controlled. Use the result to select a candidate key, but accept the message only after authenticated decryption succeeds.
Case sensitivity: JOSE parameter names and identifiers are normally treated as case-sensitive. Set caseSensitive to true unless the surrounding protocol explicitly defines case-insensitive matching.
top
GetHeader
public bool GetHeader(JsonObject json);
Introduced in version 9.5.0.89

Loads the decoded JSON header exposed by the currently loaded JWE into json. Use this method for general header inspection. Use GetProtectedHeader when the application specifically needs the shared protected-header members.

A JWE header can contain values such as alg, enc, kid, cty, zip, and algorithm-specific parameters. Header values are visible without decrypting the ciphertext.

Inspect before use, trust after verification: A decoded header is not automatically trustworthy. The authenticity of protected members is established only when JWE decryption and tag verification succeed; unprotected members are never integrity-protected.

Returns true for success, false for failure.

top
GetProtectedHeader
public bool GetProtectedHeader(JsonObject json);
Introduced in version 9.5.0.89

Loads the decoded shared JWE Protected Header into json. This is the JSON object that was base64url-encoded into the compact serialization's first segment or stored in the JSON serialization's protected member.

The protected header commonly contains alg and enc, along with other security-sensitive parameters. It is authenticated as part of the JWE but is not encrypted.

Authentication occurs during decryption: The protected header can be decoded before the key is supplied, but its integrity is not established until authenticated decryption succeeds.

Returns true for success, false for failure.

top
LoadJwe
public bool LoadJwe(string jwe);
Introduced in version 9.5.0.66

Parses the JWE serialization in jwe and loads it into this object. Compact Serialization, Flattened JWE JSON Serialization, and General JWE JSON Serialization are accepted.

Loading separates and decodes the JWE structure so that properties and methods such as NumRecipients, FindRecipient, GetHeader, and GetProtectedHeader can be used. It does not decrypt the ciphertext and does not require a key.

Parsing is not authentication: Header values are untrusted until decryption and authentication succeed. Enforce the application's allowed alg and enc values before accepting decrypted content.

Returns true for success, false for failure.

top
LoadJweSb
public bool LoadJweSb(StringBuilder sb);
Introduced in version 9.5.0.66

Parses and loads a JWE from the complete text contained in sb. It accepts compact, flattened JSON, or general JSON serialization and otherwise behaves the same as LoadJwe.

Loading prepares the object for header inspection, recipient selection, and decryption; it does not decrypt or authenticate the message.

Returns true for success, false for failure.

top
SetAad
public bool SetAad(string aad, string charset);
Introduced in version 9.5.0.66

Sets optional external Additional Authenticated Data (AAD) from the string in aad. charset specifies the character encoding used to convert the string to bytes.

AAD is not encrypted and is not part of the plaintext, but it is covered by the JWE authentication tag. In JWE JSON Serialization, its base64url representation is carried in the aad member. Any change to those bytes causes decryption to fail. External AAD cannot be represented in Compact Serialization, so setting it requires JSON Serialization.

Typical use: AAD binds protocol metadata to the ciphertext without placing that metadata inside the encrypted payload.

Returns true for success, false for failure.

top
SetAadBd
public bool SetAadBd(BinData aad);
Introduced in version 9.5.0.66

Sets optional external Additional Authenticated Data (AAD) to the exact bytes contained in aad. This method is the binary equivalent of SetAad and performs no character-set conversion.

The AAD is transmitted without encryption in the JSON serialization's aad member, but it is authenticated together with the protected header and ciphertext. External AAD is available only in JWE JSON Serialization and therefore prevents Compact Serialization.

Exact-byte requirement: Decryption succeeds only when the authenticated AAD bytes are reproduced exactly. Even a one-byte difference causes authentication failure.

Returns true for success, false for failure.

top
SetPassword
public bool SetPassword(int index, string password);
Introduced in version 9.5.0.66

Sets the password used for PBES2 key management for the recipient at zero-based index index. Use this method when the JWE alg value is one of the following:

  • PBES2-HS256+A128KW
  • PBES2-HS384+A192KW
  • PBES2-HS512+A256KW

PBES2 derives a key-encryption key from the password and then uses AES Key Wrap to protect the content-encryption key. For encryption, the protected header must also contain p2s, a base64url-encoded random salt input, and p2c, the iteration count. For decryption, those values are read from the loaded JWE.

Password strength matters: PBES2 slows password guessing but does not make a weak password strong. Use a high-entropy password, a fresh salt, and an iteration count appropriate for the application's security and performance requirements.

Returns true for success, false for failure.

top
SetPrivateKey
public bool SetPrivateKey(int index, PrivateKey privKey);
Introduced in version 9.5.0.66

Sets the private key used to decrypt for the recipient at zero-based index index. The required key type is determined by the recipient's alg header parameter.

Key typeSupported alg valuesPurpose
RSA private keyRSA1_5, RSA-OAEP, RSA-OAEP-256, RSA-OAEP-384, RSA-OAEP-512Decrypts or unwraps the content-encryption key (CEK).
EC private keyECDH-ES, ECDH-ES+A128KW, ECDH-ES+A192KW, ECDH-ES+A256KWPerforms ECDH-ES key agreement and, for the +A*KW forms, unwraps the CEK.

For a typical single-recipient JWE, index is 0. For a multi-recipient JWE, set the private key at the same index that will be passed to Decrypt, DecryptSb, or DecryptBd.

Legacy RSA mode: RSA1_5 uses RSAES-PKCS1-v1_5 encryption and should be avoided for new designs. Prefer an RSA-OAEP or ECDH-ES algorithm when interoperability permits.

Returns true for success, false for failure.

top
SetProtectedHeader
public bool SetProtectedHeader(JsonObject json);
Introduced in version 9.5.0.66

Sets the shared JWE Protected Header from the JSON object in json. The protected header is serialized as UTF-8 JSON, base64url-encoded, and included in the authenticated data used by the content-encryption algorithm. It is visible to recipients but cannot be changed without causing authentication to fail.

For encryption, the protected header normally contains at least alg, which selects the key-management algorithm, and enc, which selects the authenticated content-encryption algorithm. Other commonly protected parameters include kid, cty, typ, and zip.

Protect security-sensitive parameters: Place alg, enc, zip, and any parameter used to make a security decision in the protected header. Unprotected header members are transmitted without integrity protection.
Header names must be disjoint: A parameter name must not appear in more than one of the protected header, shared unprotected header, and recipient-specific header for the same recipient.

Returns true for success, false for failure.

top
SetPublicKey
public bool SetPublicKey(int index, PublicKey pubKey);
Introduced in version 9.5.0.66

Sets the recipient public key used for encryption at zero-based index index. The required key type is determined by the alg parameter in the JWE header.

Key typeSupported alg valuesPurpose
RSA public keyRSA1_5, RSA-OAEP, RSA-OAEP-256, RSA-OAEP-384, RSA-OAEP-512Encrypts or wraps the generated content-encryption key (CEK).
EC public keyECDH-ES, ECDH-ES+A128KW, ECDH-ES+A192KW, ECDH-ES+A256KWUses the recipient's public key for ECDH-ES key agreement.

For a typical single-recipient JWE, index is 0. For general JSON serialization, call this method for each recipient index with that recipient's public key.

Legacy RSA mode: Avoid RSA1_5 for new applications. RSA-OAEP and ECDH-ES avoid the known weaknesses of RSAES-PKCS1-v1_5 encryption.

Returns true for success, false for failure.

top
SetRecipientHeader
public bool SetRecipientHeader(int index, JsonObject json);
Introduced in version 9.5.0.66

Sets the unprotected header for the recipient at zero-based index index. Recipient index 0 is the first recipient. A recipient-specific header is represented by the header member of a flattened or general JWE JSON Serialization and cannot be represented in Compact Serialization.

This header is commonly used for recipient-specific routing metadata such as kid. It can be used for a single-recipient flattened JWE or for each recipient in a multi-recipient general JWE.

Unprotected metadata: Recipient-header members are not authenticated. Use them to locate a candidate key, but rely on successful JWE decryption and authentication before accepting the message.
No duplicate parameter names: A member name used in this header must not also appear in the shared protected or shared unprotected header for the same recipient.

Returns true for success, false for failure.

top
SetUnprotectedHeader
public bool SetUnprotectedHeader(JsonObject json);
Introduced in version 9.5.0.66

Sets the JWE Shared Unprotected Header from json. Its members apply to every recipient and are emitted only in JWE JSON Serialization. Compact Serialization has no shared unprotected-header member.

The shared unprotected header is neither encrypted nor integrity-protected. It is appropriate only for non-security-critical metadata that must remain outside the protected header.

Do not trust unprotected values: An attacker can alter these members without invalidating the JWE authentication tag. Do not place alg, enc, or other security-sensitive values here.
No duplicate parameter names: A member name used here must not also appear in the protected header or in a recipient-specific header.

Returns true for success, false for failure.

top
SetWrappingKey
public bool SetWrappingKey(int index, string encodedKey, string encoding);
Introduced in version 9.5.0.66

Sets the symmetric key used by the recipient at zero-based index index. encodedKey contains the encoded key, and encoding names its representation, such as hex, base64, or base64url.

algRequired key sizeHow the supplied key is used
A128KW, A128GCMKW128 bits (16 bytes)Key-encryption key used to wrap the CEK.
A192KW, A192GCMKW192 bits (24 bytes)Key-encryption key used to wrap the CEK.
A256KW, A256GCMKW256 bits (32 bytes)Key-encryption key used to wrap the CEK.
dirMust match the CEK size required by encThe supplied key is used directly as the CEK; no encrypted-key value is produced.

For dir, examples include 128, 192, or 256 bits for A128GCM, A192GCM, or A256GCM, and 256, 384, or 512 bits for A128CBC-HS256, A192CBC-HS384, or A256CBC-HS512.

Use random key material: This method expects a cryptographic key, not a human-readable password. Use SetPassword for PBES2 password-based key management.

Returns true for success, false for failure.

top