Jwe C# Reference Documentation
Jwe
Current Version: 11.5.0
Chilkat.Jwe
Protect string or binary payloads using JWE content-encryption algorithms
and the key-management method required by the recipient.
Load a compact, flattened, or general JWE object and decrypt the payload
using the appropriate private key, symmetric key, or password.
Produce or consume compact serialization for single-recipient workflows,
or JSON serialization when richer header or multi-recipient structure is
needed.
Create JWE data for one recipient or use general JSON serialization when
the same encrypted payload must be decryptable by multiple recipients.
Configure protected headers, unprotected headers, recipient-specific
headers, and Additional Authenticated Data for protocol-specific JWE
requirements.
Use RSA key management, AES key wrapping, direct symmetric keys,
AES-GCM key wrapping, or PBES2 password-based key wrapping.
For an extended overview, see
Jwe Class Overview.
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
Decrypt JWE messages
Compact and JSON formats
Single or multiple recipients
Headers and AAD
Flexible key management
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
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.
LastErrorHtml
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.
topLastErrorText
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.
LastErrorXml
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.
topLastMethodSuccess
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.
NumRecipients
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.
topPreferCompact
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.
PreferFlattened
Controls which JWE JSON Serialization is preferred when JSON form is used. The default is true.
- When
trueand 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.
UncommonOptions
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.
VerboseLogging
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.
Version
Methods
Decrypt
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.
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
DecryptBd
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.
bd are preserved. Clear the destination first when it should contain only the current plaintext.Returns true for success, false for failure.
DecryptSb
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.
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.
Encrypt
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 parameter | Supported families |
|---|---|
alg — key management | RSA: RSA1_5, RSA-OAEP, RSA-OAEP-256, RSA-OAEP-384, RSA-OAEP-512ECDH: ECDH-ES, ECDH-ES+A128KW, ECDH-ES+A192KW, ECDH-ES+A256KWSymmetric: dir, A128KW, A192KW, A256KW, A128GCMKW, A192GCMKW, A256GCMKWPassword: PBES2-HS256+A128KW, PBES2-HS384+A192KW, PBES2-HS512+A256KW |
enc — content encryption | A128CBC-HS256, A192CBC-HS384, A256CBC-HS512, A128GCM, A192GCM, A256GCM |
alg determines how the CEK is established or delivered to a recipient. enc determines how the plaintext is encrypted and authenticated with that CEK.Returns null on failure
EncryptBd
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.
jweSb is preserved. Clear the destination first when it should contain only one JWE.Returns true for success, false for failure.
EncryptSb
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.
jweSb first when the destination should contain only the newly generated JWE.Returns true for success, false for failure.
FindRecipient
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:
- Load the JWE.
- Call this method to locate the recipient whose
kididentifies an available key. - Set that key or password at the returned index.
- Decrypt using the same index.
caseSensitive to true unless the surrounding protocol explicitly defines case-insensitive matching.GetHeader
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.
Returns true for success, false for failure.
topGetProtectedHeader
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.
Returns true for success, false for failure.
topLoadJwe
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.
alg and enc values before accepting decrypted content.Returns true for success, false for failure.
LoadJweSb
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.
SetAad
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.
Returns true for success, false for failure.
topSetAadBd
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.
Returns true for success, false for failure.
topSetPassword
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+A128KWPBES2-HS384+A192KWPBES2-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.
Returns true for success, false for failure.
topSetPrivateKey
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 type | Supported alg values | Purpose |
|---|---|---|
| RSA private key | RSA1_5, RSA-OAEP, RSA-OAEP-256, RSA-OAEP-384, RSA-OAEP-512 | Decrypts or unwraps the content-encryption key (CEK). |
| EC private key | ECDH-ES, ECDH-ES+A128KW, ECDH-ES+A192KW, ECDH-ES+A256KW | Performs 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.
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.
SetProtectedHeader
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.
alg, enc, zip, and any parameter used to make a security decision in the protected header. Unprotected header members are transmitted without integrity protection.Returns true for success, false for failure.
SetPublicKey
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 type | Supported alg values | Purpose |
|---|---|---|
| RSA public key | RSA1_5, RSA-OAEP, RSA-OAEP-256, RSA-OAEP-384, RSA-OAEP-512 | Encrypts or wraps the generated content-encryption key (CEK). |
| EC public key | ECDH-ES, ECDH-ES+A128KW, ECDH-ES+A192KW, ECDH-ES+A256KW | Uses 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.
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.
SetRecipientHeader
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.
Returns true for success, false for failure.
topSetUnprotectedHeader
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.
alg, enc, or other security-sensitive values here.Returns true for success, false for failure.
topSetWrappingKey
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.
alg | Required key size | How the supplied key is used |
|---|---|---|
A128KW, A128GCMKW | 128 bits (16 bytes) | Key-encryption key used to wrap the CEK. |
A192KW, A192GCMKW | 192 bits (24 bytes) | Key-encryption key used to wrap the CEK. |
A256KW, A256GCMKW | 256 bits (32 bytes) | Key-encryption key used to wrap the CEK. |
dir | Must match the CEK size required by enc | The 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.
SetPassword for PBES2 password-based key management.Returns true for success, false for failure.