Re: Encrypt then decrypt yields extra dots at end

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



> 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

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux