定制 joacir/cake-aes 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

joacir/cake-aes

Composer 安装命令:

composer require joacir/cake-aes

包简介

Encrypt and decrypt fields using AES-256 plugin for CakePHP

README 文档

README

CakePHP plugin to encrypt/decrypt table fields using AES-256 algorithm on MySQL/MariaDB databases. This branch is for use with CakePHP 5.0+. Use branch "main-cake4" to use with CakePHP 4.0+

Install

Install it as require dependency:

composer require joacir/CakeAes

Setup

Enable the plugin in your Application.php or call

bin/cake plugin load CakeAes

Configure a new security hash in app_local.php:

'Security' => [
    'key' => '34601affc03a12d4b963b6123ab3afcb',
],
  • This will be the key used by the plugin for encryption.
  • NEVER use the same key for different apps.
  • Once generated, NEVER change the key.
  • The key must contain at least 32 digits in hexadecimal.

Change the type field of the fields you want to encrypt on your tables to a binary type:

char(20) -> blob
vachar(200) -> varbinary(200)
text -> blob
  • Only string types works. Use only VarBinary or Blob types.

Load the behavior on your table initialize() method:

$this->addBehavior('CakeAes.Encrypt', [
    'fields' => ['name', 'card', 'phone']
]);

Usage

To encrypt fields

Nothing is necessary, the EncryptBehavior do the encryption automatically when you set the fields in settings.

To decrypt fields, conditions, order and contain

The find() ou get() works without changes, in complex queries you can use decryptField() or decryptString().

$new = $this->Temps->get($temp->id, ['fields' => [
    'id',
    'name' => $this->Temps->decryptField('Temps.name')
]]);

$temp = $this->Temps->find()
    ->select(['name' => $this->Temps->decryptField('Temps.name')])
    ->where(['id' => 2])
    ->first();

You can use decryptEq() in conditions:

$temp = $this->Temps->find()
    ->select(['name' => $this->Temps->decryptField('Temps.name')])
    ->where([$this->Temps->decryptEq('Temps.name', $name)])
    ->first();

$temp = $this->Temps->find()
    ->select([
        'id',
        'name' => $this->Temps->decryptField('Temps.name')
    ])
    ->where([$this->Temps->decryptLike('Temps.name', '%Sa%')])
    ->first();

In updateAll() you can use encrypt() to encrypt:

$name = $this->Temps->encrypt("José");
$fields = ['name' => $name];
$conditions = [
    $this->Temps->decryptEq('Temps.name', 'Maria')
];
$this->Temps->updateAll($fields, $conditions);

To encrypt/decrypt a file

$imageFile = dirname(__FILE__) . DS . 'imagem.jpg';
$Temps->encryptFile($imageFile);

$imageFile = dirname(__FILE__) . DS . 'imagem_crypted.jpg';
$Temps->decryptFile($imageFile);

To decrypt a file in a controller

Load de Encrypt Component:

public function initialize(): void
{
    $this->loadComponent('CakeAes.Encrypt');
}

To decrypt and render the content:

$imageFile = dirname(__FILE__) . DS . 'imagem_encrypted.jpg';

return $this->Encrypt->decryptRender($imageFile);

To decrypt and download a file:

$imageFile = dirname(__FILE__) . DS . 'imagem_encrypted.jpg';

return $this->Encrypt->decryptDownload($imageFile);

统计信息

  • 总下载量: 12
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 2
  • 点击次数: 3
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 2
  • Watchers: 1
  • Forks: 2
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2022-01-14

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固