note 110435 deleted from function.mcrypt-encrypt by danbrown
| From: | danbrown@php.net | Date: | Thu, 06 Dec 2012 14:33:09 +0000 |
| Subject: | note 110435 deleted from function.mcrypt-encrypt by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-192344@lists.php.net to get a copy of this message | ||
Note Submitter: desmatic
----
class encrypt {
const EXAMPLEKEY1 = "UBeChsDHZOYdbITKz/LXceb3XMPVFOAP";
const EXAMPLEIV1 = "vsrpN/tmchipHza9jldMLVNCowLpkse5WOU8mVTnSOo=";
public static function gen_base64_key($bytes = 24, &$strong = null) {
return base64_encode(openssl_random_pseudo_bytes($bytes, $strong));
}
public static function gen_base64_ivector() {
$iv_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_CBC);
$iv = mcrypt_create_iv($iv_size);
return base64_encode($iv);
}
public static function encrypt_text($text, $base64key, $base64ivector) {
return base64_encode(mcrypt_encrypt(MCRYPT_RIJNDAEL_256, base64_decode($base64key),
bzcompress(trim($text)), MCRYPT_MODE_CBC, base64_decode($base64ivector)));
}
public static function decrypt_text($text, $base64key, $base64ivector) {
return bzdecompress(trim(mcrypt_decrypt(MCRYPT_RIJNDAEL_256, base64_decode($base64key),
base64_decode($text), MCRYPT_MODE_CBC, base64_decode($base64ivector))));
}
}
$plaintext = "this is a test";
$key = encrypt::gen_base64_key();
$iv = encrypt::gen_base64_ivector();
$cyphertext = encrypt::encrypt_text($plaintext, $key, $iv);
$decyphertext = encrypt::decrypt_text($cyphertext, $key, $iv);
echo $key . "\n";
echo $iv . "\n";
echo $plaintext . "\n";
echo $cyphertext . "\n";
echo $decyphertext . "\n";