Created
October 30, 2012 08:44
-
-
Save carlok/3979049 to your computer and use it in GitHub Desktop.
working examples to crypt and decrypt in PHP (note MCRYPT_RAND, use MASTERKEY or a string of yours)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// ckcrypt => crytps and encodes in base64 a given string | |
// ckdecrypt => decrypt a base64 encoded string | |
function ckcrypt($mprhase) { | |
$td = mcrypt_module_open('tripledes', '', 'ecb', ''); | |
$iv = mcrypt_create_iv(mcrypt_enc_get_iv_size($td), MCRYPT_RAND); | |
mcrypt_generic_init($td, MASTERKEY, $iv); | |
$crypted_value = mcrypt_generic($td, $mprhase); | |
mcrypt_generic_deinit($td); | |
mcrypt_module_close($td); | |
return base64_encode($crypted_value); | |
} | |
function ckdecrypt($mprhase) { | |
$td = mcrypt_module_open('tripledes', '', 'ecb', ''); | |
$iv = mcrypt_create_iv(mcrypt_enc_get_iv_size($td), MCRYPT_RAND); | |
mcrypt_generic_init($td, MASTERKEY, $iv); | |
$decrypted_value = mdecrypt_generic($td, base64_decode($mprhase)); | |
mcrypt_generic_deinit($td); | |
mcrypt_module_close($td); | |
return $decrypted_value; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment