PHP8 openssl_seal

2024-03-13 11:24 更新

(PHP 4 >= 4.0.4, PHP 5, PHP 7, PHP 8)

openssl_seal — 密封 (加密) 數(shù)據(jù)

說(shuō)明

openssl_seal(
    string $data,
    string &$sealed_data,
    array &$encrypted_keys,
    array $public_key,
    string $cipher_algo,
    string &$iv = null
): int|false

openssl_seal() 使用隨機(jī)生成的密鑰和給定的 cipher_algo 方法密封(加密)data 數(shù)據(jù)。密鑰用與 public_key 中的標(biāo)識(shí)符相關(guān)聯(lián)的每個(gè)公共密鑰加密,并且每個(gè)加密密鑰在 encrypted_keys 中返回。這意味著一個(gè)人可以將密封的數(shù)據(jù)發(fā)送給多個(gè)接收者(如果一個(gè)人已經(jīng)獲得了他們的公鑰)。每個(gè)接收方都必須同時(shí)接收加密的數(shù)據(jù)和用接收方的公鑰加密的信封密鑰。

參數(shù) 

data

要密封的數(shù)據(jù)。

sealed_data

被密封后的數(shù)據(jù)。

encrypted_keys

已被加密的密鑰數(shù)組。

public_key

包含公鑰的 OpenSSLAsymmetricKey 實(shí)例數(shù)組。

cipher_algo

加密算法。

警告

默認(rèn)值('RC4')認(rèn)為不安全。強(qiáng)烈建議明確指定安全密碼方法。

iv

初始化向量。

返回值 

成功時(shí)返回密封后數(shù)據(jù)的長(zhǎng)度,錯(cuò)誤為 false。 如果密封后的數(shù)據(jù)成功地通過(guò) sealed_data 變量返回,那么信封密鑰也將會(huì)通過(guò) encrypted_keys 變量返回。

更新日志 

版本說(shuō)明
8.0.0public_key 現(xiàn)在接受 OpenSSLAsymmetricKey 實(shí)例 array;之前接受類(lèi)型 OpenSSL key 的 resource 數(shù)組。
8.0.0cipher_algo 不再是可選參數(shù)。
8.0.0iv 現(xiàn)在可為 null。

示例 

示例 #1 openssl_seal() 示例

<?php
// $data is assumed to contain the data to be sealed

// fetch public keys for our recipients, and ready them
$fp = fopen("/src/openssl-0.9.6/demos/maurice/cert.pem", "r");
$cert = fread($fp, 8192);
fclose($fp);
$pk1 = openssl_get_publickey($cert);
// Repeat for second recipient
$fp = fopen("/src/openssl-0.9.6/demos/sign/cert.pem", "r");
$cert = fread($fp, 8192);
fclose($fp);
$pk2 = openssl_get_publickey($cert);

// seal message, only owners of $pk1 and $pk2 can decrypt $sealed with keys
// $ekeys[0] and $ekeys[1] respectively.
openssl_seal($data, $sealed, $ekeys, array($pk1, $pk2));

// free the keys from memory
openssl_free_key($pk1);
openssl_free_key($pk2);
?>

參見(jiàn) 

  • openssl_open() - 打開(kāi)密封的數(shù)據(jù)


以上內(nèi)容是否對(duì)您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號(hào)
微信公眾號(hào)

編程獅公眾號(hào)