Delicious Bookmark this on Delicious Share on Facebook SlashdotSlashdot It! Digg! Digg



PHP : Function Reference : Mcrypt Encryption Functions : mcrypt_cfb

mcrypt_cfb

Encrypt/decrypt data in CFB mode (PHP 4, PHP 5)
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] )


Code Examples / Notes » mcrypt_cfb

c4

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>



<?php
$key = "asfasfgaep";
$random_number = rand(10000,99999);
$encrypted=encrypt($key,$random_number);
$decrypted=decrypt($key,$encrypted);
echo "NUMBER: $random_number
ENCRYPTED VALUE: $encrypted
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));
}
?>

</body>
</html>


fandelem

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


gloomm

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


Change Language


Follow Navioo On Twitter
mcrypt_cbc
mcrypt_cfb
mcrypt_create_iv
mcrypt_decrypt
mcrypt_ecb
mcrypt_enc_get_algorithms_name
mcrypt_enc_get_block_size
mcrypt_enc_get_iv_size
mcrypt_enc_get_key_size
mcrypt_enc_get_modes_name
mcrypt_enc_get_supported_key_sizes
mcrypt_enc_is_block_algorithm_mode
mcrypt_enc_is_block_algorithm
mcrypt_enc_is_block_mode
mcrypt_enc_self_test
mcrypt_encrypt
mcrypt_generic_deinit
mcrypt_generic_end
mcrypt_generic_init
mcrypt_generic
mcrypt_get_block_size
mcrypt_get_cipher_name
mcrypt_get_iv_size
mcrypt_get_key_size
mcrypt_list_algorithms
mcrypt_list_modes
mcrypt_module_close
mcrypt_module_get_algo_block_size
mcrypt_module_get_algo_key_size
mcrypt_module_get_supported_key_sizes
mcrypt_module_is_block_algorithm_mode
mcrypt_module_is_block_algorithm
mcrypt_module_is_block_mode
mcrypt_module_open
mcrypt_module_self_test
mcrypt_ofb
mdecrypt_generic
eXTReMe Tracker