PHPackages                             silencenjoyer/jwe - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Security](/categories/security)
4. /
5. silencenjoyer/jwe

ActiveLibrary[Security](/categories/security)

silencenjoyer/jwe
=================

PHP JWE encryptor.

0.0.1(2mo ago)13MITPHPPHP ^7.4.0|^8.0.0CI passing

Since Apr 27Pushed 2mo agoCompare

[ Source](https://github.com/silencenjoyer/jwe)[ Packagist](https://packagist.org/packages/silencenjoyer/jwe)[ RSS](/packages/silencenjoyer-jwe/feed)WikiDiscussions main Synced 3w ago

READMEChangelogDependencies (3)Versions (2)Used By (0)

🔐 silencenjoyer/jwe
===================

[](#-silencenjoyerjwe)

[![Tests](https://github.com/silencenjoyer/jwe/actions/workflows/tests.yml/badge.svg)](https://github.com/silencenjoyer/jwe/actions/workflows/tests.yml)[![codecov](https://camo.githubusercontent.com/7afc53ff954827eab38f1d5974eb9047e8b470002532d5036f42ab8ebb3c2036/68747470733a2f2f636f6465636f762e696f2f67682f73696c656e63656e6a6f7965722f6a77652f6272616e63682f6d61696e2f67726170682f62616467652e737667)](https://codecov.io/gh/silencenjoyer/jwe)[![Static Analyze](https://github.com/silencenjoyer/jwe/actions/workflows/phpstan.yml/badge.svg)](https://github.com/silencenjoyer/jwe/actions/workflows/phpunit.yml)[![Latest Stable Version](https://camo.githubusercontent.com/9e9664ca02825937fc7d40645490ec648603deb4742676931bd8b6084b4a3c7c/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f73696c656e63656e6a6f7965722f6a77652e737667)](https://packagist.org/packages/silencenjoyer/jwe)[![PHP Version Require](https://camo.githubusercontent.com/a961b7d3b1c58d6231fe36bce8cd95fa2dff706991324b164fdd6d0ec924d3f8/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f7068702d762f73696c656e63656e6a6f7965722f6a77652e737667)](https://packagist.org/packages/silencenjoyer/jwe)[![License](https://camo.githubusercontent.com/4c8103310d2e18452fc339d5b6c863bfa597bce5ac920391a48a65b4e9c242d1/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c6963656e73652f73696c656e63656e6a6f7965722f6a7765)](LICENSE)

A PHP library for JSON Web Encryption (JWE) as defined in [RFC 7516](https://www.rfc-editor.org/rfc/rfc7516).

Supports symmetric shared-key and asymmetric RSA end-to-end encryption, multiple content encryption algorithms, and both JWE Compact and JSON serialization formats.

Requirements
------------

[](#requirements)

- PHP 7.4 or 8.0+
- `ext-openssl`
- `ext-json`

Installation
------------

[](#installation)

```
composer require silencenjoyer/jwe
```

Supported algorithms
--------------------

[](#supported-algorithms)

RoleIdentifierClassDescriptionKey encapsulation`dir``DirectKeyWrapper` / `DirectKeyUnwrapper`Shared symmetric key used directly as CEKKey encapsulation`RSA-OAEP``RsaKeyWrapper` / `RsaKeyUnwrapper`CEK encrypted with recipient's RSA public keyContent encryption`A256GCM``Aes256GcmFactory`AES-256 in GCM mode (32-byte CEK)Content encryption`A256CBC-HS512``Aes256CbcFactory`AES-256 in CBC mode with HMAC-SHA-512 (64-byte CEK)Usage
-----

[](#usage)

### Symmetric encryption (shared secret)

[](#symmetric-encryption-shared-secret)

Both parties share the same secret key. The key is used directly as the Content Encryption Key (CEK), so `encrypted_key` in the JWE token is empty.

```
use Silencenjoyer\Jwe\Ciphers\Factory\Aes256GcmFactory;
use Silencenjoyer\Jwe\Decryptors\Decryptor;
use Silencenjoyer\Jwe\Encryptors\Encryptor;
use Silencenjoyer\Jwe\KeyEncapsulation\DirectKeyUnwrapper;
use Silencenjoyer\Jwe\KeyEncapsulation\DirectKeyWrapper;
use Silencenjoyer\Jwe\Keys\Key;
use Silencenjoyer\Jwe\Serializers\CompactSerializer;

// A 32-byte shared secret for A256GCM (use 64 bytes for A256CBC-HS512)
$sharedKey = Key::fromContent('a-32-byte-secret-key-goes-here!!');

$encryptor = new Encryptor(
    new DirectKeyWrapper($sharedKey),
    new Aes256GcmFactory(),
);

$decryptor = new Decryptor(
    new DirectKeyUnwrapper($sharedKey),
    new Aes256GcmFactory(),
);

$serializer = new CompactSerializer();

// Encrypt
$token     = $encryptor->encrypt('Hello, World!');
$compactJwe = $serializer->serialize($token);
// eyJhbGciOiJkaXIiLCJlbmMiOiJBMjU2R0NNIn0.....

// Decrypt
$plaintext = $decryptor->decrypt($serializer->unserialize($compactJwe));
// "Hello, World!"
```

---

### RSA end-to-end encryption

[](#rsa-end-to-end-encryption)

The sender encrypts a random CEK with the recipient's **public** RSA key. Only the holder of the matching **private** key can recover the CEK and decrypt the content. No shared secret is required.

```
use Silencenjoyer\Jwe\Ciphers\Factory\Aes256GcmFactory;
use Silencenjoyer\Jwe\Decryptors\Decryptor;
use Silencenjoyer\Jwe\Encryptors\Encryptor;
use Silencenjoyer\Jwe\KeyEncapsulation\RsaKeyUnwrapper;
use Silencenjoyer\Jwe\KeyEncapsulation\RsaKeyWrapper;
use Silencenjoyer\Jwe\Keys\Key;
use Silencenjoyer\Jwe\Serializers\CompactSerializer;

$publicKey  = Key::fromPath('/path/to/public.pem');
$privateKey = Key::fromPath('/path/to/private.pem');

// Sender side: encrypt with recipient's public key
$encryptor = new Encryptor(
    new RsaKeyWrapper($publicKey),
    new Aes256GcmFactory(),
);

$serializer = new CompactSerializer();
$compactJwe  = $serializer->serialize($encryptor->encrypt('Secret message'));

// Recipient side: decrypt with own private key
$decryptor = new Decryptor(
    new RsaKeyUnwrapper($privateKey),
    new Aes256GcmFactory(),
);

$plaintext = $decryptor->decrypt($serializer->unserialize($compactJwe));
// "Secret message"
```

To generate a key pair:

```
openssl genrsa -out private.pem 2048
openssl rsa -in private.pem -pubout -out public.pem
```

---

### AutoDecryptor — algorithm-agnostic decryption

[](#autodecryptor--algorithm-agnostic-decryption)

`AutoDecryptor` reads the algorithm identifiers from the JWE protected header at runtime and selects the correct unwrapper and cipher automatically. Register the algorithms you want to support once, then decrypt any compatible token.

```
use Silencenjoyer\Jwe\Ciphers\Factory\Aes256CbcFactory;
use Silencenjoyer\Jwe\Ciphers\Factory\Aes256GcmFactory;
use Silencenjoyer\Jwe\Decryptors\AutoDecryptor;
use Silencenjoyer\Jwe\KeyEncapsulation\DirectKeyUnwrapper;
use Silencenjoyer\Jwe\KeyEncapsulation\RsaKeyUnwrapper;
use Silencenjoyer\Jwe\Keys\Key;
use Silencenjoyer\Jwe\Serializers\CompactSerializer;

$privateKey = Key::fromPath('/path/to/private.pem');
$sharedKey  = Key::fromContent('a-32-byte-secret-key-goes-here!!');

$decryptor = (new AutoDecryptor())
    // Key encapsulation algorithms
    ->addUnwrapper(new RsaKeyUnwrapper($privateKey))   // handles alg=RSA-OAEP
    ->addUnwrapper(new DirectKeyUnwrapper($sharedKey)) // handles alg=dir
    // Content encryption algorithms
    ->addCipherFactory(new Aes256GcmFactory())         // handles enc=A256GCM
    ->addCipherFactory(new Aes256CbcFactory());        // handles enc=A256CBC-HS512

$serializer = new CompactSerializer();

// Works for any combination of the registered algorithms
$plaintext = $decryptor->decrypt($serializer->unserialize($compactJwe));
```

If the token uses an algorithm that was not registered, `UnsupportedAlgorithmException` is thrown.

---

### Serialization formats

[](#serialization-formats)

The library supports both JWE serialization formats defined in RFC 7516.

**Compact serialization** — five base64url parts separated by dots, suitable for HTTP headers and URLs:

```
use Silencenjoyer\Jwe\Serializers\CompactSerializer;

$serializer = new CompactSerializer();
$compact    = $serializer->serialize($token);   // "header.enckey.iv.ciphertext.tag"
$token      = $serializer->unserialize($compact);
```

**JSON serialization** — a JSON object, easier to inspect and log:

```
use Silencenjoyer\Jwe\Serializers\JsonSerializer;

$serializer = new JsonSerializer();
$json       = $serializer->serialize($token);
// {
//   "protected": "...",
//   "encrypted_key": "...",
//   "iv": "...",
//   "ciphertext": "...",
//   "tag": "..."
// }
$token = $serializer->unserialize($json);
```

---

### Loading keys

[](#loading-keys)

```
use Silencenjoyer\Jwe\Keys\Key;

// From a PEM file on disk
$key = Key::fromPath('/path/to/key.pem');

// From a string (e.g. an environment variable or a secrets manager)
$key = Key::fromContent(getenv('SHARED_SECRET'));
```

⚒️ Code Quality:
----------------

[](#️-code-quality)

- Tests: `composer test`
- Static analysis: `composer phpstan`
- PSR-12 formatting

License
-------

[](#license)

MIT

###  Health Score

32

—

LowBetter than 69% of packages

Maintenance83

Actively maintained with recent releases

Popularity5

Limited adoption so far

Community6

Small or concentrated contributor base

Maturity29

Early-stage or recently created project

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Unknown

Total

1

Last Release

88d ago

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/92015969?v=4)[Andrii Gebrych](/maintainers/Silencenjoyer)[@silencenjoyer](https://github.com/silencenjoyer)

---

Top Contributors

[![silencenjoyer](https://avatars.githubusercontent.com/u/92015969?v=4)](https://github.com/silencenjoyer "silencenjoyer (3 commits)")

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan

Type Coverage Yes

### Embed Badge

![Health badge](/badges/silencenjoyer-jwe/health.svg)

```
[![Health](https://phpackages.com/badges/silencenjoyer-jwe/health.svg)](https://phpackages.com/packages/silencenjoyer-jwe)
```

###  Alternatives

[mews/purifier

Laravel 5/6/7/8/9/10 HtmlPurifier Package

2.0k18.7M144](/packages/mews-purifier)[paragonie/ecc

PHP Elliptic Curve Cryptography library

24820.0k41](/packages/paragonie-ecc)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
