php
runtime_error
ai_generated
true
警告:openssl_private_decrypt():密钥参数不是有效的私钥,位于 /var/www/app/src/Crypto/Decryptor.php 第 34 行
Warning: openssl_private_decrypt(): key parameter is not a valid private key in /var/www/app/src/Crypto/Decryptor.php on line 34
ID: php/openssl-invalid-key-format
82%修复率
85%置信度
1证据数
2024-03-12首次发现
版本兼容性
| 版本 | 状态 | 引入 | 弃用 | 备注 |
|---|---|---|---|---|
| PHP 8.1 | active | — | — | — |
| PHP 8.2 | active | — | — | — |
| PHP 8.3 | active | — | — | — |
| OpenSSL 1.1.1 | active | — | — | — |
| OpenSSL 3.0 | active | — | — | — |
根因分析
提供给 openssl_private_decrypt() 的私钥格式错误,缺少头部/尾部标记,或使用了不支持的格式(如 PKCS#1 而非 PKCS#8)。
English
The private key provided to openssl_private_decrypt() is malformed, missing header/footer, or in an unsupported format (e.g., PKCS#1 instead of PKCS#8).
官方文档
https://www.php.net/manual/en/function.openssl-private-decrypt.php解决方案
-
使用 OpenSSL 命令将私钥从 PKCS#1 转换为 PKCS#8 格式:`openssl pkcs8 -topk8 -inform PEM -outform PEM -in private.pem -out private_pkcs8.pem -nocrypt`
-
确保密钥字符串包含正确的头部和尾部(如 '-----BEGIN PRIVATE KEY-----' 和 '-----END PRIVATE KEY-----'),且没有多余的空格或换行。
-
在传递给解密函数之前,使用 `openssl_pkey_get_private()` 验证密钥;如果返回 false,使用 `openssl_error_string()` 记录 OpenSSL 错误。
无效尝试
常见但无效的做法:
-
70% 失败
The extension is already installed and functional; the error is about key content, not extension availability.
-
90% 失败
Memory and time settings do not affect key parsing logic.
-
80% 失败
The error occurs after the key is loaded; permissions affect file reading, not key validation.