Content · Web Development · PHP

PHP openssl_open() Function: Usage and Examples

openssl_open decrypts a sealed hybrid-encryption envelope using the recipient private key and its wrapped session.

1 views Published Oct 17, 2026 Updated Oct 17, 2026 admin

openssl_open decrypts a sealed hybrid-encryption envelope using the recipient private key and its wrapped session key.

The examples explain the data format, expected results and the difference between this API and nearby cryptographic operations.

What It Does


The operation is intended for: openssl_open decrypts a sealed hybrid-encryption envelope using the recipient private key and its wrapped session key.

The OpenSSL extension must be enabled.

Syntax and Return Value


openssl_open(string $data, $output, string $encrypted_key, $private_key, string $cipher_algo, ?string $iv = ...): bool
ParameterPurpose
$data (string)The input data bytes. Use the exact original bytes when verifying.
$output (mixed)Use the PHP signature above to determine the exact expected input type; validate untrusted inputs before calling.
$encrypted_key (string)Use the PHP signature above to determine the exact expected input type; validate untrusted inputs before calling.
$private_key (mixed)The corresponding private key; never expose or log its PEM data.
$cipher_algo (string)Cipher algorithm choice; keep matched between encryption and decryption.
$iv (?string)Initialization vector or nonce whose length must match the selected cipher.

The declared return type is shown above. Errors can include false results or exceptions depending on this API: Store the IV, cipher name and recipient encrypted session key. Envelopes must still have authenticated integrity protection.

Example 1: Basic Operation


This older envelope example lacks built-in ciphertext authentication. Prefer an authenticated encryption scheme for new systems.

<?php
$private = openssl_pkey_new(['private_key_type' => OPENSSL_KEYTYPE_RSA, 'private_key_bits' => 2048]);
if ($private === false) { throw new RuntimeException('Key generation failed'); }
$details = openssl_pkey_get_details($private);
$public = $details['key'];
$csr = openssl_csr_new(['commonName' => 'test.example'], $private, ['digest_alg' => 'sha256']);
if ($csr === false) { throw new RuntimeException('CSR creation failed'); }
$cert = openssl_csr_sign($csr, null, $private, 1, ['digest_alg' => 'sha256']);
if ($cert === false) { throw new RuntimeException('Certificate creation failed'); }
$text = "annual report";
$iv = random_bytes(openssl_cipher_iv_length("aes-256-cbc"));
openssl_seal($text, $sealed, $keys, [$public], "aes-256-cbc", $iv);
$ok = openssl_open($sealed, $plain, $keys[0], $private, "aes-256-cbc", $iv);
var_dump($ok && $plain === $text);

Example 2: A Different Use Case


This older envelope example lacks built-in ciphertext authentication. Prefer an authenticated encryption scheme for new systems.

<?php
// Check the alternative case.
$private = openssl_pkey_new(['private_key_type' => OPENSSL_KEYTYPE_RSA, 'private_key_bits' => 2048]);
if ($private === false) { throw new RuntimeException('Key generation failed'); }
$details = openssl_pkey_get_details($private);
$public = $details['key'];
$csr = openssl_csr_new(['commonName' => 'test.example'], $private, ['digest_alg' => 'sha256']);
if ($csr === false) { throw new RuntimeException('CSR creation failed'); }
$cert = openssl_csr_sign($csr, null, $private, 1, ['digest_alg' => 'sha256']);
if ($cert === false) { throw new RuntimeException('Certificate creation failed'); }
$text = "invoice 42";
$iv = random_bytes(openssl_cipher_iv_length("aes-256-cbc"));
openssl_seal($text, $sealed, $keys, [$public], "aes-256-cbc", $iv);
$ok = openssl_open($sealed, $plain, $keys[0], $private, "aes-256-cbc", $iv);
var_dump($ok && $plain === $text);

Practical Example: Checking the Result


This older envelope example lacks built-in ciphertext authentication. Prefer an authenticated encryption scheme for new systems.

<?php
// Verify the expected behavior before using results.
$private = openssl_pkey_new(['private_key_type' => OPENSSL_KEYTYPE_RSA, 'private_key_bits' => 2048]);
if ($private === false) { throw new RuntimeException('Key generation failed'); }
$details = openssl_pkey_get_details($private);
$public = $details['key'];
$csr = openssl_csr_new(['commonName' => 'test.example'], $private, ['digest_alg' => 'sha256']);
if ($csr === false) { throw new RuntimeException('CSR creation failed'); }
$cert = openssl_csr_sign($csr, null, $private, 1, ['digest_alg' => 'sha256']);
if ($cert === false) { throw new RuntimeException('Certificate creation failed'); }
$text = "signed event";
$iv = random_bytes(openssl_cipher_iv_length("aes-256-cbc"));
openssl_seal($text, $sealed, $keys, [$public], "aes-256-cbc", $iv);
$ok = openssl_open($sealed, $plain, $keys[0], $private, "aes-256-cbc", $iv);
var_dump($ok && $plain === $text);

Important Behavior and Edge Cases


Store the IV, cipher name and recipient encrypted session key. Envelopes must still have authenticated integrity protection.

Store the IV, cipher name and recipient encrypted session key. Envelopes must still have authenticated integrity protection.

Comparison with Related Functions


ApproachKey distinction
openssl_openopenssl_open decrypts a sealed hybrid-encryption envelope using the recipient private key and its wrapped session key.
Related techniqueAuthenticated encryption plus explicit key wrapping gives greater control than older envelope modes.

Common Beginner Mistakes


MistakeCorrect approach
Confusing this operation with a neighboring cryptographic or security mechanismAuthenticated encryption plus explicit key wrapping gives greater control than older envelope modes.
Ignoring a security prerequisite or failure caseStore the IV, cipher name and recipient encrypted session key. Envelopes must still have authenticated integrity protection.

Key Takeaways


  • openssl_open decrypts a sealed hybrid-encryption envelope using the recipient private key and its wrapped session key.
  • Store the IV, cipher name and recipient encrypted session key. Envelopes must still have authenticated integrity protection.
  • Authenticated encryption plus explicit key wrapping gives greater control than older envelope modes.

Apply this behavior to a real application only after confirming the required extension, configuration and failure handling on the deployment runtime.

Share this resourceSend this page to someone who may find it useful.