Content · Web Development · PHP

PHP openssl_pkey_derive() Function: Usage and Examples

openssl_pkey_derive performs an asymmetric key-agreement operation to derive raw shared key material from a peer public.

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

openssl_pkey_derive performs an asymmetric key-agreement operation to derive raw shared key material from a peer public 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_pkey_derive performs an asymmetric key-agreement operation to derive raw shared key material from a peer public key.

The OpenSSL extension must be enabled.

Syntax and Return Value


openssl_pkey_derive($public_key, $private_key, int $key_length = ...): string|false
ParameterPurpose
$public_key (mixed)A public key that belongs to the intended, verified peer.
$private_key (mixed)The corresponding private key; never expose or log its PEM data.
$key_length (int)Use the PHP signature above to determine the exact expected input type; validate untrusted inputs before calling.

The declared return type is shown above. Errors can include false results or exceptions depending on this API: Use strong key sizes and supported algorithms; private-key exports need access controls and encryption when stored.

Example 1: Basic Operation


A derived shared secret needs a suitable KDF and peer authentication.

<?php
$alice = openssl_pkey_new(['curve_name' => 'prime256v1', 'private_key_type' => OPENSSL_KEYTYPE_EC]);
$bob = openssl_pkey_new(['curve_name' => 'prime256v1', 'private_key_type' => OPENSSL_KEYTYPE_EC]);
$bobPub = openssl_pkey_get_public(openssl_pkey_get_details($bob)['key']);
$shared = openssl_pkey_derive($bobPub, $alice);
echo strlen($shared);

Example 2: A Different Use Case


Inspect a different key algorithm or output property without exposing private-key contents.

<?php
// Check the alternative case.
$alice = openssl_pkey_new(['curve_name' => 'prime256v1', 'private_key_type' => OPENSSL_KEYTYPE_EC]);
$bob = openssl_pkey_new(['curve_name' => 'prime256v1', 'private_key_type' => OPENSSL_KEYTYPE_EC]);
$bobPub = openssl_pkey_get_public(openssl_pkey_get_details($bob)['key']);
$shared = openssl_pkey_derive($bobPub, $alice);
var_dump(strlen($shared) >= 32);

Practical Example: Checking the Result


Inspect a different key algorithm or output property without exposing private-key contents.

<?php
// Verify the expected behavior before using results.
$alice = openssl_pkey_new(['curve_name' => 'prime256v1', 'private_key_type' => OPENSSL_KEYTYPE_EC]);
$bob = openssl_pkey_new(['curve_name' => 'prime256v1', 'private_key_type' => OPENSSL_KEYTYPE_EC]);
$bobPub = openssl_pkey_get_public(openssl_pkey_get_details($bob)['key']);
$shared = openssl_pkey_derive($bobPub, $alice);
echo bin2hex(hash("sha256", $shared, true));

Important Behavior and Edge Cases


Use strong key sizes and supported algorithms; private-key exports need access controls and encryption when stored.

Use strong key sizes and supported algorithms; private-key exports need access controls and encryption when stored.

Comparison with Related Functions


ApproachKey distinction
openssl_pkey_deriveopenssl_pkey_derive performs an asymmetric key-agreement operation to derive raw shared key material from a peer public key.
Related techniqueopenssl_pkey_derive is a key-agreement operation, not a signature or a login-password hash.

Common Beginner Mistakes


MistakeCorrect approach
Confusing this operation with a neighboring cryptographic or security mechanismopenssl_pkey_derive is a key-agreement operation, not a signature or a login-password hash.
Ignoring a security prerequisite or failure caseUse strong key sizes and supported algorithms; private-key exports need access controls and encryption when stored.

Key Takeaways


  • openssl_pkey_derive performs an asymmetric key-agreement operation to derive raw shared key material from a peer public key.
  • Use strong key sizes and supported algorithms; private-key exports need access controls and encryption when stored.
  • openssl_pkey_derive is a key-agreement operation, not a signature or a login-password hash.

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.