Вот юолее простой пример использования mcrypt_encrypt / mcrypt_decrypt:
<?php
function encrypt_data($key, $text){
$iv_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB);
$iv = mcrypt_create_iv($iv_size, MCRYPT_RAND);
$encrypted_text = mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $key, $text, MCRYPT_MODE_ECB, $iv);
return $encrypted_text;
}
function decrypt_data($key, $text){
global $encryptionkey;
$iv_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB);
$iv = mcrypt_create_iv($iv_size, MCRYPT_RAND);
$decrypted_text = mcrypt_decrypt(MCRYPT_RIJNDAEL_256, $key, $text, MCRYPT_MODE_ECB, $iv);
return $decrypted_text;
}
$key = 'This is a very secret key';
$text = "Meet me at 11 o'clock behind the monument.";
echo $text .':'. mb_strlen ($text)."\n";
$crypttext = encrypt_data ($key,$text);
$decrypttext = decrypt_data ($key, $crypttext) ;
echo $decrypttext.':'. mb_strlen ($decrypttext)."\n";
var_dump($decrypttext);
В результате выполнения:
[AGvin@localhost public]$ php test.php
Meet me at 11 o'clock behind the monument.:42
Meet me at 11 o'clock behind the monument.:64
string(64) "Meet me at 11 o'clock behind the monument.\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000"
В качестве лечения, можно использовать
$decrypttext= rtrim($decrypttext, '\0');
или поправить функцию decrypt_data:
<?php
function decrypt_data($key, $text){
global $encryptionkey;
$iv_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB);
$iv = mcrypt_create_iv($iv_size, MCRYPT_RAND);
$decrypted_text = mcrypt_decrypt(MCRYPT_RIJNDAEL_256, $key, $text, MCRYPT_MODE_ECB, $iv);
return rtrim($decrypted_text, '\0');;
}