php - PHP:openssl_private_decrypt():key 参数不是有效的私钥
问题描述
我有这个代码来创建一个 RSA 4096 公钥和私钥来加密和解密一个字符串。
代码:
<?php
$config = array(
"config" => "C:/xampp/php/extras/openssl/openssl.cnf",
"private_key_bits" => 4096,
"private_key_type" => OPENSSL_KEYTYPE_RSA
);
// Create the private and public key
$res = openssl_pkey_new($config);
// Extract the private key from $res to $privKey
openssl_pkey_export($res, $privKey);
// Extract the public key from $res to $pubKey
$pubKey = openssl_pkey_get_details($res);
$pubKey = $pubKey["key"];
$data = 'Hello, World!';
// Encrypt the data to $encrypted using the public key
openssl_public_encrypt($data, $encrypted, $pubKey);
echo $encrypted;
// Decrypt the data using the private key and store the results in $decrypted
openssl_private_decrypt($encrypted, $decrypted, $privKey);
echo $decrypted;
?>
它创建密钥,加密data
字符串 ( Hello, World!
),但是当尝试解密encrypted
字符串时,会发生错误:
警告:openssl_private_decrypt(): key parameter is not a valid private key in C:\xampp\htdocs\rsa\index.php on line 26
解决方案
好的,这对我有用:
更改openssl_pkey_export($res, $privKey);
为openssl_pkey_export($res, $privKey, NULL, $config);
。
推荐阅读
- java - 将对象列表转换为数组。获取 ArrayStoreException
- java - Elasticsearch 集成测试和 spring-test 不能很好地协同工作 - AccessControlException
- c# - mvc中的view不能第二次渲染html
- css - Angular EPUB 框架的 iOS 移动浏览器中的 domainurl/stylesheet.css 取消错误
- go - Gorutine 从 main 返回后没有完成执行。为什么?
- xamarin.android - 如何强制项目进入屏幕上的固定位置?
- jenkins - 使用阶段视图或 Jenkins CLI 执行 Jenkins 管道的特定阶段
- docker - 无法绑定80端口
- ios - 从 iOS 上的 Flutter 应用打开 youtube 应用
- python - 如何在 Flask 和 HTML 中忽略 render_template 参数中的特殊字符