Content · Web Development · PHP

PHP openssl_pkey_export() Function: Usage and Examples

openssl_pkey_export exports a private key in PEM format, optionally encrypting the exported key with a.

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

openssl_pkey_export exports a private key in PEM format, optionally encrypting the exported key with a passphrase.

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_export exports a private key in PEM format, optionally encrypting the exported key with a passphrase.

The OpenSSL extension must be enabled.

Syntax and Return Value


openssl_pkey_export($key, $output, ?string $passphrase = ..., ?array $options = ...): bool
ParameterPurpose
$key (mixed)Raw key bytes in the required algorithm-specific length; protect them from disclosure.
$output (mixed)Use the PHP signature above to determine the exact expected input type; validate untrusted inputs before calling.
$passphrase (?string)Use the PHP signature above to determine the exact expected input type; validate untrusted inputs before calling.
$options (?array)Driver or algorithm-specific options; avoid unchecked user-controlled settings.

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


Export private keys only into protected storage with a strong secret.

<?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'); }
openssl_pkey_export($private, $pem, 'example-passphrase');
echo str_starts_with($pem, '-----BEGIN ENCRYPTED PRIVATE KEY-----') ? 'encrypted PEM' : 'check export';

Example 2: A Different Use Case


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

<?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'); }
openssl_pkey_export($private, $pem, 'example-passphrase');
echo strlen($pem) . " PEM bytes";

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.
$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'); }
openssl_pkey_export($private, $pem, 'example-passphrase');
var_dump(str_contains($pem, "PRIVATE KEY"));

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_exportopenssl_pkey_export exports a private key in PEM format, optionally encrypting the exported key with a passphrase.
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_export exports a private key in PEM format, optionally encrypting the exported key with a passphrase.
  • 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.