note 101232 deleted from mcrypt.examples by danbrown
| From: | danbrown@php.net | Date: | Fri, 03 Dec 2010 22:15:48 +0000 |
| Subject: | note 101232 deleted from mcrypt.examples by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-174601@lists.php.net to get a copy of this message | ||
Note Submitter: ecrane57 at gmail dot com
----
jizzy @ Nowhere posted a routine to encrypt and decrypt just below. The decrypt pad-character
removing function has a problem. It starts to low (0) and ends to high. So changing the $i in the
for loop to start at 1 and end before $bit_check like below, fixes the padding range. I tested with
a 40 character clear text and removed one char at a time down to 1 and it worked.
function decrypt($encrypted_text,$key,$iv,$bit_check){
$cipher = mcrypt_module_open(MCRYPT_TRIPLEDES,'','cbc','');
mcrypt_generic_init($cipher, $key, $iv);
$decrypted = mdecrypt_generic($cipher,base64_decode($encrypted_text));
mcrypt_generic_deinit($cipher);
$last_char=substr($decrypted,-1);
for($i=1; $i < $bit_check; $i++){
if(chr($i)==$last_char){
$decrypted=substr($decrypted, 0, strlen($decrypted) - $i);
break;
}
}
return $decrypted;
}