note 21478 added to function.mcrypt-cfb
| From: | fandelem at hotmail dot com | Date: | Tue, 14 May 2002 17:53:11 +0000 |
| Subject: | note 21478 added to function.mcrypt-cfb | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-30637@lists.php.net to get a copy of this message | ||
please disregard the comment above me
(i wrote it prematurely.. if it could be
deleted, that would be great :)
anyways, here is a full working version of utilitizing cfb!
function encrypt($key, $plain_text) {
// returns encrypted text
// incoming: should be the $key that was encrypt
// with and the $plain_text that wants to be encrypted
$plain_text = trim($plain_text);
/* Quoting Mcrypt:
"You must (in CFB and OFB mode) or can (in CBC mode)
supply an initialization vector (IV) to the respective
cipher function. The IV must be unique and must be the
same when decrypting/encrypting."
Meaning, we need a way to generate a _unique_ initialization vector
but at the same time, be able to know how to gather our IV at both
encrypt/decrypt stage. My personal recommendation would be
(if you are working with files) is to get the md5() of the file.
In this example, however, I want more of a broader scope, so I chose
to md5() the key, which should be the same both times. Note that the IV
needs to be the size of our algorithm, hence us using substr.
*/
$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) {
// incoming: should be the $key that you encrypted
// with and the $c_t (encrypted text)
// returns plain text
// decode it first :)
$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));
}
cheers,
kyle
--
http://www.php.net/manual/en/function.mcrypt-cfb.php
http://master.php.net/manage/user-notes.php?action=edit+21478
http://master.php.net/manage/user-notes.php?action=delete+21478
http://master.php.net/manage/user-notes.php?action=reject+21478