Created
July 23, 2012 06:15
-
-
Save gegere/3162221 to your computer and use it in GitHub Desktop.
2-Way Encryption Scheme
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
class Encryption | |
{ | |
public function encrypt($plaintext,$key) | |
{ | |
$td = mcrypt_module_open(MCRYPT_RIJNDAEL_256, '', MCRYPT_MODE_CBC, ''); | |
$iv = mcrypt_create_iv(mcrypt_enc_get_iv_size($td), MCRYPT_RAND); | |
mcrypt_generic_init($td, $key, $iv); | |
$crypttext = mcrypt_generic($td, $plaintext); | |
mcrypt_generic_deinit($td); | |
return base64_encode($iv.$crypttext); | |
} | |
public function decrypt($crypttext, $key) | |
{ | |
$crypttext = base64_decode($crypttext); | |
$plaintext = ''; | |
$td = mcrypt_module_open(MCRYPT_RIJNDAEL_256, '', MCRYPT_MODE_CBC, ''); | |
$ivsize = mcrypt_enc_get_iv_size($td); | |
$iv = substr($crypttext, 0, $ivsize); | |
$crypttext = substr($crypttext, $ivsize); | |
if ($iv) | |
{ | |
mcrypt_generic_init($td, $key, $iv); | |
$plaintext = mdecrypt_generic($td, $crypttext); | |
} | |
return $plaintext; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment