Page 1 sur 1

mcrypt : Can not create an IV with a size of less then 1 or

Posté : 18 janv. 2012, 19:48
par zzzzzz
Bonjour j'utilise 2 fonctions, une pour crypter, une pour decrypter. Pour être franc je n'y comprend pas grand chose...

J'ai essayé dans un fichier test.php le code suivant pour crypter :
$string = "Some text to be encrypted";
$secret_key = "k4Mdj9Ka8Kam2kMxn2jMdj0KA2";
 
// Encryption Algorithm
$etype = MCRYPT_RIJNDAEL_256;
 
// Create the initialization vector for added security.
$iv = mcrypt_create_iv(mcrypt_get_iv_size($etype, MCRYPT_MODE_ECB), MCRYPT_RAND);
 
// Output original string
PRINT "Original string: $string <p>";
 
// Encrypt $string
$encrypted_string = mcrypt_encrypt($etype, $secret_key, $string, MCRYPT_MODE_CBC, $iv);
 
// Convert to hexadecimal and send to browser
PRINT "Encrypted string: ".BIN2HEX($encrypted_string)."<p>";
 
$decrypted_string = mcrypt_decrypt($etype, $secret_key, $encrypted_string, MCRYPT_MODE_CBC, $iv);
 
PRINT "Decrypted string is: $decrypted_string"
Tout marche!

Maintenant j'essaye d'y transposer en pratique dans ma class Secure :
class Secure {
const Cle = 'blablabla';
const etype = MCRYPT_RIJNDAEL_256;

...

function Crypter($etype,$secret_key,$string)
{
$iv = mcrypt_create_iv(mcrypt_get_iv_size($etype, MCRYPT_MODE_ECB), MCRYPT_RAND);
return bin2hex(mcrypt_encrypt($etype, $secret_key, $string, MCRYPT_MODE_CBC, $iv));
}

function Decrypt($etype,$secret_key,$encrypted_string)
{
$iv = mcrypt_create_iv(mcrypt_get_iv_size($etype, MCRYPT_MODE_ECB), MCRYPT_RAND);
return mcrypt_decrypt($etype, $secret_key, $encrypted_string, MCRYPT_MODE_CBC, $iv);
}
Puis dans ma class pour enregistrer un membre j'ai quelque chose de ce style :
$pseudo = Secure::Crypter($this->etype,$this->Cle,$pseudo);
$email =  Secure::Crypter($this->etype,$this->Cle,$email);
Conclusion cela ne marche pas et j'ai des warning :
Warning: mcrypt_get_iv_size() [function.mcrypt-get-iv-size]: Module initialization failed in /home/public_html/class/Secure.php on line 162

Warning: mcrypt_create_iv() [function.mcrypt-create-iv]: Can not create an IV with a size of less then 1 or greater then 2147483647 in /home/public_html/class/Secure.php on line 162

Warning: mcrypt_encrypt() [function.mcrypt-encrypt]: Module initialization failed in /home/public_html/class/Secure.php on line 163

Warning: mcrypt_get_iv_size() [function.mcrypt-get-iv-size]: Module initialization failed in /home/public_html/class/Secure.php on line 162
Je n'arrive pas à voir ou je me suis planter... le test.php simple marche. Quelles erreurs ai-je fais ? J'avoue ne pas comprendre grand chose à mcrypt.

Merci d'avance pour votre aide!