Content · Web Development · PHP

PHP openssl_pkey_new() Function: Usage and Examples

openssl_pkey_new generates a new asymmetric private key with an explicit key type and key.

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

openssl_pkey_new generates a new asymmetric private key with an explicit key type and key parameters.

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_new generates a new asymmetric private key with an explicit key type and key parameters.

The OpenSSL extension must be enabled.

Syntax and Return Value


openssl_pkey_new(?array $options = ...): OpenSSLAsymmetricKey|false
ParameterPurpose
$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


Use modern recommended algorithms for deployment and protect the private key.

<?php
$key = openssl_pkey_new(['private_key_type' => OPENSSL_KEYTYPE_RSA, 'private_key_bits' => 2048]);
var_dump($key instanceof OpenSSLAsymmetricKey);

Example 2: A Different Use Case


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

<?php
$key = openssl_pkey_new(['private_key_type' => OPENSSL_KEYTYPE_EC, 'curve_name' => 'prime256v1']);
var_dump($key instanceof OpenSSLAsymmetricKey);

Practical Example: Checking the Result


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

<?php
$key = openssl_pkey_new(['private_key_type' => OPENSSL_KEYTYPE_RSA, 'private_key_bits' => 2048]);
echo openssl_pkey_get_details($key)['bits'];

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_newopenssl_pkey_new generates a new asymmetric private key with an explicit key type and key parameters.
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_new generates a new asymmetric private key with an explicit key type and key parameters.
  • 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.