Encrypt then decrypt yields extra dots at end
am 07.09.2009 19:00:10 von Kelly Jones
I thought this code:
$enc=mcrypt_ecb(MCRYPT_RIJNDAEL_256,"salt123","encrypt_me",M CRYPT_ENCRYPT);
$dec=mcrypt_ecb(MCRYPT_RIJNDAEL_256,"salt123",$enc,MCRYPT_DE CRYPT);
echo $dec;
would yield "encrypt_me". The actual result is
"encrypt_me......................" (bunch of extra dots).
Why, and how do I fix it?
Does it have something to do w/ the warning I get:
Warning: mcrypt_ecb() [function.mcrypt-ecb]: Attempt to use an empty
IV, which is NOT recommend
I assumed that was a security warning, not a functionality warnings?
--
We're just a Bunch Of Regular Guys, a collective group that's trying
to understand and assimilate technology. We feel that resistance to
new ideas and technology is unwise and ultimately futile.
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Re: Encrypt then decrypt yields extra dots at end
am 09.09.2009 21:00:19 von Ben Dunlap
> I thought this code:
>
> $enc=mcrypt_ecb(MCRYPT_RIJNDAEL_256,"salt123","encrypt_me",M CRYPT_ENCRYPT);
> $dec=mcrypt_ecb(MCRYPT_RIJNDAEL_256,"salt123",$enc,MCRYPT_DE CRYPT);
> 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
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php