JWE API

jose.jwe.decrypt(jwe_str, key)

Decrypts a JWE compact serialized string and returns the plaintext.

Parameters:
  • jwe_str (str) – A JWE to be decrypt.

  • key (str or dict) – A key to attempt to decrypt the payload with. Can be individual JWK or JWK set.

Returns:

The plaintext bytes, assuming the authentication tag is valid.

Return type:

bytes

Raises:

JWEError – If there is an exception verifying the token.

Examples

>>> from jose import jwe
>>> jwe.decrypt(jwe_string, 'asecret128bitkey')
'Hello, World!'
jose.jwe.encrypt(plaintext, key, encryption='A256GCM', algorithm='dir', zip=None, cty=None, kid=None)

Encrypts plaintext and returns a JWE cmpact serialization string.

Parameters:
  • plaintext (bytes) – A bytes object to encrypt

  • key (str or dict) – The key(s) to use for encrypting the content. Can be individual JWK or JWK set.

  • encryption (str, optional) – The content encryption algorithm used to perform authenticated encryption on the plaintext to produce the ciphertext and the Authentication Tag. Defaults to A256GCM.

  • algorithm (str, optional) – The cryptographic algorithm used to encrypt or determine the value of the CEK. Defaults to dir.

  • zip (str, optional) – The compression algorithm) applied to the plaintext before encryption. Defaults to None.

  • cty (str, optional) – The media type for the secured content. See http://www.iana.org/assignments/media-types/media-types.xhtml

  • kid (str, optional) – Key ID for the provided key

Returns:

The string representation of the header, encrypted key,

initialization vector, ciphertext, and authentication tag.

Return type:

bytes

Raises:

JWEError – If there is an error signing the token.

Examples

>>> from jose import jwe
>>> jwe.encrypt('Hello, World!', 'asecret128bitkey', algorithm='dir', encryption='A128GCM')
'eyJhbGciOiJkaXIiLCJlbmMiOiJBMTI4R0NNIn0..McILMB3dYsNJSuhcDzQshA.OfX9H_mcUpHDeRM4IA.CcnTWqaqxNsjT4eCaUABSg'
jose.jwe.get_unverified_header(jwe_str)

Returns the decoded headers without verification of any kind.

Parameters:

jwe_str (str) – A compact serialized JWE to decode the headers from.

Returns:

The dict representation of the JWE headers.

Return type:

dict

Raises:

JWEError – If there is an exception decoding the JWE.