note 54734 modified in function.mcrypt-decrypt by nlopess

From: Date: Fri, 23 Sep 2005 08:32:39 +0000
Subject: note 54734 modified in function.mcrypt-decrypt by nlopess
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-95649@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: <? $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 ---- My comments are based on PHP 4.3.4 http://php.net/manual/en/function.mcrypt-decrypt.php

« previous php.notes (#95649) next »