<?php
/*
* PHP mcrypt - Basic encryption and decryption of a string
*/
$secret_key = "This is my secret key";
$string = "Some text to be encrypted";
// Create the initialization vector for added security.
$iv = mcrypt_create_iv(mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB), MCRYPT_RAND);
// Encrypt $string
$encrypted_string = mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $secret_key, $string, MCRYPT_MODE_CBC, $iv);
// Encodes data with MIME base64
$encrypted_string_base64 = base64_encode($encrypted_string);
$encrypted_string = base64_decode($encrypted_string_base64);
// Decrypt $string
$decrypted_string = mcrypt_decrypt(MCRYPT_RIJNDAEL_256, $secret_key, $encrypted_string, MCRYPT_MODE_CBC, $iv);
echo "Original string : " . $string . "\n";
echo "Encrypted string : " . $encrypted_string . "\n";
echo 'Base 64 String: ' . $encrypted_string_base64 . "\n";
echo "Decrypted string : " . $decrypted_string . "\n";
?>
Friday, August 28, 2015
PHP mcrypt - Basic encryption and decryption of a string
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment