3 /** @package verysimple::Encryption */
6 * require supporting files
10 * A static utility class for encryption/decrytion using the php mcrypt functionality
12 * @package verysimple::Encryption::McryptUtil
17 static $CIPHER = MCRYPT_RIJNDAEL_256
;
18 static $MODE = MCRYPT_MODE_ECB
;
21 * Creates the initialization vector.
22 * This method is called automatically
23 * when encrypt/decrypt are called and does not need to be explicitly called
25 static function Init()
27 if (! McryptUtil
::$IV) {
28 if (! function_exists("mcrypt_get_iv_size")) {
29 throw new Exception("The mcrypt extension does not appear to be enabled.");
32 $iv_size = mcrypt_get_iv_size(McryptUtil
::$CIPHER, McryptUtil
::$MODE);
33 McryptUtil
::$IV = mcrypt_create_iv($iv_size, MCRYPT_RAND
);
41 * string data to be encrypted
43 * string encryption key/passphrase
45 * bool [optional] true to base64 encode the result
46 * @return string encrypted data
48 static function Encrypt($data, $key, $encode = true)
52 $encrypted = mcrypt_encrypt(McryptUtil
::$CIPHER, $key, $data, McryptUtil
::$MODE, McryptUtil
::$IV);
54 return ($encode) ?
base64_encode($encrypted) : $encrypted;
58 * Decrypts data that was previously encrypted
61 * string data to be decrypted
63 * string encryption key/passphrase
65 * bool [optional] true if the encrypted string is base64 encoded
67 * bool [optional] true to strip the null character padding at the end
68 * @return string decrypted data
70 static function Decrypt($data, $key, $decode = true, $strip_nulls = true)
75 $data = base64_decode($data);
78 $decrypted = mcrypt_decrypt(McryptUtil
::$CIPHER, $key, $data, McryptUtil
::$MODE, McryptUtil
::$IV);
80 // mcrypt pads the end of the block with null chars, so we need to strip them
81 return $strip_nulls ?
rtrim($decrypted, "\0") : $decrypted;