hamoi1/eloquent-encryptable

This package provides a way to encrypt and decrypt the data in the database using the Hill Cipher algorithm.

0.1.3 2025-02-06 05:27 UTC

This package is auto-updated.

Last update: 2025-05-06 05:57:07 UTC


README

Latest Version on Packagist Total Downloads Software License

This package allows you to encrypt and decrypt model fields using the Hill Cipher algorithm in your Laravel application.

Table of Contents

Features

  • Encrypts and decrypts model fields using the Hill Cipher algorithm.
  • Supports re-encryption of model data with a new key matrix.
  • Handles uppercase and lowercase letters, spaces, and numbers.
  • encrypt and decrypt text using the Hill Cipher algorithm.
  • Supports 2x2 and 3x3 key matrices.

Matrix Key Support

  • [✅] 2x2 matrix
  • [✅] 3x3 matrix
  • [❌] 4x4 matrix
  • [❌] 5x5 matrix

Language Support

  • [✅] English
  • [✅] Kurdish (Sorani)
  • [✅] Arabic

Installation

1. Install the package

You can install the eloquent-encryptable package via composer:

composer require hamoi1/eloquent-encryptable

2. Publish the configuration file

you can publish the configuration file to change the key matrix for encryption and decryption, and assign models to re-encrypt by running the following command:

php artisan vendor:publish  --provider="Hamoi1\\EloquentEncryptAble\\EloquentEncryptAbleServiceProvider" --tag="config"

3. Configure the key matrix

in .env file you can configure the key matrix for encryption and decryption, by adding the following lines:

# for 2x2 matrix
ELOQUENT_ENCRYPTABLE_KEY= "[[4, 7], [3, 10]]"

# for 3x3 matrix
ELOQUENT_ENCRYPTABLE_KEY= "[[1, 11,6], [21, 20,15] ,[2, 20, 9]]"

now the key matrix should be a 2x2 matrix, and the previous key matrix is used to re-encrypt model data with a new key matrix.

4. Use for text encryption and decryption

You can use the EloquentEncryptAbleService service to encrypt and decrypt text using the Hill Cipher algorithm.

use Hamoi1\EloquentEncryptAble\Services\EloquentEncryptAbleService;

$cipher = new EloquentEncryptAbleService();
$encrypted = $cipher->encrypt('Hello, World!');
$decrypted = $cipher->decrypt($encrypted);

output of the above code will be:

$encrypted ='"Ejrno, Wtenl!";
$decrypted = 'Hello, World!';

5. Encrypt and decrypt model fields

You can encrypt and decrypt model fields by using the EncryptAble trait in your model class, and specify the fields that you want to encrypt in the $encryptAble property.

use Illuminate\Database\Eloquent\Model;
use Hamoi1\EloquentEncryptAble\Traits\EncryptAble;

class User extends Model
{
    use EncryptAble;

    public $encryptAble = ['name', 'email'];
}

now the name and email fields will be encrypted and decrypted automatically, when you save and retrieve , like the following example:

$user = new User();
$user->name = 'John Doe';
$user->email = 'john@gmail.com';
$user->save();

$user = User::find(1);
$user->name; // John Doe
$user->email; // john@gmail.com

the name and email fields will be encrypted in the database, and decrypted when you retrieve them.

6. Re-encrypt model data

You can re-encrypt model data with a new key matrix , but you should specify the previous key matrix in the .env file.

# for 2x2 key matrix
ELOQUENT_ENCRYPTABLE_KEY= "[[4, 7], [3, 10]]"
ELOQUENT_ENCRYPTABLE_PREVIOUS_KEY= "[[14, 17], [13, 20]]"

# for 3x3 matrix
ELOQUENT_ENCRYPTABLE_KEY= "[[1, 11,6], [21, 20,15] ,[2, 20, 9]]"
ELOQUENT_ENCRYPTABLE_PREVIOUS_KEY= "[[13,5,14],[7,10,1],[4,3,16]]"

and added models that you want to re-encrypt in the config/eloquent-encryptable.php file:

'models' => [
    User::class,
    Category::class
],

then you can run the following command to re-encrypt model data:

php artisan eloquent-encryptable:re-encrypt

This command will re-encrypt all model fields that are encrypted with the previous key matrix will be re-encrypted with the new key matrix.

7. Custom Validation Rules

Unique Rule

You can use the unique validation rule with encrypted fields by using the EncryptAbleUniqueRule rule.

use Hamoi1\EloquentEncryptAble\Rules\EncryptAbleUniqueRule;

$request->validate([
    'email' => ['required', new EncryptAbleUniqueRule('users', 'email')],
]);

and you can add 3rd parameter to expect a specific value:

use Hamoi1\EloquentEncryptAble\Rules\EncryptAbleUniqueRule;

$request->validate([
    'email' => ['required', new EncryptAbleUniqueRule('users', 'email',[
        'column' => 'id',
        'value' => $this->user_id
    ])],
]);

Exists Rule

You can use the exists validation rule with encrypted fields by using the EncryptAbleExistsRule rule.

use Hamoi1\EloquentEncryptAble\Rules\EncryptAbleExistsRule;

$request->validate([
    'email' => ['required', new EncryptAbleExistsRule('users', 'email')],
]);

and you can add 3rd parameter to expect a specific value:

use Hamoi1\EloquentEncryptAble\Rules\EncryptAbleExistsRule;

$request->validate([
    'email' => ['required', new EncryptAbleExistsRule('users', 'email',[
        'column' => 'id',
        'value' => $this->user_id
    ])],
]);

8. Blade Directives

You can use the @encrypt and @decrypt blade directives to encrypt and decrypt text in your blade views.

@encrypt('Hello, World!')
@decrypt('Ejrno, Wtenl!')

Security

If you discover any security-related issues, please email ihama9728@gmail.com instead of using the issue tracker.

License

The MIT License (MIT). Please see License File for more information.