Re: PKCS#5 padding
| From: | Robin Vickery | Date: | Fri, 09 Dec 2005 13:56:53 +0000 |
| Subject: | Re: PKCS#5 padding | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-227190@lists.php.net to get a copy of this message | ||
On 12/9/05, Binay(Local) <binay@oliveinternet.com> wrote:
> Hi All
>
> Has anybody got the working code to apply the PKCS#5 padding to the text for encryption. Please
> let me know as it seems PHP inbuilt functions do not support this padding at all. Been worried about
> from past 2 days. Help me out.
The pad function simply returns the text padded to the blocksize.
The unpad function returns the unpadded text, or false if the padding
isn't valid.
function pkcs5_pad ($text, $blocksize)
{
$pad = $blocksize - (strlen($text) % $blocksize);
return $text . str_repeat(chr($pad), $pad);
}
function pkcs5_unpad ($text)
{
$pad = ord($text{strlen($text)-1});
if ($pad > strlen($text)) return false;
if (!strspn($text, chr($pad), strlen($text) - $pad)) return false;
return substr($text, 0, -1 * $pad);
}