PHPackages                             mrzainulabideen/aesencrypt - 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. [Database &amp; ORM](/categories/database)
4. /
5. mrzainulabideen/aesencrypt

ActiveLibrary[Database &amp; ORM](/categories/database)

mrzainulabideen/aesencrypt
==========================

Laravel MySql AES Encrypt/Decrypt

7.0(6y ago)0806[3 PRs](https://github.com/mrzainulabideen/mysql-aes-encrypt/pulls)Apache-2.0PHPPHP &gt;=5.4.0

Since Oct 13Pushed 4y agoCompare

[ Source](https://github.com/mrzainulabideen/mysql-aes-encrypt)[ Packagist](https://packagist.org/packages/mrzainulabideen/aesencrypt)[ Docs](https://github.com/mrzainulabideen/mysql-aes-encrypt)[ RSS](/packages/mrzainulabideen-aesencrypt/feed)WikiDiscussions master Synced today

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

- [Installation](#1install-the-package-via-composer)
- [Configure Provider](#2configure-provider)
- [Updating your Eloquent Models](#updating-your-eloquent-models)
- [Creating tables to support encrypt columns](#creating-tables-to-support-encrypt-columns)
- [Set encryption key in .env file](#set-encryption-key-in-env-file)
- [Encrypt existing data](#encrypt-existing-data)
- [Decrypt your data in MySQL](#decrypt-your-data-in-mySQL)

Laravel 7.x, 6.x &amp; 5.x MySql AES Encrypt/Decrypt
====================================================

[](#laravel-7x-6x--5x-mysql-aes-encryptdecrypt)

Based on

Improvements:

- Added improved security by using an unique IV for each encrypted field.
- Added support for multiple encryption methods including: aes-256-cbc
- Added use of MySQL session variables to prevent the encryption key from being outputted when an sql error occures.
- Added laravel 6 support
- Added laravel 7 support (By me)

Laravel 5.x, 6.x &amp; 7.x Database Encryption in mysql side, use native mysql function AES\_DECRYPT and AES\_ENCRYPT
Auto encrypt and decrypt signed fields/columns in your Model
Can use all functions of Eloquent/Model
You can perform the operations "=&gt;, &lt;',' between ',' LIKE ' in encrypted columns

1.Install the package via Composer:
-----------------------------------

[](#1install-the-package-via-composer)

```
For laravel 7.x:
$ composer require mrzainulabideen/aesencrypt

For laravel 6.x:
$ composer require mrzainulabideen/aesencrypt:6.0.x-dev

For laravel 5.x:
$ composer require mrzainulabideen/aesencrypt:5.0.x-dev
```

2.Configure provider
--------------------

[](#2configure-provider)

You'll need to add to add a service provider if you are using Laravel 5.4 or lower or if the encryption is not working, add to following in config/app.php:

```
'providers' => array(
    mrzainulabideen\AESEncrypt\Database\DatabaseServiceProviderEncrypt::class
)
```

Updating Your Eloquent Models
-----------------------------

[](#updating-your-eloquent-models)

Your models that have encrypted columns, should extend from ModelEncrypt:

```
namespace App\Models;

use mrzainulabideen\AESEncrypt\Database\Eloquent\ModelEncrypt;

class Person extends ModelEncrypt
{
    /**
     * The attributes that are encrypted.
     *
     * @var array
     */
    protected $fillableEncrypt = [
        'name'
    ];

}
```

Creating tables to support encrypt columns
------------------------------------------

[](#creating-tables-to-support-encrypt-columns)

It adds new features to Schema which you can use in your migrations:

```
    Schema::create('persons', function (Blueprint $table) {
        // here you do all columns supported by the schema builder
        $table->increments('id')->unsigned;
        $table->string('description', 250);
        $table ->unsignedInteger('created_by')->nullable();
        $table ->unsignedInteger('updated_by')->nullable();
    });

    // once the table is created use a raw query to ALTER it and add the BLOB, MEDIUMBLOB or LONGBLOB
    DB::statement("ALTER TABLE persons ADD name MEDIUMBLOB after id");
```

});

Set encryption settings in .env file
------------------------------------

[](#set-encryption-settings-in-env-file)

```
APP_AESENCRYPT_KEY=yourencryptedkey
APP_AESENCRYPT_MODE=aes-256-cbc
```

See [https://dev.mysql.com/doc/refman/8.0/en/server-system-variables.html#sysvar\_block\_encryption\_mode](https://dev.mysql.com/doc/refman/8.0/en/server-system-variables.html#sysvar_block_encryption_mode) for all available encryption methods.

To publish the config file and view run the following command

```
php artisan vendor:publish --provider="mrzainulabideen\AESEncrypt\AesEncryptServiceProvider"
```

Encrypt existing data
---------------------

[](#encrypt-existing-data)

In order to use this package with existing data, you must first encrypt all existing columns you want to use.

Note: If the database is allready encrypted, make sure to decrypt it before executing the folowing query.

**Always make a back-up before making changes to your data**

The easiest and more secure way to use this, is to use this MySQL function when updating your records:

```
CREATE FUNCTION `aes_encrypt_string` (col blob, aeskey char(255))
RETURNS blob
BEGIN
SET @iv = RANDOM_BYTES(16);

RETURN CONCAT(AES_ENCRYPT(col, aeskey, @iv), ".iv.",@iv);
END
```

After adding the MySQL function, update your records like so:

```
SET @@SESSION.block_encryption_mode = 'aes-256-cbc';
SET @AESKEY = 'yourencryptedkey';

UPDATE your_table SET your_column = aes_encrypt_string(your_column, @AESKEY), your_column2 = aes_encrypt_string(your_column2, @AESKEY) WHERE your_column NOT LIKE '%.iv.%';
```

The folowing code will ensure the only data that isn't encrypted yet will be encrypted, in case you need to run the query multiple times.

If you cannot create MySQL functions you can perform the following but this will use the same IV for every record which is less secure.

```
SET @@SESSION.block_encryption_mode = 'aes-256-cbc';
SET @AESKEY = 'yourencryptedkey';
SET @iv = RANDOM_BYTES(16);

UPDATE your_table SET your_column = CONCAT(AES_ENCRYPT(your_column, @AESKEY, @iv), ".iv.",@iv), your_column2 = CONCAT(AES_ENCRYPT(your_column2, @AESKEY, @iv), ".iv.",@iv) WHERE your_column NOT LIKE '%.iv.%';
```

The folowing code will ensure the only data that isn't encrypted yet will be encrypted, in case you need to run the query multiple times.

Decrypt your data in MySQL
--------------------------

[](#decrypt-your-data-in-mysql)

If you want to decrypt your data using mysql query, you can add this function to your mysql database:

```
CREATE FUNCTION `aes_decrypt_string` (col blob, aeskey char(255))
RETURNS text
BEGIN

RETURN CAST(AES_DECRYPT(SUBSTRING_INDEX(col, '.iv.', 1), aeskey, SUBSTRING_INDEX(col, '.iv.', -1)) as char);
END
```

Now when you want to decrypt your mysql you can do so like this:

```
SET @@SESSION.block_encryption_mode = 'aes-256-cbc';
SET @AESKEY = 'yourencryptedkey';
SELECT *, aes_decrypt_string(yourEncryptedColum, @AESKEY) decryptedColumn, aes_decrypt_string(yourEncryptedColum2, @AESKEY) decryptedColumn2  FROM yourtable;
```

Or if you cannot or do not want to use a MySQL function you can use the following query

```
SET @@SESSION.block_encryption_mode = 'aes-256-cbc';
SET @AESKEY = 'yourencryptedkey';

SELECT CAST(AES_DECRYPT(SUBSTRING_INDEX(yourEncryptedColum, '.iv.', 1), @AESKEY, SUBSTRING_INDEX(yourEncryptedColum, '.iv.', -1)) as CHAR)  decrypted_column FROM yourtable WHERE yourEncryptedColum LIKE '%.iv.%';
```

###  Health Score

27

—

LowBetter than 49% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity12

Limited adoption so far

Community13

Small or concentrated contributor base

Maturity56

Maturing project, gaining track record

 Bus Factor2

2 contributors hold 50%+ of commits

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

Total

4

Last Release

2201d ago

Major Versions

5.0.x-dev → 6.0.x-dev2019-10-13

6.0.x-dev → 7.02020-04-30

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/31840471?v=4)[Zain Ul Abideen](/maintainers/mrzainulabideen)[@mrzainulabideen](https://github.com/mrzainulabideen)

---

Top Contributors

[![devmaster10](https://avatars.githubusercontent.com/u/38945076?v=4)](https://github.com/devmaster10 "devmaster10 (20 commits)")[![MarshallDez](https://avatars.githubusercontent.com/u/210487023?v=4)](https://github.com/MarshallDez "MarshallDez (14 commits)")[![mrzainulabideen](https://avatars.githubusercontent.com/u/31840471?v=4)](https://github.com/mrzainulabideen "mrzainulabideen (5 commits)")[![redsd](https://avatars.githubusercontent.com/u/3687115?v=4)](https://github.com/redsd "redsd (3 commits)")

---

Tags

laravellaravel-7mysql-aesmysql-aes-decryptmysql-aes-encryptlaravelaesmysqlencrypt

### Embed Badge

![Health badge](/badges/mrzainulabideen-aesencrypt/health.svg)

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

###  Alternatives

[cviebrock/eloquent-sluggable

Easy creation of slugs for your Eloquent models in Laravel

4.0k13.6M253](/packages/cviebrock-eloquent-sluggable)[kirschbaum-development/eloquent-power-joins

The Laravel magic applied to joins.

1.6k25.2M34](/packages/kirschbaum-development-eloquent-power-joins)[tucker-eric/eloquentfilter

An Eloquent way to filter Eloquent Models

1.8k4.8M26](/packages/tucker-eric-eloquentfilter)[cviebrock/eloquent-taggable

Easy ability to tag your Eloquent models in Laravel.

567694.8k3](/packages/cviebrock-eloquent-taggable)[dragon-code/laravel-data-dumper

Adding data from certain tables when executing the `php artisan schema:dump` console command

3418.6k](/packages/dragon-code-laravel-data-dumper)[mpyw/laravel-mysql-system-variable-manager

A tiny extension of MySqlConnection that manages session system variables

115.8k](/packages/mpyw-laravel-mysql-system-variable-manager)

PHPackages © 2026

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