$publicKey = "../ssh/public/pub"; $plaintext = "要加密的字符串";
$pubKey = openssl_pkey_get_public($publicKey);
openssl_public_encrypt($plaintext, $encrypted, $pubKey);
echo $encrypted; //encrypted string
以上代码产生以下错误
openssl_public_encrypt() [http://php.net/function.openssl-public-encrypt]: key 参数不是有效的公钥 [APP/controllers/supportservice_controller.php,第 144 行]
我使用 openssl 创建了 key :
生成一个 1024 位的 rsa 私钥,要求一个密码来加密它并保存到文件 openssl genrsa -des3 -out/path/to/privatekey 1024
为私钥生成公钥并保存到文件
openssl rsa -in/path/to/privatekey -pubout -out/path/to/publickey
最佳答案
在我的例子中,我将公钥拆分成多行,解决了问题。
PHP 版本 7.1.17
$publicKey = "-----BEGIN PUBLIC KEY-----\n" . wordwrap($publicKey, 64, "\n", true) . "\n-----END PUBLIC KEY-----";
$str = "str to be encrypted";
$opensslPublicEncrypt = openssl_public_encrypt($str, $encrypted, $publicKey);
关于php - get the key parameter is not a valid public key 错误在 openssl_public_encrypt(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18842403/