# 警告：openssl_private_decrypt()：密钥参数不是有效的私钥，位于 /var/www/app/src/Crypto/Decryptor.php 第 34 行

- **ID:** `php/openssl-invalid-key-format`
- **领域:** php
- **类别:** runtime_error
- **验证级别:** ai_generated
- **修复率:** 82%

## 根因

提供给 openssl_private_decrypt() 的私钥格式错误，缺少头部/尾部标记，或使用了不支持的格式（如 PKCS#1 而非 PKCS#8）。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| PHP 8.1 | active | — | — |
| PHP 8.2 | active | — | — |
| PHP 8.3 | active | — | — |
| OpenSSL 1.1.1 | active | — | — |
| OpenSSL 3.0 | active | — | — |

## 解决方案

1. ```
   使用 OpenSSL 命令将私钥从 PKCS#1 转换为 PKCS#8 格式：`openssl pkcs8 -topk8 -inform PEM -outform PEM -in private.pem -out private_pkcs8.pem -nocrypt`
   ```
2. ```
   确保密钥字符串包含正确的头部和尾部（如 '-----BEGIN PRIVATE KEY-----' 和 '-----END PRIVATE KEY-----'），且没有多余的空格或换行。
   ```
3. ```
   在传递给解密函数之前，使用 `openssl_pkey_get_private()` 验证密钥；如果返回 false，使用 `openssl_error_string()` 记录 OpenSSL 错误。
   ```

## 无效尝试

- **** — The extension is already installed and functional; the error is about key content, not extension availability. (70% 失败率)
- **** — Memory and time settings do not affect key parsing logic. (90% 失败率)
- **** — The error occurs after the key is loaded; permissions affect file reading, not key validation. (80% 失败率)
