PHP compiled without OpenSSL support? Here's how you can call the openssl command-line utility to achieve the same goal:
<?php
// $sealed and $env_key are assumed to contain the sealed data
// and our envelope key, both given to us by the sealer.
// specify private key file and passphrase
$pkey_file='key.pem';
$pkey_pp='netsvc';
// call openssl to decrypt envelope key
$ph=proc_open('openssl rsautl -decrypt -inkey '.
escapeshellarg($pkey_file).' -passin fd:3',array(
0 => array('pipe','r'), // stdin < envelope key
1 => array('pipe','w'), // stdout > decoded envelope key
2 => STDERR,
3 => array('pipe','r'), // < passphrase
),$pipes);
// write envelope key
fwrite($pipes[0],$env_key);
fclose($pipes[0]);
// write private key passphrase
fwrite($pipes[3],$pkey_pp);
fclose($pipes[3]);
// read decoded key, convert to hexadecimal
$env_key='';
while(!feof($pipes[1])){
$env_key.=sprintf("%02x",ord(fgetc($pipes[1])));
}
fclose($pipes[1]);
if($xc=proc_close($ph)){
echo "Exit code: $xc\n";
}
// call openssl to decryp
$ph=proc_open('openssl rc4 -d -iv 0 -K '.$env_key,array(
0 => array('pipe','r'), // stdin < sealed data
1 => array('pipe','w'), // stdout > opened data
2 => STDERR,
),$pipes);
// write sealed data
fwrite($pipes[0],$sealed);
fclose($pipes[0]);
// read opened data
//$open=stream_get_contents($pipes[1]);
$open='';
while(!feof($pipes[1])){
$open.=fgets($pipes[1]);
}
fclose($pipes[1]);
if($xc=proc_close($ph)){
echo "Exit code: $xc\n";
}
// display the decrypted data
echo $open;
?>openssl_open
Почист и полокален преглед на PHP референцата, со задржана структура од PHP.net и подобра читливост за примери, секции и белешки.
openssl_open
Референца за `function.openssl-open.php` со подобрена типографија и навигација.
openssl_open
(PHP 4 >= 4.0.4, PHP 5, PHP 7, PHP 8)
openssl_open — Отвори запечатени податоци
= NULL
string
$data,Иницијализира контекст за инкрементално хеширање string
&$output,string
$encrypted_key,Иницијализира контекст за инкрементално хеширање OpenSSLAsymmetricKey|OpenSSLCertificate|array|string
$private_key,string
$cipher_algo,?string
$iv = null): bool
openssl_open() отвора (дешифрира) data користејќи клучен плик што се дешифрира од encrypted_key using
private_key. Дешифрирањето се врши со користење на
cipher_algo and iv. IV се бара само ако методот на шифрирање го бара тоа. Функцијата пополнува output со дешифрираните податоци. Клучот на пликот обично се генерира кога податоците се запечатуваат со јавен клуч што е поврзан со приватниот клуч. Види openssl_seal() Користење на PHP од командната линија
Параметри
data-
Запечатените податоци.
output-
Ако повикот е успешен, отворените податоци се враќаат во овој параметар.
encrypted_key-
Шифрираниот симетричен клуч што може да се дешифрира со
private_key. private_key-
Приватниот клуч што се користи за дешифрирање на
encrypted_key. cipher_algo-
Методот на шифрирање што се користи за дешифрирање на
data.Безбедност: стандардниот сет на знациСтандардната вредност за PHP верзии пред 8.0 е (
'RC4') што се смета за несигурно. Силно се препорачува експлицитно да се специфицира сигурен метод за шифрирање. iv-
Иницијализирачкиот вектор што се користи за дешифрирање на
data. Тој е задолжителен ако методот за шифрирање бара IV. Ова може да се дознае со повикување на openssl_cipher_iv_length() withcipher_algo.
Вратени вредности
Патеката до PHP скриптата што треба да се провери. true на успех или false при неуспех.
Дневник на промени
| Верзија | = NULL |
|---|---|
| 8.0.0 |
private_key прифаќа OpenSSLAsymmetricKey
or OpenSSLCertificate инстанца сега; претходно, а resource од тип OpenSSL key or OpenSSL X.509 CSR
беше прифатено.
|
| 8.0.0 |
cipher_algo веќе не е опционален параметар.
|
Примери
Пример #1 openssl_open() example
<?php
// $sealed, $env_key and $iv are assumed to contain the sealed data, our
// envelope key and IV. All given to us by the sealer.
// Fetch private key from file located in private_key.pem
$pkey = openssl_get_privatekey("file://private_key.pem");
// Decrypt the data and store it in $open
if (openssl_open($sealed, $open, $env_key, $pkey, 'AES256', $iv)) {
echo "Here is the opened data: ", $open;
} else {
echo "Failed to open data";
}
?>Белешки од корисници 2 забелешки
Example code, assume mycert.pem is a certificate containing both private and public key.
$cert = file_get_contents("mycert.pem");
$public = openssl_get_publickey($cert);
$private = openssl_get_privatekey($cert);
$data = "I'm a lumberjack and I'm okay.";
echo "Data before: {$data}\n";
openssl_seal($data, $cipher, $e, array($public));
echo "Ciphertext: {$cipher}\n";
openssl_open($cipher, $open, $e[0], $private);
echo "Decrypted: {$open}\n";