note 105173 deleted from function.mcrypt-encrypt by nikic
| From: | nikic@php.net | Date: | Sat, 09 Nov 2013 11:17:23 +0000 |
| Subject: | note 105173 deleted from function.mcrypt-encrypt by nikic | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-197478@lists.php.net to get a copy of this message | ||
Note Submitter: antonio dot daeliminare dot bonifati at gmail dot com
----
If you want to be interoperable with other PKCS #7 padding implementations, like the Legion of the
Bouncy Castle Java cryptography APIs, you should always pad, that is a 8-byte (block size) padding
should be added, even if not necessary:
<?php
function encrypt($str, $key)
{
$block = mcrypt_get_block_size('des', 'ecb');
$pad = $block - (strlen($str) % $block);
$str .= str_repeat(chr($pad), $pad);
return mcrypt_encrypt(MCRYPT_DES, $key, $str, MCRYPT_MODE_ECB);
}
function decrypt($str, $key)
{
$str = mcrypt_decrypt(MCRYPT_DES, $key, $str, MCRYPT_MODE_ECB);
$block = mcrypt_get_block_size('des', 'ecb');
$pad = ord($str[($len = strlen($str)) - 1]);
return substr($str, 0, strlen($str) - $pad);
}
?>