note 117499 added to function.openssl-encrypt
| From: | Raphael | Date: | Thu, 18 Jun 2015 19:55:18 +0000 |
| Subject: | note 117499 added to function.openssl-encrypt | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-202902@lists.php.net to get a copy of this message | ||
Beware of the padding this method adds !
<?php
$encryption_key = openssl_random_pseudo_bytes(32);
$iv = openssl_random_pseudo_bytes(16);
$data = openssl_random_pseudo_bytes(32);
for ($i = 0; $i < 5; $i++) {
$data = openssl_encrypt($data, 'aes-256-cbc', $encryption_key, OPENSSL_RAW_DATA, $iv);
echo strlen($data) . "\n";
}
?>
With this sample the output will be:
48
64
80
96
112
This is because our $data is already taking all the block size, so the method is adding a new block
which will contain only padded bytes.
The only solution that come to my mind to avoid this situation is to add the option
OPENSSL_ZERO_PADDING along with the first one:
<?php
$data = openssl_encrypt($data, 'aes-256-cbc', $encryption_key,
OPENSSL_RAW_DATA|OPENSSL_ZERO_PADDING, $iv);
?>
/!\ Be careful when using this option, be sure that you provide data that have already been padded
or that takes already all the block size.
----
Server IP: 72.52.91.14
Probable Submitter: 109.24.149.197
----
Manual Page -- http://php.net/manual/en/function.openssl-encrypt.php
Edit -- https://master.php.net/note/edit/117499
Del: integrated -- https://master.php.net/note/delete/117499/integrated
Del: useless -- https://master.php.net/note/delete/117499/useless
Del: bad code -- https://master.php.net/note/delete/117499/bad+code
Del: spam -- https://master.php.net/note/delete/117499/spam
Del: non-english -- https://master.php.net/note/delete/117499/non-english
Del: in docs -- https://master.php.net/note/delete/117499/in+docs
Del: other reasons-- https://master.php.net/note/delete/117499
Reject -- https://master.php.net/note/reject/117499
Search -- https://master.php.net/manage/user-notes.php