PHP Reference ยท Web and Security
PHP openssl_encrypt() and openssl_decrypt()
Reference for PHP openssl_encrypt() and openssl_decrypt(): encrypt data with AES-256-GCM, handle IVs and authentication tags, and avoid common mistakes.
openssl_encrypt() encrypts a string with a symmetric cipher such as AES, and openssl_decrypt() reverses it. Both belong to the OpenSSL extension, which is common but optional: check extension_loaded('openssl') if your code must run on unknown servers.
For new code, use an authenticated mode such as aes-256-gcm. It encrypts and also produces an authentication tag, so any change to the stored data is detected when you decrypt. Older modes like aes-256-cbc encrypt only; they do not tell you if the data was tampered with.
$cipher = openssl_encrypt($plain, 'aes-256-gcm', $key, OPENSSL_RAW_DATA, $iv, $tag);
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.
Syntax
openssl_encrypt( string $data, string $cipher_algo, string $passphrase, int $options = 0, string $iv = "", &$tag = null, string $aad = "", int $tag_length = 16 ): string|false openssl_decrypt( string $data, string $cipher_algo, string $passphrase, int $options = 0, string $iv = "", ?string $tag = null, string $aad = "" ): string|false
| Name | Description |
|---|---|
$data |
The plain text (encrypt) or the ciphertext (decrypt). |
$cipher_algo |
A cipher name from openssl_get_cipher_methods(), for example aes-256-gcm. |
$passphrase |
The key. Despite the name, pass a binary key of the right length (32 bytes for AES-256), not a human password. |
$options |
OPENSSL_RAW_DATA returns and expects raw bytes; without it the data is base64. |
$iv |
Initialization vector, openssl_cipher_iv_length() bytes (12 for GCM). |
$tag |
GCM only: filled with the authentication tag on encryption, required on decryption. |
$aad |
Optional additional authenticated data: not encrypted but covered by the tag. |
Return value: the encrypted or decrypted string, or false on failure (wrong key, wrong tag, tampered data). Errors: an unknown cipher throws a ValueError.
Usage notes
- Never reuse an IV with the same key. For GCM that breaks both secrecy and authentication. Generate a new one with
random_bytes()for every message and store it next to the ciphertext. The IV is not secret. - Always check for
falsewhen decrypting. It means the data cannot be trusted. - Without
OPENSSL_RAW_DATAthe output is base64-encoded, and decryption expects base64. Mixing the two is a common source offalse. - Derive keys properly. A key must be random bytes, or come from a password through a key-derivation function such as
hash_pbkdf2(). Do not use a short password directly. - CBC needs a MAC. If you must use
aes-256-cbc, add anhash_hmac()over the IV and ciphertext and verify it before decrypting. - Passwords are not encrypted: hash them with
password_hash(). - Prefer Sodium (
sodium_crypto_secretbox()) when available: it has fewer ways to go wrong. See Sodium functions. - Keep keys out of the code and out of the database that holds the ciphertext.