Skip to content

Instantly share code, notes, and snippets.

@gegere
Created July 23, 2012 06:15
Show Gist options
  • Save gegere/3162221 to your computer and use it in GitHub Desktop.
Save gegere/3162221 to your computer and use it in GitHub Desktop.
2-Way Encryption Scheme
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