note 42227 added to function.mcrypt-cfb
| From: | c4 at rn2 dot php dot net | Date: | Sat, 08 May 2004 20:15:53 +0000 |
| Subject: | note 42227 added to function.mcrypt-cfb | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-69352@lists.php.net to get a copy of this message | ||
There is something wrong (with your code?) Kyle, the decryption part sometimes deletes the last
character! Below is the code I tested, if you run it a lot of times you will see that the decrypted
value sometimes doesn't match the encrypted one (the last digit is missing).
Any idea what might be wrong?
Otherwise I like to code very much!
Regards,
c4
CODE:
<html>
<body>
<p>
<?php
$key = "asfasfgaep";
$random_number = rand(10000,99999);
$encrypted=encrypt($key,$random_number);
$decrypted=decrypt($key,$encrypted);
echo "NUMBER: $random_number<br>
ENCRYPTED VALUE: $encrypted<br>
DECRYPTED VALUE: $decrypted";
/* Your functions without the comments are below */
function encrypt($key, $plain_text) {
$plain_text = trim($plain_text);
$iv = substr(md5($key), 0,mcrypt_get_iv_size (MCRYPT_CAST_256,MCRYPT_MODE_CFB));
$c_t = mcrypt_cfb (MCRYPT_CAST_256, $key, $plain_text, MCRYPT_ENCRYPT, $iv);
return trim(chop(base64_encode($c_t)));
}
function decrypt($key, $c_t) {
$c_t = trim(chop(base64_decode($c_t)));
$iv = substr(md5($key), 0,mcrypt_get_iv_size (MCRYPT_CAST_256,MCRYPT_MODE_CFB));
$p_t = mcrypt_cfb (MCRYPT_CAST_256, $key, $c_t, MCRYPT_DECRYPT, $iv);
return trim(chop($p_t));
}
?>
</p>
</body>
</html>
----
Manual Page -- http://www.php.net/manual/en/function.mcrypt-cfb.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+42227
Delete -- http://master.php.net/manage/user-notes.php?action=delete+42227&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+42227&report=yes
Search -- http://master.php.net/manage/user-notes.php