我正在尝试密封打开一个文件。加密失败,并产生以下错误。
error:0E06D06C:configuration file routines:NCONF_get_string:no value
下面是一个代码示例,可以重现这个问题。
// Generate key pair and keep them safe...
$key = openssl_pkey_new([
'private_key_bits' => 4096,
'private_key_type' => OPENSSL_KEYTYPE_EC,
'curve_name' => 'prime256v1',
]);
$privKey = null;
openssl_pkey_export($key, $privKey);
$pubKeyDetails = openssl_pkey_get_details($key);
$pubKey = $pubKeyDetails['key'];
openssl_free_key($key);
// Load the pubkey to encrypt
$key = openssl_pkey_get_public($pubKey);
$data = file_get_contents('of-some-pretty-large-file');
// ----- This here fails -----
if (openssl_seal($data, $sealed, $eKeys, [$key]) === false) {
echo "Encryption failed\n";
echo openssl_error_string() . "\n";
exit;
}
openssl_free_key($key);
$key = openssl_pkey_get_private($privKey);
if (openssl_open($sealed, $decryptedData, $eKeys[0], $key)) {
echo ($decryptedData === $data ? "Matched\n" : "Trash\n");
}
openssl_free_key($key);
错误信息
error:0E06D06C:configuration file routines:NCONF_get_string:no value
不是由 openssl_seal
但是,通过 openssl_pkey_new
. 这并不影响功能,也就是说,密钥已经成功生成,参见 此处 和 此处. 这也适用于发布的代码,它生成一个SEC1格式的私钥和一个X.509格式的公钥。
PHP方法 openssl_seal
是基于OpenSSL函数 EVP_SealInit
, EVP_SealUpdate
和 EVP_SealFinal
, 此处. 其中 相应 OpenSSL文档中描述了以下内容 笔记-节。
公钥必须是RSA,因为它是唯一支持密钥传输的OpenSSL公钥算法。
这意味着 openssl_seal
只适用于RSA或RSA密钥。如果在发布的代码中使用了以下内容。
$key = openssl_pkey_new([
'private_key_bits' => 4096,
'private_key_type' => OPENSSL_KEYTYPE_RSA
]);
解密和加密就会像预期的那样工作.