PHPackages                             chuyubo/field\_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. chuyubo/field\_encryption

ActiveLibrary[Security](/categories/security)

chuyubo/field\_encryption
=========================

数据库字段加密解密

v6.0.0(3y ago)02MITPHPPHP ^7.2

Since Feb 10Pushed 3y ago1 watchersCompare

[ Source](https://github.com/YuboChu/field_encryption)[ Packagist](https://packagist.org/packages/chuyubo/field_encryption)[ RSS](/packages/chuyubo-field-encryption/feed)WikiDiscussions master Synced 1mo ago

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

基于laravel实现数据库敏感字段加密
====================

[](#基于laravel实现数据库敏感字段加密)

原理：
---

[](#原理)

#### 1.替代db.factory ,将对应的 factory 指向自定义 FieldEncryptionConnectionFactory, 将对应的mysqlConnection 指向自定义FieldEncryptionMysqlConnection,最终将Builder替换成自定义FieldBuilder

[](#1替代dbfactory-将对应的-factory-指向自定义-fieldencryptionconnectionfactory-将对应的mysqlconnection-指向自定义fieldencryptionmysqlconnection最终将builder替换成自定义fieldbuilder)

#### 2.改写 FieldBuilder 中get,insert,update 等方法，将需要加密的字段进行加密，读取时将加密字段进行解密

[](#2改写-fieldbuilder-中getinsertupdate-等方法将需要加密的字段进行加密读取时将加密字段进行解密)

注意：
---

[](#注意)

#### 并不是所有写法都适合，比如Raw，whereRaw等原生写法，也不支持sql函数对该字段进行处理，当相关业务涉及到上述写法，建议修改写法或者加字段加密成加密字段再添加

[](#并不是所有写法都适合比如rawwhereraw等原生写法也不支持sql函数对该字段进行处理当相关业务涉及到上述写法建议修改写法或者加字段加密成加密字段再添加)

##### 加密:

[](#加密)

```
//$rule => 配置保留前置位数和加密
$rule = ['pre_len' => 0, 'encryption_field_len' => 20000];
/** @var \FieldEncryption\Utils\EncryptionUtils $encryption */
$encryption = app(\FieldEncryption\Utils\EncryptionUtils::class);
$value = $encryption->encryptionAes($value, $rule['pre_len'], $rule['encryption_field_len']);

```

##### 解密:

[](#解密)

```
/** @var \FieldEncryption\Utils\DecryptUtils $decrypt */
$decrypt = app(\FieldEncryption\Utils\DecryptUtils::class);
$value = $decrypt->decryptAes($value);

```

使用方法：
-----

[](#使用方法)

#### 1.在laravel中app/config/app.php 中 providers 添加

[](#1在laravel中appconfigappphp-中-providers-添加)

```
 \FieldEncryption\Providers\DatabaseServiceCustomProvider::class,
 \FieldEncryption\Providers\FieldEncryptionProvider::class,

```

注意要放到 Illuminate\\Database\\DatabaseServiceProvider::class下面的位置，相当于覆盖

#### 2.将 vendor/chuyubo/field\_encryption/config/field\_encryption.php 复制到app/config/文件夹下

[](#2将-vendorchuyubofield_encryptionconfigfield_encryptionphp-复制到appconfig文件夹下)

##### aes\_key 加密密钥，保证不进行修改

[](#aes_key-加密密钥保证不进行修改)

##### aes\_pre 加密后的字段前缀标识，不要为空

[](#aes_pre-加密后的字段前缀标识不要为空)

##### aes\_tail 加密后的字段后缀标识，不要为空

[](#aes_tail-加密后的字段后缀标识不要为空)

##### table\_tmp 临时表后缀，要复杂不要与原表名后缀一样，不要为空

[](#table_tmp-临时表后缀要复杂不要与原表名后缀一样不要为空)

##### table\_tmp\_switch 临时表开关，true|false，跑同步脚本的时候需要打开，其他情况下关闭

[](#table_tmp_switch-临时表开关truefalse跑同步脚本的时候需要打开其他情况下关闭)

##### field 表字段配置 二维数组，内容如下

[](#field-表字段配置-二维数组内容如下)

```
[
    [
        'table' => '', //table 表名称
        'column' => '', //加密列
        'rule' => '', //加密规则，与下面rules 相对应
    ]
]

```

##### rules 加密规则：

[](#rules-加密规则)

```
[
    //键值对应field中的rule
    'default' => [
        'pre_len' => 0, //加密字段前置保留位数，适配一些模糊查询
        'tail_len' => 0, //加密字段后置保留位数，目前并不管用
        'encryption_field_len' => 20000, //加密位数，目前根据前置位数+加密位数来决定加密成什么样子
    ],
]

```

#### 3.将数据同步成加密数据的加密脚本

[](#3将数据同步成加密数据的加密脚本)

```
php artisan sys:filed-encryption

```

可以根据自己的需求改写，脚本所在位置
vendor/chuyubo/field\_encryption/src/Command/SysCommand.php
原理：生成临时表进行插入加密后的数据然后关联替换

#### 4.最终加密后效果：

[](#4最终加密后效果)

field\_NResPuJT+vjlfgspUqNINah5PsJwwJ2yA6shz9WsSSS=\_encryption0,0
解析：'field \_'为前置加密标识，'\_encryption'为后置加密标识, 0,0 前置保留位数0，后置保留位数0

###  Health Score

19

—

LowBetter than 10% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity2

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity40

Maturing project, gaining track record

 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

Unknown

Total

1

Last Release

1185d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/720b3675030fea4e27adea24ca1872716fec96b7a169de3bf8effbc9a76163cd?d=identicon)[YuboChu](/maintainers/YuboChu)

---

Top Contributors

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

### Embed Badge

![Health badge](/badges/chuyubo-field-encryption/health.svg)

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

###  Alternatives

[genealabs/laravel-governor

Managing policy and control in Laravel.

201262.8k](/packages/genealabs-laravel-governor)[bjorn-voesten/ciphersweet-for-laravel

1411.6k](/packages/bjorn-voesten-ciphersweet-for-laravel)

PHPackages © 2026

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