PHP
downloads | documentation | faq | getting help | mailing lists | wiki | reporting bugs | php.net sites | links | conferences | my php.net

search for in the

mcrypt_create_iv> <mcrypt_cbc
Last updated: Fri, 14 Nov 2008

view this page in

mcrypt_cfb

(PHP 4, PHP 5)

mcrypt_cfbChiffre/déchiffre des données en mode CFB

Description

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 ] )

La première syntaxe utilise libmcrypt 2.2.x, et la seconde libmcrypt 2.4.x. Le paramètre mode doit être MCRYPT_ENCRYPT ou MCRYPT_DECRYPT.

mcrypt_cfb() ne doit plus être utilisée. Vous pouvez la remplacer par mcrypt_generic() et mdecrypt_generic().



mcrypt_create_iv> <mcrypt_cbc
Last updated: Fri, 14 Nov 2008
 
add a note add a note User Contributed Notes
mcrypt_cfb
GloomM
24-Jan-2006 12:14
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
c4
09-May-2004 01: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
14-May-2002 10: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

mcrypt_create_iv> <mcrypt_cbc
Last updated: Fri, 14 Nov 2008
 
 
show source | credits | sitemap | contact | advertising | mirror sites