Content · Web Development · PHP

PHP openssl_private_encrypt() Function: Usage and Examples

openssl_private_encrypt applies a legacy low-level private-key RSA operation; it is not a modern digital-signature.

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

openssl_private_encrypt applies a legacy low-level private-key RSA operation; it is not a modern digital-signature interface.

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_private_encrypt applies a legacy low-level private-key RSA operation; it is not a modern digital-signature interface.

The OpenSSL extension must be enabled.

Syntax and Return Value


openssl_private_encrypt(string $data, $encrypted_data, $private_key, int $padding = ...): bool
ParameterPurpose
$data (string)The input data bytes. Use the exact original bytes when verifying.
$encrypted_data (mixed)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.
$padding (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: RSA block operations have strict padding and message-size limits. Do not use private_encrypt/public_decrypt as a replacement for standard signature APIs.

Example 1: Basic Operation


This is low-level RSA on short messages, not a complete secure encryption or signature protocol.

<?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_private_encrypt("annual report", $encoded, $private, OPENSSL_PKCS1_PADDING);
echo strlen($encoded);

Example 2: A Different Use Case


This is low-level RSA on short messages, not a complete secure encryption or signature protocol.

<?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_private_encrypt("invoice 42", $encoded, $private, OPENSSL_PKCS1_PADDING);
echo strlen($encoded);

Practical Example: Checking the Result


This is low-level RSA on short messages, not a complete secure encryption or signature protocol.

<?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_private_encrypt("signed event", $encoded, $private, OPENSSL_PKCS1_PADDING);
echo strlen($encoded);

Important Behavior and Edge Cases


RSA block operations have strict padding and message-size limits. Do not use private_encrypt/public_decrypt as a replacement for standard signature APIs.

RSA block operations have strict padding and message-size limits. Do not use private_encrypt/public_decrypt as a replacement for standard signature APIs.

Comparison with Related Functions


ApproachKey distinction
openssl_private_encryptopenssl_private_encrypt applies a legacy low-level private-key RSA operation; it is not a modern digital-signature interface.
Related techniquePrefer openssl_sign/openssl_verify for signatures and hybrid encryption for large payloads.

Common Beginner Mistakes


MistakeCorrect approach
Confusing this operation with a neighboring cryptographic or security mechanismPrefer openssl_sign/openssl_verify for signatures and hybrid encryption for large payloads.
Ignoring a security prerequisite or failure caseRSA block operations have strict padding and message-size limits. Do not use private_encrypt/public_decrypt as a replacement for standard signature APIs.

Key Takeaways


  • openssl_private_encrypt applies a legacy low-level private-key RSA operation; it is not a modern digital-signature interface.
  • RSA block operations have strict padding and message-size limits. Do not use private_encrypt/public_decrypt as a replacement for standard signature APIs.
  • Prefer openssl_sign/openssl_verify for signatures and hybrid encryption for large payloads.

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.