fix: set default x12 partner for item in billing manager (#7513)
[openemr.git] / portal / patient / fwk / libs / verysimple / Encryption / McryptUtil.php
blob4b2a7c30e3b9a50555c30a68037202a64fad461e
1 <?php
3 /** @package verysimple::Encryption */
5 /**
6 * require supporting files
7 */
9 /**
10 * A static utility class for encryption/decrytion using the php mcrypt functionality
12 * @package verysimple::Encryption::McryptUtil
14 class McryptUtil
16 static $IV;
17 static $CIPHER = MCRYPT_RIJNDAEL_256;
18 static $MODE = MCRYPT_MODE_ECB;
20 /**
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);
37 /**
38 * Encrypts data
40 * @param
41 * string data to be encrypted
42 * @param
43 * string encryption key/passphrase
44 * @param
45 * bool [optional] true to base64 encode the result
46 * @return string encrypted data
48 static function Encrypt($data, $key, $encode = true)
50 McryptUtil::Init();
52 $encrypted = mcrypt_encrypt(McryptUtil::$CIPHER, $key, $data, McryptUtil::$MODE, McryptUtil::$IV);
54 return ($encode) ? base64_encode($encrypted) : $encrypted;
57 /**
58 * Decrypts data that was previously encrypted
60 * @param
61 * string data to be decrypted
62 * @param
63 * string encryption key/passphrase
64 * @param
65 * bool [optional] true if the encrypted string is base64 encoded
66 * @param
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)
72 McryptUtil::Init();
74 if ($decode) {
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;