为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

PHP
慕森卡 2022-08-05 10:17:03
我有这个代码来创建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 $privKeyopenssl_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 keyopenssl_public_encrypt($data, $encrypted, $pubKey);echo $encrypted;// Decrypt the data using the private key and store the results in $decryptedopenssl_private_decrypt($encrypted, $decrypted, $privKey);echo $decrypted;?>它创建密钥,加密字符串 (),但当尝试解密字符串时,会发生错误:dataHello, World!encrypted警告:openssl_private_decrypt(): 在第 26 行的 C:\xampp\htdocs\rsa\index.php 密钥参数不是有效的私钥
查看完整描述

2 回答

?
慕莱坞森

TA贡献1810条经验 获得超4个赞

好吧,这对我有用:

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


查看完整回答
反对 回复 2022-08-05
?
RISEBY

TA贡献1856条经验 获得超5个赞

您不需要像这样导出密钥私有,至少在将其保存在安全的地方之前是这样:


$config = array(

    "private_key_bits" => 4096,

    "private_key_type" => OPENSSL_KEYTYPE_RSA

);


$pki     = openssl_pkey_new($config);

$public  = openssl_pkey_get_public(

    openssl_pkey_get_details($pki)['key']

); // why on earth did they implement it like this? so clunky.

$private = openssl_pkey_get_private($pki);


$data = 'Hello, World!';


openssl_public_encrypt($data, $encrypted, $public);

openssl_private_decrypt($encrypted, $decrypted, $private);


var_dump(

    bin2hex($encrypted),

    $decrypted

);


查看完整回答
反对 回复 2022-08-05
  • 2 回答
  • 0 关注
  • 571 浏览

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号