Created
August 12, 2008 18:50
-
-
Save geoffgarside/5121 to your computer and use it in GitHub Desktop.
This file contains 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 | |
require_once 'mycrypt.php'; | |
$str = '3ryfbyevfibsd'; | |
$enc = encrypt($str); | |
$dec = decrypt($enc); | |
header("Content-Type: text/plain"); | |
echo $str ."\n"; | |
echo bin2hex($enc) ."\n"; | |
echo $dec ."\n"; | |
?> |
This file contains 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 | |
// Encryption Routines | |
define('ENCRYPTION_KEY', 'artofwar'); | |
define('ENCRYPTION_ALGORITHM', 'rijndael-256'); | |
define('ENCRYPTION_MODE', 'ecb'); | |
define('ENCRYPTION_IV_SOURCE', MCRYPT_DEV_RANDOM); // Change to MCRYPT_RAND if Windows | |
define('MYCRYPT_ENC', 1); | |
define('MYCRYPT_DEC', 2); | |
function encrypt($string) { | |
return mycrypt($string, MYCRYPT_ENC); | |
} | |
function decrypt($string) { | |
return mycrypt($string, MYCRYPT_DEC); | |
} | |
function mycrypt($string, $meth) { | |
$aes = mcrypt_module_open(ENCRYPTION_ALGORITHM, '', ENCRYPTION_MODE, ''); | |
if (!$aes) die('Unable to initialize encryption engine'); | |
$iv = mcrypt_create_iv(mcrypt_enc_get_iv_size($aes), MCRYPT_DEV_RANDOM); | |
$key = substr(md5(ENCRYPTION_KEY), 0, | |
mcrypt_enc_get_key_size($aes)); | |
mcrypt_generic_init($aes, $key, $iv); | |
switch ($meth) { | |
case MYCRYPT_ENC: | |
$ret = mcrypt_generic($aes, $string); | |
break; | |
case MYCRYPT_DEC: | |
$ret = rtrim(mdecrypt_generic($aes, $string), '\0'); | |
break; | |
} | |
mcrypt_generic_deinit($aes); | |
mcrypt_module_close($aes); | |
return $ret; | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment