note 116058 added to function.mcrypt-encrypt

From: Date: Tue, 04 Nov 2014 13:19:09 +0000
Subject: note 116058 added to function.mcrypt-encrypt
Groups: php.notes php.notes 
Request: Send a blank email to php-notes+get-200782@lists.php.net to get a copy of this message
I tried to implement the mcrypt with rijndael-128. For reference I took the code from example #1 and tried running that first, but on the decryption part came back with the error: "The IV parameter must be as long as the blocksize". After a while i figured out that the generated IV string will not have the same length every run, and is almost never the size of the result of mcrypt_get_iv_size. To work around that, before merging the IV and the encrypted text, I added null padding to match the IV size. When retrieving the IV, I then could use the IV size and rtrim null padding to get the matching IV back. The altered parts from example #1 <?php # --- ENCRYPTION --- $iv_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC); $iv = mcrypt_create_iv($iv_size, MCRYPT_RAND); $ciphertext = mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $key, $plaintext, MCRYPT_MODE_CBC, $iv); # Add null padding to match the espected size while(strlen($iv) < $iv_size){ $iv .= "\0"; } # prepend the IV for it to be available for decryption $ciphertext = $iv . $ciphertext; ?> <?php # --- DECRYPTION --- mcrypt_get_iv_size() $iv_dec = rtrim(substr($ciphertext_dec, 0, $iv_size)); ?> ---- Server IP: 72.52.91.14 Probable Submitter: 80.113.17.58 ---- Manual Page -- http://php.net/manual/en/function.mcrypt-encrypt.php Edit -- https://master.php.net/note/edit/116058 Del: integrated -- https://master.php.net/note/delete/116058/integrated Del: useless -- https://master.php.net/note/delete/116058/useless Del: bad code -- https://master.php.net/note/delete/116058/bad+code Del: spam -- https://master.php.net/note/delete/116058/spam Del: non-english -- https://master.php.net/note/delete/116058/non-english Del: in docs -- https://master.php.net/note/delete/116058/in+docs Del: other reasons-- https://master.php.net/note/delete/116058 Reject -- https://master.php.net/note/reject/116058 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#200782) next »