bespredel/encryption-form
Composer 安装命令:
composer require bespredel/encryption-form
包简介
A Laravel package to securely encrypt form fields on the client-side using public key encryption and decrypt them on the server-side using the private key.
关键字:
README 文档
README
🔐 Protect sensitive form data before it leaves the browser
EncryptionForm is a lightweight solution for encrypting form data on the client side before sending it to the server.
It helps protect sensitive information such as passwords, personal data, and confidential inputs from interception.
✨ Features
- RSA Encryption: Uses
JSEncryptfor secure RSA encryption. - HTML Attribute Control: Specify which fields to encrypt using
data-encrypt="true". - Flexible Form Encryption: Target specific forms using
data-encrypt-formattribute. - Blade Directive: Automatically inject encryption scripts with
@encryptFormScripts. - Simple Key Management: Easily configure keys via
.envor generate new keys via artisan commands. - Zero Dependencies: No NPM required; all scripts are included in the package.
🚀 Use Cases
- Login & authentication forms
- Payment / sensitive input fields
- Personal data collection forms
- Secure admin panels
- API request protection
🧠 How It Works
- User fills the form
- Data is encrypted in the browser
- Encrypted payload is sent to server
- Server decrypts the data
- Application processes secure input
📦 Installation
-
Install the Package:
composer require bespredel/encryption-form
-
Publish Config and Scripts:
php artisan vendor:publish --tag=encryption-form
-
Add RSA Keys to
.env:ENCRYPTION_FORM_PUBLIC_KEY="-----BEGIN PUBLIC KEY-----...-----END PUBLIC KEY-----" ENCRYPTION_FORM_PRIVATE_KEY="-----BEGIN PRIVATE KEY-----...-----END PRIVATE KEY-----"
If you don't have keys, you can generate them using the following commands:
php artisan encryption-form:generate-keys
-
Include the Blade Directive in Your Template: Add
@encryptFormScriptsto your layout file or specific views where forms are encrypted.
🚀 Usage
Middleware
For auto decryption of form data, add the DecryptRequestFields middleware to your Kernel:
Add the middleware to your Kernel protected $middleware = [ // Other middleware \Bespredel\EncryptionForm\Middleware\DecryptRequestFields::class ]
or use it in a route:
Route::middleware('decrypt-form')->group(function () { // Your code })
HTML Form Example
In your Blade template:
<head> @encryptFormStyles @encryptFormScripts </head> <form data-encrypt-form action="/submit" method="POST"> <input type="text" name="name" data-encrypt="true" placeholder="Enter your name" /> <input type="email" name="email" data-encrypt="true" placeholder="Enter your email" /> <input type="text" name="address" placeholder="Enter your address" /> <div class="encrypt-form-status"></div> <!-- Optional element to display encryption operation status --> <button type="submit">Submit</button> </form>
- Add
data-encrypt-formto the<form>tag to enable encryption for this form. All supported form fields will be encrypted.- Use
data-encrypt="true"for fields that require encryption. All other fields will not be encrypted. - Use
data-encrypt="false"for fields that do not require encryption. All other fields will be encrypted.
- Use
Types of Fields Available for Encryption:
-
Input Fields:
- Supported types:
text,email,password,number,date, and similar. - Exceptions:
file,checkbox,radio,select.
- Supported types:
-
Textarea:
- Fully supported.
!!! It is important to note that the encrypted value will be longer than the original value, which may affect data length constraints.
Manual decrypting data on the server
Use the Decryptor class to decrypt data on the server:
use Bespredel\EncryptionForm\Services\Decryptor; $decryptor = new Decryptor(); $value = $request->input('name'); // Example for 'name' field $privateKey = config('encryption-form.private_key'); $prefix = config('encryption-form.prefix', 'ENCF:'); $decryptedValue = $decryptor->decryptValue($value, $privateKey, $prefix);
Or use dependency injection:
use Bespredel\EncryptionForm\Services\Contracts\DecryptorInterface; public function __construct(DecryptorInterface $decryptor) { $this->decryptor = $decryptor; } // In your method: $value = $request->input('name'); $privateKey = config('encryption-form.private_key'); $prefix = config('encryption-form.prefix', 'ENCF:'); $decryptedValue = $this->decryptor->decryptValue($value, $privateKey, $prefix);
Or use the openssl_private_decrypt function to decrypt data on the server:
$privateKey = config('encryption-form.private_key'); $encryptedData = $request->input('name'); // Example for 'name' field $decryptedData = null; $decodedValue = base64_decode((string)str($encryptedData)->after('ENCF:'), true); openssl_private_decrypt($decodedValue, $decryptedData, $privateKey); echo $decryptedData; // Output the decrypted value
⚡ Commands
Generate New RSA Keys
To generate a new pair of RSA keys:
php artisan encryption-form:generate-keys
This will update the keys in your .env file.
Keys are saved as escaped multiline values to keep the .env file parse-safe.
⚙️ Configuration
Config File:
config/encryption-form.php
return [ 'public_key' => env('ENCRYPTION_FORM_PUBLIC_KEY'), // Public key, required 'private_key' => env('ENCRYPTION_FORM_PRIVATE_KEY'), // Private key, required 'prefix' => env('ENCRYPTION_FORM_PREFIX', 'ENCF:'), // Field value prefix, needed for optimization to find encrypted values, default: 'ENCF:' 'strict_mode' => env('ENCRYPTION_FORM_STRICT_MODE', false), // Throw DecryptionException when encrypted fields fail to decrypt 'key_rotation' => [ // Key automatic rotation configuration 'enabled' => env('ENCRYPTION_FORM_KEY_ROTATION_ENABLED', false), // Enable key rotation 'cron_expression' => '0 0 * * *', // Cron expression for key rotation ], 'skip_for_ips' => [ // Skip encryption for specific IP addresses //'127.0.0.1', ], ];
Quality checks
composer lint
composer analyse
composer test
Key Rotation via Scheduler
You can schedule automatic key rotation via the key_rotation key in the config file.:
return [ ... 'key_rotation' => [ 'enabled' => env('ENCRYPTION_FORM_KEY_ROTATION_ENABLED', false), 'cron_expression' => '0 0 * * *', ], ];
🤝 Contributing
- Fork the repository.
- Create your feature branch:
git checkout -b feature/my-feature. - Commit your changes:
git commit -m 'Add some feature'. - Push to the branch:
git push origin feature/my-feature. - Open a pull request.
🛡 Security
PLEASE DON'T DISCLOSE SECURITY-RELATED ISSUES PUBLICLY.
If you discover any security related issues, please email hello@bespredel.name instead of using the issue tracker.
🙏 Acknowledgements
I would like to thank the authors and contributors of the JSEncrypt library for providing a secure RSA encryption solution for client-side data encryption.
📄 License
This package is open-source software licensed under the MIT license.
⭐ Support
If you find this project useful, give it a star ⭐
bespredel/encryption-form 适用场景与选型建议
bespredel/encryption-form 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 32 次下载、GitHub Stars 达 1, 最近一次更新时间为 2024 年 12 月 13 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「security」 「encryption」 「laravel」 「openssl」 「data-privacy」 「rsa-encryption」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 bespredel/encryption-form 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 bespredel/encryption-form 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 bespredel/encryption-form 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
A simple PHP class to encrypt a string and decrypt an encrypted string
A package for automatically encrypting and decrypting Eloquent attributes in Laravel 5.5+, based on configuration settings.
Provide a way to secure accesses to all routes of an symfony application.
Encryption with AES-256 and HMAC-SHA256
Allows installation of Laravel where the PHP Mcrypt extension is not available. Provides encryption using OpenSSL, or by disabling encryption entierly.
High-level cryptographic primitives and security utilities for Maatify systems including password hashing, reversible encryption, HKDF key derivation, and key rotation.
统计信息
- 总下载量: 32
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 2
- 点击次数: 30
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2024-12-13