PHP 配置文件敏感信息加密保护

```html PHP 配置文件敏感信息加密保护实践

PHP 配置文件敏感信息加密保护实践

在 Web 开发中,数据库密码、API 密钥、JWT 秘钥等敏感信息常存于配置文件(如 .envconfig/database.php)。若直接明文存储,一旦源码泄露或服务器被入侵,将造成严重安全风险。本文介绍一种轻量、可靠且符合 PHP 最佳实践的加密保护方案。

核心思路:运行时解密,不硬编码密钥

避免将加密密钥写死在代码中。推荐使用 **环境变量 + OpenSSL 加密** 组合: ✅ 密钥通过操作系统环境变量传入(如 ENCRYPTION_KEY) ✅ 敏感值使用 AES-256-CBC 加密后存入配置文件 ✅ 应用启动时动态解密,内存中仅保留明文(生命周期可控)

示例:加密与解密工具类

<?php
// src/Security/ConfigCipher.php
class ConfigCipher
{
    private string $cipher = 'AES-256-CBC';
    
    public function __construct(private string $key)
    {
        if (strlen($this->key) !== 32) {
            throw new InvalidArgumentException('Encryption key must be exactly 32 bytes.');
        }
    }

    public function encrypt(string $plaintext): string
    {
        $iv = openssl_random_pseudo_bytes(openssl_cipher_iv_length($this->cipher));
        $encrypted = openssl_encrypt($plaintext, $this->cipher, $this->key, 0, $iv);
        return base64_encode($iv . $encrypted);
    }

    public function decrypt(string $encrypted): string
    {
        $data = base64_decode($encrypted);
        $ivLen = openssl_cipher_iv_length($this->cipher);
        $iv = substr($data, 0, $ivLen);
        $ciphertext = substr($data, $ivLen);
        $decrypted = openssl_decrypt($ciphertext, $this->cipher, $this->key, 0, $iv);
        if ($decrypted === false) {
            throw new RuntimeException('Decryption failed: ' . openssl_error_string());
        }
        return $decrypted;
    }
}

// 使用示例(加密阶段 —— 仅执行一次)
// $cipher = new ConfigCipher($_ENV['ENCRYPTION_KEY'] ?? '');
// $encryptedDbPass = $cipher->encrypt('my-secret-password');
// echo "Encrypted: {$encryptedDbPass}\n"; // 存入 .env 或 config.php
?>

配置文件集成(以 .env 为例)

.env 文件(明文不可见敏感值):

DB_PASSWORD=U2FsdGVkX1+YzQ...[base64-encoded-ciphertext]
ENCRYPTION_KEY=32-byte-key-from-env-or-vault

加载配置时自动解密:

<?php
// config/database.php
$key = $_SERVER['ENCRYPTION_KEY'] ?? getenv('ENCRYPTION_KEY');
if (!$key) {
    throw new RuntimeException('ENCRYPTION_KEY not set in environment.');
}
$cipher = new ConfigCipher($key);

return [
    'database' => 'app_db',
    'username' => 'app_user',
    'password' => $cipher->decrypt($_ENV['DB_PASSWORD'] ?? ''),
];
?>
⚠️ 重要提醒: • 将 ENCRYPTION_KEY 严格限制在生产环境变量中(如 Docker secrets、Kubernetes Secrets 或 systemd env files),绝不提交到 Git; • 加密后的密文可安全纳入版本控制; • 建议配合 phpdotenv v8+ 的 Dotenv::createImmutable() 提升加载安全性。

安全无捷径——加密配置不是银弹,而是纵深防御的一环。结合最小权限原则、定期密钥轮换与日志脱敏,方能筑牢 PHP 应用的第一道防线。

```