note 120891 deleted from function.openssl-decrypt by cmb
| From: | cmb@php.net | Date: | Thu, 20 Aug 2020 09:10:00 +0000 |
| Subject: | note 120891 deleted from function.openssl-decrypt by cmb | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-215003@lists.php.net to get a copy of this message | ||
Note Submitter: Bitbang3r
----
openssl_decrypt assumes that $data is base64-encoded by default, but $key and $iv (when using AES)
must be "raw" byte values.
In other words, assume that $data, $key, and $iv are all base64-encoded strings. Both $key and $iv
must be base64-decoded before openssl_decrypt can use them.
Example code:
$ciphertext64 = "gfcC6t1BarndpzMuvYj2JFpWHqlWSJMhTtxPN7QjyEg=";
$key64 = "AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8=";
$iv64="AAECAwQFBgcICQoLDA0ODw==";
$key = base64_decode($key64, true);
$iv = base64_decode($iv64, true);
$decrypted = openssl_decrypt($ciphertext64, 'aes-256-cbc', $key, 0, $iv);
Other 'gotchas' to keep in mind when cipher is 'aes-256-cbc':
* 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.