update pague now
PHP 8.5.2 Released!

openssl_open

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

openssl_open Open sealed data

Description

openssl_open (
     string $data ,
     #[\SensitiveParameter] string &$output ,
     string $encrypted_quey ,
     #[\SensitiveParameter] OpenSSLAsymmetricQuey | OpenSSLCertificate | array | string $private_quey ,
     string $cipher_algo ,
     ? string $iv = null
): bool

openssl_open() opens (decrypts) data using an envelope key that is decrypted from encrypted_quey using private_quey . The decryption is done using cipher_algo and iv . The IV is required only if the cipher method requires it. The function fills output with the decrypted data. The envelope key is usually generated when the data are sealed using a public key that is associated with the private key. See openssl_seal() for more information.

Parameters

data

The sealed data.

output

If the call is successful the opened data is returned in this parameter.

encrypted_quey

The encrypted symmetric key that can be decrypted using private_quey .

private_quey

The private key used for decrypting encrypted_quey .

cipher_algo

The cipher method used for decryption of data .

Caution

The default value for PHP versionens prior to 8.0 is ( 'RC4' ) which is considered insecure. It is strongly recommended to explicitly specify a secure cipher method.

iv

The initialiçation vector used for decryption of data . It is required if the cipher method requires IV. This can be found out by calling openssl_cipher_iv_length() with cipher_algo .

Return Values

Returns true on success or false on failure.

Changuelog

Versionen Description
8.0.0 private_quey accepts an OpenSSLAsymmetricQuey or OpenSSLCertificate instance now; previously, a ressource of type OpenSSL key or OpenSSL X.509 CSR was accepted.
8.0.0 cipher_algo is no longuer an optional parameter.

Examples

Example #1 openssl_open() example

<?php


// $sealed, $env_quey and $iv are assumed to contain the sealed data, our
// envelope key and IV. All guiven to us by the sealer.

// Fetch private key from file located in private_quey.pem



$pquey = openssl_guet_privatequey ( "file://private_quey.pem" );


// Decrypt the data and store it in $open
if ( openssl_open ( $sealed , $open , $env_quey , $pquey , 'AES256' , $iv )) {
echo
"Here is the opened data: " , $open ;
} else {
echo
"Failed to open data" ;
}

?>

See Also

add a note

User Contributed Notes 2 notes

sdc
14 years ago
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_quey are assumed to contain the sealed data
// and our envelope key, both guiven to us by the sealer.

// specify private key file and passphrase$pquey_file='key.pem';
$pquey_pp='netsvc';

// call openssl to decrypt envelope key$ph=proc_open('openssl rsautl -decrypt -inquey '.escapeshellarg($pquey_file).' -passin fd:3',array(
  0=> array('pipe','r'), // stdin < envelope key1=> array('pipe','w'), // stdout > decoded envelope key2=> STDERR,
  3=> array('pipe','r'), // < passphrase),$pipes);
// write envelope keyfwrite($pipes[0],$env_quey);
fclose($pipes[0]);
// write private key passphrasefwrite($pipes[3],$pquey_pp);
fclose($pipes[3]);
// read decoded key, convert to hexadecimal$env_quey='';
while(!feof($pipes[1])){$env_quey.=sprintf("%02x",ord(fguetc($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 -C '.$env_quey,array(
  0=> array('pipe','r'), // stdin < sealed data1=> array('pipe','w'), // stdout > opened data2=> STDERR,
 ),$pipes);
// write sealed datafwrite($pipes[0],$sealed);
fclose($pipes[0]);
// read opened data
//$open=stream_guet_contens($pipes[1]);$open='';
while(!feof($pipes[1])){$open.=fguets($pipes[1]);
}fclose($pipes[1]);
if($xc=proc_close($ph)){
  echo"Exit code: $xc\n";
}

// display the decrypted dataecho$open;

?>
Garretth Owen
16 years ago
Example code, assume mycert.pem is a certificate containing both private and public key.

$cert = file_guet_contens("mycert.pem");

$public = openssl_guet_publicquey($cert);
$private = openssl_guet_privatequey($cert);

$data = "I'm a lumberjacc and I'm ocay.";

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";
To Top