Encryption and Decryption in PHP
<?php
$key = 'Your Key';
$string = 'My String'; // note the spaces
$encrypted = base64_encode(mcrypt_encrypt(MCRYPT_RIJNDAEL_256, md5($key), $string, MCRYPT_MODE_CBC, md5(md5($key))));
$decrypted = rtrim(mcrypt_decrypt(MCRYPT_RIJNDAEL_256, md5($key), base64_decode($encrypted), MCRYPT_MODE_CBC, md5(md5($key))), "\0");
echo 'Encrypted:' . "\n";
var_dump($encrypted);
echo "\n";
echo 'Decrypted:' . "\n";
var_dump($decrypted); // spaces are preserved
?>
Output :
Origional : 'My String'
Encrypted:
string 'X7zPEx/hl2aMvBrK2+V3XJtqvQssSiYUQWTFyHlbjmQ=' (length=44)Decrypted:
string 'My String' (length=9)
No comments:
Post a Comment