PHPackages                             bereznii/yii2-encrypter - 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. bereznii/yii2-encrypter

ActiveYii2-extension[Security](/categories/security)

bereznii/yii2-encrypter
=======================

Openssl Encrypter for Yii2

1.1.1(4y ago)031GPL-3.0PHP

Since Jan 23Pushed 4y agoCompare

[ Source](https://github.com/bereznii/yii2-encrypter)[ Packagist](https://packagist.org/packages/bereznii/yii2-encrypter)[ RSS](/packages/bereznii-yii2-encrypter/feed)WikiDiscussions master Synced today

READMEChangelog (3)Dependencies (1)Versions (9)Used By (0)

Openssl Encrypter for Yii2
==========================

[](#openssl-encrypter-for-yii2)

This extension is used for two-way encryption. The cypher method used is **AES256**.

The main difference from [original package](https://github.com/nickcv-ln/yii2-encrypter) is usage of unique initialization vector for each operation. The main purpose is to provide additional level of security by randomization of resulting hash. Also, as it seems from threads on stackoverflow, publicly stored IV is a very bad practice. However, it is needed to decrypt value. Implemented idea is described below.

Step-by-step **encryption**:

1. Passphrase (32 bytes length) is being stored as ENV constant;
2. Unique IV is randomly generated for each encryption operation by [openssl\_random\_pseudo\_bytes()](https://www.php.net/manual/en/function.openssl-random-pseudo-bytes.php) and is presented as 16 bytes length string;
3. Input is encrypted with [openssl\_encrypt()](https://www.php.net/manual/en/function.openssl-encrypt.php). Result is encrypted byte-string;
4. IV (16 bytes) is prepended in front of byte-string from previous step;
5. Concatenated string is encoded with [base64\_encode()](https://www.php.net/manual/en/function.base64-encode) to avoid encoding problems when transferring over a network or storing in a database.
6. Result is a securely encrypted and encoded string.

Step-by-step **decryption**:

1. Fully encrypted string is decoded with [base64\_decode()](https://www.php.net/manual/en/function.base64-decode.php);
2. First 16 bytes is retrieved for further decryption process. This is IV;
3. Remaining part of string is decrypted with [openssl\_decrypt()](https://www.php.net/manual/en/function.openssl-decrypt) using IV from previous step and passphrase from ENV;
4. Result is the initial string.

---

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

[](#installation)

The preferred way to install this extension is through [composer](http://getcomposer.org/download/).

Either run

```
composer require bereznii/yii2-encrypter

```

or add

```
"bereznii/yii2-encrypter": "*"

```

to the require section of your `composer.json` file.

Set Up
------

[](#set-up)

To use Encrypter component it needs to be registered in [Components list of Yii2 Application](https://www.yiiframework.com/doc/guide/2.0/en/structure-application-components).

```
'components' => [
    'encrypter' => [
        'class' => \bereznii\encrypter\components\Encrypter::class,
        'key' => getenv('ENCRYPTION_KEY'),
    ],
    ...
]

```

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

[](#basic-usage)

### Component

[](#component)

Encrypter can be used both from web and console.

To encrypt value manually in any part of the application encrypter can be used as follows:

```
Yii::$app->encrypter->encrypt('Hello World!');

```

or to decrypt:

```
Yii::$app->encrypter->decrypt('Hello World!');

```

### Behavior

[](#behavior)

The extension also comes with a behavior class that can be easily attached to any ActiveRecord Model.

Use the following syntax to attach the behavior.

```
public function behaviors()
{
    return [
        'encryption' => [
            'class' => \bereznii\encrypter\behaviors\EncryptionBehavior::class,
            'attributes' => [
                'attributeName1',
                'attributeName2',
            ],
        ],
    ];
}

```

The behavior will automatically encrypt all the data before saving it on the database and decrypt it after the retrieve.

**Keep in mind that the behavior will use the current configuration of the extension for the encryption.**

Unit Testing
------------

[](#unit-testing)

[Original package](https://github.com/nickcv-ln/yii2-encrypter) was built with TDD. However, current package is not covered with unit-tests due to lack of time caused by russian invasion to Ukraine. Hopefully, one day unit tests will be added and package will become more customizable. But until then, it is published for educational purposes only.

Warnings
--------

[](#warnings)

It is extremely hard (or practically impossible) to decrypt the data without the password, copy of it should be store in secure place to avoid losing all encrypted data.

**Two-way encryption should not be used to store passwords: you should use a one-way encryption function like sha1 and a SALT**

###  Health Score

28

—

LowBetter than 52% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity4

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity68

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 ~432 days

Recently: every ~648 days

Total

7

Last Release

1583d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/c76380ae0be08fce5e343215533a7b3f30c876b7741d4ce16147872e59ad30c7?d=identicon)[bereznii](/maintainers/bereznii)

---

Top Contributors

[![dmytrobereznii](https://avatars.githubusercontent.com/u/39523268?v=4)](https://github.com/dmytrobereznii "dmytrobereznii (5 commits)")

---

Tags

opensslphpyii2yii2-extensionyii2-frameworkyii2-modulesencryptionopensslyii2extensionBehavioractive-record

### Embed Badge

![Health badge](/badges/bereznii-yii2-encrypter/health.svg)

```
[![Health](https://phpackages.com/badges/bereznii-yii2-encrypter/health.svg)](https://phpackages.com/packages/bereznii-yii2-encrypter)
```

###  Alternatives

[nickcv/yii2-encrypter

Openssl Encrypter for Yii2

19680.1k1](/packages/nickcv-yii2-encrypter)[craftcms/cms

Craft CMS

3.6k3.6M3.1k](/packages/craftcms-cms)[skeeks/cms

SkeekS CMS — control panel and tools based on php framework Yii2

13825.8k58](/packages/skeeks-cms)[hyperia/yii2-secure-headers

Secure headers for your Yii2 app

21206.7k](/packages/hyperia-yii2-secure-headers)[v0lume/yii2-meta-tags

DB based model meta data for SEO

204.1k](/packages/v0lume-yii2-meta-tags)

PHPackages © 2026

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