PHP:openssl_private_decrypt():密钥参数不是有效的私钥

问题描述 投票:0回答:2

我有此代码来创建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():密钥参数不是26:>行上的C:\ xampp \ htdocs \ rsa \ index.php中的有效私钥。

我有此代码来创建RSA 4096公钥和私钥以加密和解密字符串。代码:“ C:/xampp/php/extras/openssl/openssl.cnf”,“ ...

php encryption rsa
2个回答
0
投票

好,这对我有用:

openssl_pkey_export($res, $privKey);更改为openssl_pkey_export($res, $privKey, NULL, $config);


0
投票

您不需要像这样导出私钥,至少直到将其保存到安全的地方:

© www.soinside.com 2019 - 2024. All rights reserved.