https://github.com/aashakib/cakephp-encrypt-decrypt
A CakePHP library to encrypt and decrypt data.
https://github.com/aashakib/cakephp-encrypt-decrypt
cakephp cakephp-plugin cakephp3 cakephp4 decrypt decryption encrypt encryption encryption-decryption
Last synced: about 1 month ago
JSON representation
A CakePHP library to encrypt and decrypt data.
- Host: GitHub
- URL: https://github.com/aashakib/cakephp-encrypt-decrypt
- Owner: aashakib
- License: mit
- Created: 2021-07-10T18:07:44.000Z (over 4 years ago)
- Default Branch: master
- Last Pushed: 2025-03-18T03:48:57.000Z (12 months ago)
- Last Synced: 2025-12-26T15:20:26.083Z (2 months ago)
- Topics: cakephp, cakephp-plugin, cakephp3, cakephp4, decrypt, decryption, encrypt, encryption, encryption-decryption
- Language: PHP
- Homepage:
- Size: 12.7 KB
- Stars: 2
- Watchers: 1
- Forks: 1
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# CakePHP Encrypt Decrypt
A CakePHP library to encrypt and decrypt data.
### Features
- Encrypt data when saving and decrypt data when fetching data from database.
- Encrypt & decrypt historical data.
### Install
Via Composer
For CakePHP 5 & CakePHP 4:
`composer require shakib/cakephp-encrypt-decrypt`
For CakePHP 3.4 and above versions for CakePHP 3.x:
`composer require shakib/cakephp-encrypt-decrypt:~1.1`
For CakePHP <=3.3:
`composer require shakib/cakephp-encrypt-decrypt:1.0`
### Setup
Add the type in `bootstrap.php`
``` php
TypeFactory::map('encrypted', 'EncryptDecrypt\Database\Type\EncryptType');
```
Add config value in `config\app.php`
``` php
'Security' => [
'encryption_key' => env('ENCRYPTION_KEY', 'YOUR-KEY'),
]
```
### Uses
Table structure: Use `BLOB \ VARBINARY` type for those columns you are want to be encrypted. Such as:
``` sql
CREATE TABLE `accounts`(
`id` INT NOT NULL AUTO_INCREMENT,
`full_name` VARCHAR(100) NOT NULL,
`account_number` VARBINARY(255) NOT NULL,
`email` VARBINARY(255) NOT NULL,
`created` DATETIME NOT NULL,
`modified` DATETIME NULL,
PRIMARY KEY(`id`)
) ENGINE = InnoDB;
```
Map all columns in your Table class.
``` php
use Cake\ORM\Table;
use Cake\Database\Schema\TableSchemaInterface;
use EncryptDecrypt\Traits\EncryptDecrypt;
class AccountsTable extends Table
{
use EncryptDecrypt;
/**
* @param TableSchemaInterface $schema
* @return TableSchemaInterface
*/
protected function _initializeSchema(TableSchemaInterface $schema): TableSchemaInterface
{
$schema->setColumnType('account_number', 'encrypted');
$schema->setColumnType('email', 'encrypted');
return $schema;
}
}
```
To encrypt or decrypt historical data, add this method in your table class and run
``` php
public function encryptDecryptAllData()
{
// columns that are in plain text
$sourceColumns = ['column1', 'column2'];
// columns that need to be encrypted / decrypted
$destinationColumns = ['column3', 'column4'];
return $this->encryptAll($this, $sourceColumns, $destinationColumns);
}
```
To search any data, you can use search text in where clause
``` php
$query->where(['email' => 'test@domain.com', 'account_number' => 'xxxxxxxxx']);
}
```