bespredel/encryption-form 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

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

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

Readme EN Readme RU GitHub license Downloads

Latest Version Latest Version Packagist PHP from Packagist Laravel Version

🔐 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 JSEncrypt for secure RSA encryption.
  • HTML Attribute Control: Specify which fields to encrypt using data-encrypt="true".
  • Flexible Form Encryption: Target specific forms using data-encrypt-form attribute.
  • Blade Directive: Automatically inject encryption scripts with @encryptFormScripts.
  • Simple Key Management: Easily configure keys via .env or 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

  1. User fills the form
  2. Data is encrypted in the browser
  3. Encrypted payload is sent to server
  4. Server decrypts the data
  5. Application processes secure input

📦 Installation

  1. Install the Package:

    composer require bespredel/encryption-form
  2. Publish Config and Scripts:

    php artisan vendor:publish --tag=encryption-form
  3. 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
  4. Include the Blade Directive in Your Template: Add @encryptFormScripts to 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-form to 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.

Types of Fields Available for Encryption:

  • Input Fields:

    • Supported types: text, email, password, number, date, and similar.
    • Exceptions: file, checkbox, radio, select.
  • 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

  1. Fork the repository.
  2. Create your feature branch: git checkout -b feature/my-feature.
  3. Commit your changes: git commit -m 'Add some feature'.
  4. Push to the branch: git push origin feature/my-feature.
  5. 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 我们能提供哪些服务?
定制开发 / 二次开发

基于 bespredel/encryption-form 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2024-12-13