Hi,
After some debugging of the script I found that the missing characters are due to the trim and chop used in the script for some reason.
If you put it like so, you always get a perfect encryption/decryption:
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 base64_encode($c_t);
}
function decrypt($key, $c_t) {
$c_t = 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($p_t);
}
GloomM
mcrypt_cfb
(PHP 4, PHP 5)
mcrypt_cfb — CFB モードでデータを暗号化/復号する
説明
string mcrypt_cfb
( int $cipher
, string $key
, string $data
, int $mode
, string $iv
)
string mcrypt_cfb
( string $cipher
, string $key
, string $data
, int $mode
[, string $iv
] )
最初のプロトタイプは libmcrypt 2.2.x とリンクした場合、2 番目は、 libmcrypt 2.4.x とリンクした場合のものです。 mode は MCRYPT_ENCRYPT あるいは MCRYPT_DECRYPT のいずれかです。
この関数は使用すべきではありません。代替となる関数については mcrypt_generic() および mdecrypt_generic() を参照ください。
mcrypt_cfb
GloomM
24-Jan-2006 08:14
24-Jan-2006 08:14
c4
09-May-2004 08:15
09-May-2004 08:15
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>
fandelem at hotmail dot com
15-May-2002 05:53
15-May-2002 05:53
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