PHPackages                             tobento/service-encryption - 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. tobento/service-encryption

ActiveLibrary[Security](/categories/security)

tobento/service-encryption
==========================

Encryption for PHP applications.

2.0(7mo ago)0704MITPHPPHP &gt;=8.4

Since Jun 3Pushed 7mo ago1 watchersCompare

[ Source](https://github.com/tobento-ch/service-encryption)[ Packagist](https://packagist.org/packages/tobento/service-encryption)[ Docs](https://www.tobento.ch)[ RSS](/packages/tobento-service-encryption/feed)WikiDiscussions 2.x Synced 1mo ago

READMEChangelog (3)Dependencies (6)Versions (5)Used By (4)

Encryption Service
==================

[](#encryption-service)

Encryption interfaces for PHP applications using [Crypto](https://github.com/defuse/php-encryption) as default implementation.

Table of Contents
-----------------

[](#table-of-contents)

- [Getting started](#getting-started)
    - [Requirements](#requirements)
    - [Highlights](#highlights)
- [Documentation](#documentation)
    - [Basic Usage](#basic-usage)
        - [Encrypt And Decrypt](#encrypt-and-decrypt)
    - [Interfaces](#interfaces)
        - [Encrypter Factory Interface](#encrypter-factory-interface)
        - [Encrypter Interface](#encrypter-interface)
        - [Encrypters Interface](#encrypters-interface)
        - [Key Generator Interface](#key-generator-interface)
    - [Encrypters](#encrypters)
        - [Default Encrypters](#default-encrypters)
        - [Lazy Encrypters](#lazy-encrypters)
    - [Crypto Implementation](#crypto-implementation)
- [Credits](#credits)

---

Getting started
===============

[](#getting-started)

Add the latest version of the encryption service project running this command.

```
composer require tobento/service-encryption

```

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

[](#requirements)

- PHP 8.4 or greater

Highlights
----------

[](#highlights)

- Framework-agnostic, will work with any project
- Decoupled design

Documentation
=============

[](#documentation)

Basic Usage
-----------

[](#basic-usage)

### Encrypt And Decrypt

[](#encrypt-and-decrypt)

```
use Tobento\Service\Encryption\EncrypterInterface;

class SomeService
{
    public function __construct(
        private EncrypterInterface $encrypter,
    ) {
        $encrypted = $encrypter->encrypt('something');

        $decrypted = $encrypter->decrypt($encrypted);
    }
}
```

Interfaces
----------

[](#interfaces)

### Encrypter Factory Interface

[](#encrypter-factory-interface)

You may use the encrypter factory interface for creating encrypters.

```
namespace Tobento\Service\Encryption;

interface EncrypterFactoryInterface
{
    /**
     * Create a new Encrypter.
     *
     * @param string $name
     * @param array $config
     * @return EncrypterInterface
     * @throws EncrypterException
     */
    public function createEncrypter(string $name, array $config): EncrypterInterface;
}
```

### Encrypter Interface

[](#encrypter-interface)

```
namespace Tobento\Service\Encryption;

interface EncrypterInterface
{
    /**
     * Returns the encrypter name.
     *
     * @return string
     */
    public function name(): string;

    /**
     * Returns the encrypted data.
     *
     * @param mixed $data
     * @return string
     * @throws EncryptException
     */
    public function encrypt(mixed $data): string;

    /**
     * Returns the decrypted data.
     *
     * @param string $encrypted
     * @return mixed
     * @throws DecryptException
     */
    public function decrypt(string $encrypted): mixed;
}
```

### Encrypters Interface

[](#encrypters-interface)

```
namespace Tobento\Service\Encryption;

interface EncryptersInterface
{
    /**
     * Returns true if encrypter exists, otherwise false.
     *
     * @param string $name
     * @return bool
     */
    public function has(string $name): bool;

    /**
     * Returns the encrypter if exists, otherwise null.
     *
     * @param string $name
     * @return null|EncrypterInterface
     */
    public function get(string $name): null|EncrypterInterface;
}
```

### Key Generator Interface

[](#key-generator-interface)

You may use the key generator interface for generating keys.

```
namespace Tobento\Service\Encryption;

interface KeyGeneratorInterface
{
    /**
     * Returns a generated new key.
     *
     * @return string
     * @throws KeyException
     */
    public function generateKey(): string;
}
```

Encrypters
----------

[](#encrypters)

You may use the following encrypters if you want to manage multiple encrypters for your application.

### Default Encrypters

[](#default-encrypters)

```
use Tobento\Service\Encryption\Encrypters;
use Tobento\Service\Encryption\EncryptersInterface;
use Tobento\Service\Encryption\EncrypterInterface;

$encrypters = new Encrypters(
    $encrypter, // EncrypterInterface
    $anotherEncrypter, // EncrypterInterface
);

var_dump($encrypters instanceof EncryptersInterface);
// bool(true)

var_dump($encrypters instanceof EncrypterInterface);
// bool(true)
// Uses the first encrypter specified.
```

Check out the [Encrypters Interface](#encrypters-interface) to learn more about it.

Check out the [Encrypter Interface](#encrypter-interface) to learn more about it.

### Lazy Encrypters

[](#lazy-encrypters)

```
use Tobento\Service\Encryption\LazyEncrypters;
use Tobento\Service\Encryption\EncryptersInterface;
use Tobento\Service\Encryption\EncrypterInterface;
use Tobento\Service\Encryption\EncrypterFactoryInterface;
use Tobento\Service\Encryption\Crypto;
use Psr\Container\ContainerInterface;

$encrypters = new LazyEncrypters(
    container: $container, // ContainerInterface
    encrypters: [
        'default' => [
            // factory must implement EncrypterFactoryInterface
            'factory' => Crypto\EncrypterFactory::class,
            'config' => [
                'key' => 'secret-key',
            ],
        ],

        'cookies' => [
            // ...
        ],
    ],
);

var_dump($encrypters instanceof EncryptersInterface);
// bool(true)

var_dump($encrypters instanceof EncrypterInterface);
// bool(true)
// Uses the first encrypter specified.
```

Check out the [Encrypters Interface](#encrypters-interface) to learn more about it.

Check out the [Encrypter Interface](#encrypter-interface) to learn more about it.

Crypto Implementation
---------------------

[](#crypto-implementation)

You may check out the [Crypto](https://github.com/defuse/php-encryption) to learn more about it.

**Usage**

```
use Tobento\Service\Encryption\Crypto\KeyGenerator;
use Tobento\Service\Encryption\Crypto\EncrypterFactory;
use Tobento\Service\Encryption\KeyGeneratorInterface;
use Tobento\Service\Encryption\EncrypterFactoryInterface;
use Tobento\Service\Encryption\EncrypterInterface;

$keyGenerator = new KeyGenerator();

var_dump($keyGenerator instanceof KeyGeneratorInterface);
// bool(true)

// Generate a key and store it savely for reusage.
$key = $keyGenerator->generateKey();

$encrypterFactory = new EncrypterFactory();

var_dump($encrypterFactory instanceof EncrypterFactoryInterface);
// bool(true)

$encrypter = $encrypterFactory->createEncrypter(
    name: 'crypto',
    config: ['key' => $key],
);

var_dump($encrypter instanceof EncrypterInterface);
// bool(true)
```

Credits
=======

[](#credits)

- [Tobias Strub](https://www.tobento.ch)
- [All Contributors](../../contributors)
- [Crypto](https://github.com/defuse/php-encryption)

###  Health Score

40

—

FairBetter than 88% of packages

Maintenance62

Regular maintenance activity

Popularity11

Limited adoption so far

Community13

Small or concentrated contributor base

Maturity65

Established project with proven stability

 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

Every ~211 days

Total

5

Last Release

236d ago

Major Versions

1.x-dev → 2.02025-09-24

PHP version history (2 changes)1.0.0PHP &gt;=8.0

2.0PHP &gt;=8.4

### Community

Maintainers

![](https://www.gravatar.com/avatar/055d6a1b5c2384bb179c75ab0b55914231d898fdc4dffeb30770f81200e52206?d=identicon)[TOBENTOch](/maintainers/TOBENTOch)

---

Top Contributors

[![tobento-ch](https://avatars.githubusercontent.com/u/16684832?v=4)](https://github.com/tobento-ch "tobento-ch (11 commits)")

---

Tags

phppackageencryptiontobento

###  Code Quality

TestsPHPUnit

Static AnalysisPsalm

Type Coverage Yes

### Embed Badge

![Health badge](/badges/tobento-service-encryption/health.svg)

```
[![Health](https://phpackages.com/badges/tobento-service-encryption/health.svg)](https://phpackages.com/packages/tobento-service-encryption)
```

###  Alternatives

[stymiee/php-simple-encryption

The PHP Simple Encryption library is designed to simplify the process of encrypting and decrypting data while ensuring best practices are followed. By default is uses a secure encryption algorithm and generates a cryptologically strong initialization vector so developers do not need to becomes experts in encryption to securely store sensitive data.

448.0k](/packages/stymiee-php-simple-encryption)[poly-crypto/poly-crypto

High-level cryptographic functions that are interoperable between NodeJS and PHP 7.1+

127.8k1](/packages/poly-crypto-poly-crypto)

PHPackages © 2026

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