PHP 5.4.36 Released


(PHP 5 >= 5.3.0)

openssl_encryptEncripta datos


string openssl_encrypt ( string $data , string $method , string $password [, int $options = 0 [, string $iv = "" ]] )

Encripta la información dada con el método y la clave dados, y devuelve una cadena codificada en bruto o mediante base64.


Esta función no está documentada actualmente, solamente se encuentra disponible la lista de parámetros.



Los datos.


El método de cifrado.


La contraseña.


options puede ser una de las constantes OPENSSL_RAW_DATA, OPENSSL_ZERO_PADDING.


Un Vector de Inicialización no NULL.

Valores devueltos

Devuelve la cadena encriptada si se tuvo éxito o FALSE en caso de error.


Emite un error de nivel E_WARNING si se pasa un algoritmo de cifrado desconocido mediante el parámetro method.

Emite un error de nivel E_WARNING si se le pasa un valor vacío mediante el parámetro iv.

Historial de cambios

Versión Descripción
5.3.3 Se añadió el parámetro iv.
5.4.0 raw_output se cambió a options.

Ver también

add a note add a note

User Contributed Notes 7 notes

biohazard dot ge at gmail dot com
3 years ago
Many users give up with handilng problem when openssl command line tool cant decrypt php openssl encrypted file which is encrypted with openssl_encrypt function.

For example how beginner is encrypting data:


= 'It works ? Or not it works ?';
$pass = '1234';
$method = 'aes128';

file_put_contents ('./file.encrypted', openssl_encrypt ($string, $method, $pass));


And then how beginner is trying to decrypt data from command line:

# openssl enc -aes-128-cbc -d -in file.encrypted -pass pass:123

Or even if he/she determinates that openssl_encrypt output was base64 and tries:

# openssl enc -aes-128-cbc -d -in file.encrypted -base64 -pass pass:123

Or even if he determinates that base64 encoded file is represented in one line and tries:

# openssl enc -aes-128-cbc -d -in file.encrypted -base64 -A -pass pass:123

Or even if he determinates that IV is needed and adds some string iv as encryption function`s fourth parameter and than adds hex representation of iv as parameter in openssl command line :

# openssl enc -aes-128-cbc -d -in file.encrypted -base64 -pass pass:123 -iv -iv 31323334353637383132333435363738

Or even if he determinates that aes-128 password must be 128 bits there fore 16 bytes and sets $pass = '1234567812345678' and tries:

# openssl enc -aes-128-cbc -d -in file.encrypted -base64 -pass pass:1234567812345678 -iv -iv 31323334353637383132333435363738

All these troubles will have no result in any case.


It means that the password parameter of the function is not the same string used as [-pass pass:] parameter with openssl cmd tool for file encryption decryption.


And now how to correctly encrypt data with php openssl_encrypt and how to correctly decrypt it from openssl command line tool.


function strtohex($x)
        foreach (
str_split($x) as $c) $s.=sprintf("%02X",ord($c));
$source = 'It works !';

$iv = "1234567812345678";
$pass = '1234567812345678';
$method = 'aes-128-cbc';

"\niv in hex to use: ".strtohex ($iv);
"\nkey in hex to use: ".strtohex ($pass);

file_put_contents ('./file.encrypted',openssl_encrypt ($source, $method, $pass, true, $iv));

$exec = "openssl enc -".$method." -d -in file.encrypted -nosalt -nopad -K ".strtohex($pass)." -iv ".strtohex($iv);

'executing: '.$exec."\n\n";
exec ($exec);


IV and Key parameteres passed to openssl command line must be in hex representation of string.

The correct command for decrypting is:

# openssl enc -aes-128-cbc -d -in file.encrypted -nosalt -nopad -K 31323334353637383132333435363738 -iv 31323334353637383132333435363738

As it has no salt has no padding and by setting functions third parameter we have no more base64 encoded file to decode. The command will echo that it works...

: /
2 years ago
PHP OpenSSL functions openssl_encrypt() and openssl_decrypt() seem to use PKCS5/7 style padding for all symmetric ciphers. Upon this, you can't use them to encrypt using null byte padding or to decrypt null byte padded data.

The developers of the wrapper forgot the padding scheme flags... :(
public at grik dot net
4 years ago
The list of methods for this function can be obtained with openssl_get_cipher_methods();
The password can be encrypted with the openssl_private/public_encrypt()
kasper at webmasteren dot eu
2 years ago
For security reasons, if NOT  encrypting using a (cryptographically) random IV, then the IV itself must be entryptet as well ( this is not handled behind the scene).
the reason is that it leads to predictable IV's, which then breaks semantic security. [this kind of attack can already be done on ssl/ TLS 1.1].
2 years ago
Might be useful to people trying to use 'aes-256-cbc' cipher (and probably other cbc ciphers) in collaboration with other implementations of AES (C libs for example) that the openssl extension has a strict implementation regarding padding bytes. I found the solution only by manually going through the openssl source.

In C, you would want to pad plaintexts the following way (assuming all mem allocations are proper):

nPadding = ( 16 - ( bufferSize % 16 ) ) ? ( 16 - ( bufferSize % 16 ) ) : 16;
for( index = bufferSize; index < bufferSize + nPadding; index++ )
    plaintext[ index ] = (char)nPadding;

while decryptions are validated like:

isSuccess = TRUE;
for( index = bufferSize - 1; index > ( bufferSize - nPadding ); index-- )
    if( plaintext[ index ] != nPadding )
        isSuccess = FALSE;
decryptedSize = bufferSize - nPadding;

In plain english, the buffer must be padded up to blockSize. If the buffer is already a multiple of blockSize, you add an entire new blockSize bytes as padding.

The value of the padding bytes MUST be the number of padding bytes as a byte...

So 5 bytes of padding will result in the following bytes added at the end of the ciphertext:
[ 0x05 ][ 0x05 ][ 0x05 ][ 0x05 ][ 0x05 ]

Hope this saves someone else a few hours of their life.
kazaaknet at yahoo dot com
2 years ago
Be advised there was a memory leak in this function:  I believe this got fixed in 5.3.6, but on production webservers running 5.3.5 with modest traffic, this became  a memory hemorrhage that brought my site down.  Look at mcrypt_encrypt instead.
public at grik dot net
4 years ago
In 5.3.3 they added a new parameter, string $iv (initialization vector)
Real parameters are:
string openssl_encrypt ( string $data , string $method , string $password, bool $raw_output = false, string $iv )

If $iv is missing, a warning is issued: "Using an empty Initialization Vector (iv) is potentially insecure and not recommended".

If $iv is too short, another warning:
"IV passed is only 3 bytes long, cipher expects an IV of precisely 8 bytes, padding with \0"

same IV should be used in openssl_decrypt()
To Top