|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2021-10-04 17:12 UTC] alec at alec dot pl
Description: ------------ If I do: $tag = null; openssl_decrypt($cipher, $method, $ckey, $opts, $iv, $tag); I get: PHP Deprecated: openssl_decrypt(): Passing null to parameter #6 ($tag) of type string is deprecated. But if I do: $tag = ''; openssl_decrypt($cipher, $method, $ckey, $opts, $iv, $tag); I get: PHP Warning: openssl_decrypt(): The tag cannot be used because the cipher algorithm does not support AEAD. I understand the warnings, but this is not convenient if my code is supposed to support AEAD and non-AEAD cipher methods. Do I have to do two code paths depending on the cipher method is used? What's more. The default value for the $tag argument in openssl_encrypt() is null, which sounds kind of inconsistent. PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Tue Oct 28 04:00:01 2025 UTC |
``` <?php ini_set('error_reporting', E_ALL); ini_set('display_errors', 'on'); function encrypt($clear) { $key = 'key'; $ckey = '123456789012345678901234'; $method = 'DES-EDE3-CBC'; $opts = OPENSSL_RAW_DATA; $iv = random_bytes(openssl_cipher_iv_length($method)); $cipher = openssl_encrypt($clear, $method, $ckey, $opts, $iv, $tag); $cipher = $iv . $cipher; return $cipher; } function decrypt($cipher) { $key = 'key'; $ckey = '123456789012345678901234'; $method = 'DES-EDE3-CBC'; $opts = OPENSSL_RAW_DATA; $iv_size = openssl_cipher_iv_length($method); $tag = null; $iv = substr($cipher, 0, $iv_size); $cipher = substr($cipher, $iv_size); return openssl_decrypt($cipher, $method, $ckey, $opts, $iv, $tag); } echo decrypt(encrypt('test')) === 'test'; ``` One correction, I'm using PHP 8.1.0RC2 on Ubuntu 18.04.