If you want to encrypt and decrypt a string, you can use example below.


Example


$string = "Hello";

//--- ENCRYPTION ----
$chipher_size = mcrypt_get_key_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CFB);
$chipher = openssl_random_pseudo_bytes($chipher_size, $strong);

echo "<b>CHIPHER</b><br />";
echo $chipher . "<br />";
echo strlen($chipher) . "bit<br /><br />";

$iv_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CFB);
$iv = mcrypt_create_iv($iv_size, MCRYPT_DEV_URANDOM);

echo "<b>INITIALISATION VECTOR</b><br />";
echo $iv . "<br />";
echo strlen($iv) . "bit<br /><br />";

$encrypted_string = mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $chipher, $string, MCRYPT_MODE_CFB, $iv);

echo "<b>ENCRYPTED</b><br />";
echo $encrypted_string . "<br />";
echo strlen($encrypted_string) . "bit<br /><br />";

//--- DECRYPTION ----
echo $decrypted_string = mcrypt_decrypt(MCRYPT_RIJNDAEL_128, $chipher, $encrypted_string, MCRYPT_MODE_CFB, $iv);