note 54734 modified in function.mcrypt-decrypt by danbrown

From: Date: Fri, 30 Sep 2011 21:37:58 +0000
Subject: note 54734 modified in function.mcrypt-decrypt by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-183563@lists.php.net to get a copy of this message
It appears that mcrypt_decrypt pads the *RETURN STRING* with nulls ('\0') to fill out to n * blocksize. For old C-programmers, like myself, it is easy to believe the string ends at the first null. In PHP it does not: strlen("abc\0\0") returns 5 and *NOT* 3 strcmp("abc", "abc\0\0") returns -2 and *NOT* 0 I learned this lesson painfully when I passed a string returned from mycrypt_decrypt into a NuSoap message, which happily passed the nulls along to the receiver, who couldn't figure out what I was talking about. My solution was: <?php $retval = mcrypt_decrypt( ...etc ...); $retval = rtrim($retval, "\0"); // trim ONLY the nulls at the END ?> --was-- It appears that mcrypt_decrypt pads the *RETURN STRING* with nulls ('\0') to fill out to n * blocksize. For old C-programmers, like myself, it is easy to believe the string ends at the first null. In PHP it does not: strlen("abc\0\0") returns 5 and *NOT* 3 strcmp("abc", "abc\0\0") returns -2 and *NOT* 0 I learned this lesson painfully when I passed a string returned from mycrypt_decrypt into a NuSoap message, which happily passed the nulls along to the receiver, who couldn't figure out what I was talking about. My solution was: <? $retval = mcrypt_decrypt( ...etc ...); $retval = rtrim($retval, "\0"); // trim ONLY the nulls at the END ?> http://php.net/manual/en/function.mcrypt-decrypt.php

« previous php.notes (#183563) next »