Here is a safe encrypt and decrypt function for php developer. You can Encrypt and Decrypt using md5. The md5() function calculates the MD5 hash of a string. md5() and sha1() provides the same functinality of encryption in php but they differ in a simple way that md5() generates 32 characters of encrypted string which sha1() generates same of 40 characters.
How to use encrypt or decrypt code:
<?php /* Online PHP Examples with Source Code website: http://4evertutorials.blogspot.com/ */ function encrypt_decrypt($action, $string) { $output = false; $key = 'My strong random secret key'; // initialization vector $iv = md5(md5($key)); if( $action == 'encrypt' ) { $output = mcrypt_encrypt(MCRYPT_RIJNDAEL_256, md5($key), $string, MCRYPT_MODE_CBC, $iv); $output = base64_encode($output); } else if( $action == 'decrypt' ){ $output = mcrypt_decrypt(MCRYPT_RIJNDAEL_256, md5($key), base64_decode($string), MCRYPT_MODE_CBC, $iv); $output = rtrim($output, ""); } return $output; } ?>
How to use encrypt or decrypt code:
<?php /* Online PHP Examples with Source Code website: http://4evertutorials.blogspot.com/ */ $plain_txt = "This is my plain text"; $encrypted_txt = encrypt_decrypt('encrypt', $plain_txt); echo "Encrypted = $encrypted_txt\n"; echo "
"; $decrypted_txt = encrypt_decrypt('decrypt', $encrypted_txt); echo "Decrypted = $decrypted_txt\n"; ?>
0 comments:
Post a Comment