openssl_encrypt

FunctionParams changed in PHP 8.6

Encrypts data.

PHP 5
PHP 7
PHP 8.0
Improved
PHP 8.1
PHP 8.2
Changed
PHP 8.3
PHP 8.4
PHP 8.5
PHP 8.6
Improved

openssl_encrypt Function synopsis

openssl_encrypt(
    string $data,
    string $cipher_algo,
    string $passphrase,
    int $options = 0,
    string $iv = "",
    &$tag = null,
    ?string $aad = "",
    int $tag_length = 16
  ): string|false

Parameters

$data

Typestring

The plaintext message data to be encrypted.

$cipher_algo

Typestring

The cipher method. For a list of available cipher methods, use openssl_get_cipher_methods.

$passphrase

Typestring

The passphrase. If the passphrase is shorter than expected, it is silently padded with NUL characters; if the passphrase is longer than expected, it is silently truncated.

$options

OptionalTypeintDefault value0

$options is a bitwise disjunction of the flags OPENSSL_RAW_DATA, and OPENSSL_ZERO_PADDING or OPENSSL_DONT_ZERO_PAD_KEY.

$iv

OptionalTypestringDefault value""

A non-null Initialization Vector. If the IV is shorter than expected, it is padded with NUL characters and warning is emitted; if the passphrase is longer than expected, it is truncated and warning is emitted.

$tag

Passed by reference, OptionalDefault valuenull

The authentication tag passed by reference when using AEAD cipher mode (GCM or CCM).

$aad

OptionalType?stringDefault value""

Additional authenticated data.

$tag_length

OptionalTypeintDefault value16

The length of the authentication $tag. Its value can be between 4 and 16 for GCM mode.

Note that the parameter $tag is passed by reference, and contains additional/updated context data that may be useful.

Return value

Typestring|false

Returns the encrypted string on success or false on failure.

Changes to the openssl_encrypt Function

PHP 8.6

  • Parameter type changed for parameter #7 ($aad): string to ?string
  openssl_encrypt(
      string $data,
      string $cipher_algo,
      string $passphrase,
      int $options = 0,
      string $iv = "",
      &$tag = null,
-     string $aad = "",
+     ?string $aad = "",
      int $tag_length = 16
    ): string|false

PHP 8.2

PHP 8.0

  • Return type added: string|false
  • Parameter type added for parameter #1 ($data): string
  • Parameter name of parameter #2 changed: $method to $cipher_algo
  • Parameter type added for parameter #2 ($cipher_algo): string
  • Parameter name of parameter #3 changed: $password to $passphrase
  • Parameter type added for parameter #3 ($passphrase): string
  • Parameter type added for parameter #4 ($options): int
  • Parameter default value added for position #4 ($options): 0
  • Parameter type added for parameter #5 ($iv): string
  • Parameter default value added for position #5 ($iv): ""
  • Parameter default value added for position #6 ($tag): null
  • Parameter type added for parameter #7 ($aad): string
  • Parameter default value added for position #7 ($aad): ""
  • Parameter type added for parameter #8 ($tag_length): int
  • Parameter default value added for position #8 ($tag_length): 16
  openssl_encrypt(
-     $data,
+     string $data,
-     $method,
+     string $cipher_algo,
-     $password,
+     string $passphrase,
-     $options,
+     int $options = 0,
-     $iv,
+     string $iv = "",
-     &$tag,
+     &$tag = null,
-     $aad,
+     string $aad = "",
-     $tag_length
+     int $tag_length = 16
-   )
+   ): string|false

PHP 7.1

  • Optional parameter added - #6: &$tag
  • Optional parameter added - #7: $aad
  • Optional parameter added - #8: $tag_length
 openssl_encrypt(
     $data,
     $method,
     $password,
     $options,
-    $iv
+    $iv,
+    &$tag,
+    $aad,
+    $tag_length
   )

PHP 5.4

  • Parameter name of parameter #4 changed: $raw_output to $options
  openssl_encrypt(
      $data,
      $method,
      $password,
-     $raw_output,
+     $options,
      $iv
    )

openssl_encrypt Function Availability

PHP VersionAvailability
PHP 8.6Upcoming Release Yes
PHP 8.5Supported (Latest) Yes
PHP 8.4Supported Yes
PHP 8.3Security-Fixes Only Yes
PHP 8.2Security-Fixes Only Yes
PHP 8.1Unsupported Yes
PHP 8.0Unsupported Yes
PHP 7.4Unsupported Yes
PHP 7.3Unsupported Yes
PHP 7.2Unsupported Yes
PHP 7.1Unsupported Yes
PHP 7.0Unsupported Yes
PHP 5.6Unsupported Yes
PHP 5.5Unsupported Yes
PHP 5.4Unsupported Yes
PHP 5.3Unsupported Yes