亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

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 關注
  • 565 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號