Re: Encrypt then decrypt yields extra dots at end
| From: | Ben Dunlap | Date: | Wed, 09 Sep 2009 19:00:19 +0000 |
| Subject: | Re: Encrypt then decrypt yields extra dots at end | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-297841@lists.php.net to get a copy of this message | ||
> I thought this code:
>
> $enc=mcrypt_ecb(MCRYPT_RIJNDAEL_256,"salt123","encrypt_me",MCRYPT_ENCRYPT);
> $dec=mcrypt_ecb(MCRYPT_RIJNDAEL_256,"salt123",$enc,MCRYPT_DECRYPT);
> echo $dec;
>
> would yield "encrypt_me". The actual result is
> "encrypt_me......................" (bunch of extra dots).
>
> Why, and how do I fix it?
The manual says that mcrypt_ecb() is deprecated and recommends
mcrypt_generic() instead. Its page mentions that the input string will
be padded to the next-highest multiple of the current block size, and
points out:
'Note the string returned by mdecrypt_generic() will be [padded] as
well...use rtrim($str, "\0") to remove the padding'
http://us3.php.net/manual/en/function.mcrypt-generic.php
So I would guess that mcrypt_ecb() operates in a similar way, which
can be solved with rtrim(). Does your script actually echo "."
characters (ASCII 0x2E), or is that your terminal's way of
representing some non-printable character? It would surprise me if
mcrypt_ecb() used the "." character as its pad, but maybe it does.
Ben