A se vedea și

User Contributed Notes 6 notes

- $data can be as the description says raw or base64. If no $option is set (this is, if value of 0 is passed in this parameter), data will be assumed to be base64 encoded. If parameter OPENSSL_RAW_DATA is set, it will be understood as row data.

- $password (key) is a String of [pseudo] bytes as those generated by the function openssl_random_pseudo_bytes().

- $options as (as for 2016) two possible values OPENSSL_RAW_DATA and OPENSSL_ZERO_PADDING. Setting both can be done by OPENSSL_RAW_DATA||OPENSSL_ZERO_PADDING. If no OPENSSL_ZERO_PADDING is specify, default pading of PKCS#7 will be done as it's been observe by [openssl at mailismagic dot com]'s coment in openssl_encrypt()

- $iv is as in the case of $password, a String of bytes. Its length depends on the algorithm used. May be the best way to generate an $iv is by:

in case that hosting do not provide openssl_encrypt decrypt functions - it could be mimiced via commad prompt executions this functions will check is if openssl is installed and try to use it by default

* strlen($key) SHOULD be 32. PHP will apparently pad the key if necessary, with potentially unpredictable interoperability with other libraries and platforms, and almost certain reduction in cipher strength. Save yourself the headache, and make sure it's EXACTLY 32.

* strlen($iv) MUST be 16. By definition, AES uses 128-bit blocks, regardless of whether the key length is 128, 192, or 256... and iv's length must be precisely equal to that block length.

* Remember... $iv doesn't necessarily have to be SECRET (it's just a salt), but it MUST be cryptographically random AND different EACH TIME you begin a new round of AES encryption ("round" == "one call to openssl_encrypt or equivalent").

* Don't assume that your random numbers are cryptographically secure unless the function guarantees it. In general, PHP's random numbers AREN'T cryptographically secure (at least, not by default, and not unless the server's admin has gone out of his way to try). There's a HUGE difference between numbers that "look random", and numbers that genuinely ARE random, and it can make the difference between robust long-term encryption and mere obfuscation. See openssl_random_pseudo_bytes.

Finally, if you're attempting to use 'aes-256-gcm' (AEAD), search Google for "67304 gcm" to confirm that it's both supported AND known to work in whatever version of PHP you have available.