PHP openssl_pkey_export OpenSSL 函數

定義和用法

openssl_pkey_export - 將一個密鑰的可輸出表示轉換爲字符串

語法

openssl_pkey_export( mixed $key , string &$out [, string $passphrase [, array $configargs ]] )

openssl_pkey_export() 將 key 當作 PEM 編碼字符串導出並且將之保存到out (通過引用傳遞的)中。

注意: 必須安裝有效的 openssl.cnf 以保證此函數正確運行。

參數

參數 必需的 描述
key 祕鑰
out 輸出保存變量
passphrase 密鑰可以通過值爲passphrase的密碼來保護。
configargs configargs 可以用來調整導出流程,通過指定或者覆蓋openssl配置文件選項。參見 openssl_csr_new() 獲取更多關於 configargs 的信息。

 

返回值

成功時返回 TRUE, 或者在失敗時返回 FALSE。

示例

// Create the keypair
$res=openssl_pkey_new();
// Get private key
openssl_pkey_export($res, $privkey, "PassPhrase number 1" );
// Get public key
$pubkey=openssl_pkey_get_details($res);
$pubkey=$pubkey["key"];
//var_dump($privkey);
//var_dump($pubkey);
// Create the keypair
$res2=openssl_pkey_new();
// Get private key
openssl_pkey_export($res2, $privkey2, "This is a passPhrase *µà" );
// Get public key
$pubkey2=openssl_pkey_get_details($res2);
$pubkey2=$pubkey2["key"];
var_dump($privkey2);
var_dump($pubkey2);
$data = "Only I know the purple fox. Trala la !";
openssl_seal($data, $sealed, $ekeys, array($pubkey, $pubkey2));
var_dump("sealed");
var_dump(base64_encode($sealed));
var_dump(base64_encode($ekeys[0]));
var_dump(base64_encode($ekeys[1]));
// decrypt the data and store it in $open
if (openssl_open($sealed, $open, $ekeys[1], openssl_pkey_get_private  ($privkey2  ,"This is a passPhrase *µà" ) ) ) {
        echo "here is the opened data: ", $open;
} else {
        echo "failed to open data";
}

相關頁面

openssl_get_md_methods() - 獲取可用的摘要算法

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章