Skip to content

Instantly share code, notes, and snippets.

@carlok
Created October 30, 2012 08:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save carlok/3979049 to your computer and use it in GitHub Desktop.
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)
<?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