PHPackages                             gestazion/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. gestazion/aesencrypt

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

gestazion/aesencrypt
====================

Laravel MySql AES Encrypt/Decrypt

10.0.0(2y ago)04Apache-2.0PHPPHP &gt;=5.4.0

Since Sep 27Pushed 2y agoCompare

[ Source](https://github.com/Gestazion2023/mysql-aes-encrypt)[ Packagist](https://packagist.org/packages/gestazion/aesencrypt)[ Docs](https://github.com/redsd/mysql-aes-encrypt)[ RSS](/packages/gestazion-aesencrypt/feed)WikiDiscussions master Synced 1mo ago

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

Laravel MySql AES Encrypt/Decrypt
=================================

[](#laravel-mysql-aes-encryptdecrypt)

- [Installation](#1install-the-package-via-composer)
- [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)

Summary
-------

[](#summary)

Based on

Improvements:

- Added improved security by using a 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 a sql error occurs.

Laravel 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)

```
composer require gestazion/aes-encrypt
```

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

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

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

```
namespace App\Models;

use Gestazion\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', static function (Blueprint $table) {
        // Here you do all columns supported by the schema builder
        $table->id();
        $table->string('description', 250)->nullable();
        $table->timestamps();

        // This is used to add BLOB type into database
        $table->binary('name');
    });

    // once the table is created use a raw query to ALTER it and add the 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)

```
AES_ENCRYPT_KEY=yourencryptedkey
AES_ENCRYPT_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="Gestazion\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

19

—

LowBetter than 10% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity3

Limited adoption so far

Community9

Small or concentrated contributor base

Maturity39

Early-stage or recently created project

 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

Unknown

Total

1

Last Release

959d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/3c9838ce55ee98eec0e43efa01ba8deea7ca154d7ea0070ef40926557a21c637?d=identicon)[Gestazion2023](/maintainers/Gestazion2023)

---

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)")[![redsd](https://avatars.githubusercontent.com/u/3687115?v=4)](https://github.com/redsd "redsd (11 commits)")

---

Tags

laravelaesmysqlencrypt

### Embed Badge

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

```
[![Health](https://phpackages.com/badges/gestazion-aesencrypt/health.svg)](https://phpackages.com/packages/gestazion-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)[dolphiq/laravel-aescrypt

AES encrypt and decrypt Eloquent attributes inspired by elocryptfive

171.7k](/packages/dolphiq-laravel-aescrypt)

PHPackages © 2026

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