/** * Encrypt and return a string using the Rijndael 256 cypher * @param string $str String to encrypt * @param string $key Key to encrypt the string * @param string $iv Initialisation vector * @return string */ public static function _encryptRijndael256($str, $key, $iv = FALSE) { // If the string is empty if (empty($str)) { return ''; } // Open the cypher $cypher = mcrypt_module_open('rijndael-256', '', 'ofb', ''); // Create the IV if there is none if ($iv === FALSE) { $iv = self::_genRijndael256IV($cypher); } // Set Key $key = substr($key, 0, mcrypt_enc_get_key_size($cypher)); // Initialise encryption mcrypt_generic_init($cypher, $key, $iv); // Encrypt String $encrypted = mcrypt_generic($cypher, $strString); // Terminate encryption hander mcrypt_generic_deinit($cypher); // Close Module mcrypt_module_close($cypher); // Return encrypted string return $encrypted; }
/** */ public function encrypt($text) { mcrypt_generic_init($this->_mcrypt, $this->key, empty($this->iv) ? str_repeat('0', Horde_Crypt_Blowfish::IV_LENGTH) : $this->iv); $out = mcrypt_generic($this->_mcrypt, $this->_pad($text)); mcrypt_generic_deinit($this->_mcrypt); return $out; }
function tripleDESEncrypt1($src, $key) { if ($src == NULL) { return NULL; } $iv = $this->my_mcrypt_create_iv(); /* Open module, and create IV */ $td = mcrypt_module_open(MCRYPT_3DES, '', 'cbc', ''); if ($key == NULL) { $key = ""; } $key = substr($key, 0, mcrypt_enc_get_key_size($td)); /* Initialize encryption handle */ if (mcrypt_generic_init($td, $key, $iv) != -1) { /* Encrypt data */ $encryptedData = mcrypt_generic($td, $src); $iv_size = mcrypt_get_iv_size(MCRYPT_3DES, MCRYPT_MODE_CBC); $encryptedData = chr($iv_size) . $iv . $encryptedData; mcrypt_generic_deinit($td); //echo "src is:" . $src. "<br>\n"; //echo "iv is :" .bin2hex($iv)."<br>\n"; //echo "key is :" .bin2hex($key)."<br>\n"; } else { $encryptedData = null; } mcrypt_module_close($td); return $encryptedData; }
public static function encrypt($keyString, $value) { $origKey = $keyString; if (strlen($keyString) > 32) { $keyString = substr($keyString, 0, 32); } if (strlen($keyString) < 32) { $keyString = str_pad($keyString, 32, 'X'); } $cipher = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', MCRYPT_MODE_ECB, ''); $iv = TPSecurityUtils::genRandomString(16); if (mcrypt_generic_init($cipher, $keyString, $iv) != -1) { $blockSize = mcrypt_get_block_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_ECB); $padding = $blockSize - strlen($value) % $blockSize; $value .= str_repeat(chr($padding), $padding); // PHP pads with NULL bytes if $value is not a multiple of the block size.. $cipherText = mcrypt_generic($cipher, $value); mcrypt_generic_deinit($cipher); mcrypt_module_close($cipher); $safe = TPSecurityUtils::urlensafe($cipherText); return $safe . TPSecurityUtils::DELIM . TPSecurityUtils::hashHmacSha256($origKey, $safe); } $safe = TPSecurityUtils::urlensafe($value); return $safe . TPSecurityUtils::DELIM . TPSecurityUtils::hashHmacSha256($origKey, $safe); }
/** * Encrypt * * @return mixed string|null * @param object $value */ public function encrypt($value) { if (empty($value)) { return $value; } return base64_encode(mcrypt_generic($this->_handler, (string) $value)); }
/** * Ritorna la string codificata. * Se il costruttore ha fallito nell'inizializzazione dell'algoritmo di cifratura, la stringa viene * restituita in chiaro; altrimenti la codifica consiste nella cifratura con l'algoritmo scelto. * * @param string $str Se non viene passata nessuna stringa ritorna una stringa vuota. * @return string */ public function encrypt($str) { try { $str = filter_var($str, FILTER_SANITIZE_STRING); if (empty($str)) { throw new InvalidArgumentException('Empty or not valid string as agrument', E_USER_NOTICE); } if (!isset($this->_cipher)) { throw new UnexpectedValueException('Cypher error', E_USER_WARNING); } if (false === ($iv = mcrypt_create_iv(mcrypt_enc_get_iv_size($this->_cipher), MCRYPT_RAND))) { throw new UnexpectedValueException('Cannot initialize vector IV', E_USER_WARNING); } $s = mcrypt_generic_init($this->_cipher, $this->_key, $iv); if (0 > $s || false === $s) { throw new exceptions('Encrypt failure', E_USER_WARNING); } $enc = mcrypt_generic($this->_cipher, $str); mcrypt_generic_deinit($this->_cipher); return $enc; } catch (Exception $e) { debug::exception($e); return $str; } }
public function enkripsi($algoritma, $mode, $secretkey, $fileplain) { /* Membuka Modul untuk memilih Algoritma & Mode Operasi */ $td = mcrypt_module_open($algoritma, '', $mode, ''); /* Inisialisasi IV dan Menentukan panjang kunci yang digunakan */ $iv = mcrypt_create_iv(mcrypt_enc_get_iv_size($td), MCRYPT_RAND); $ks = mcrypt_enc_get_key_size($td); /* Menghasilkan Kunci */ $key = $secretkey; //echo "kuncinya : ". $key. "<br>"; /* Inisialisasi */ mcrypt_generic_init($td, $key, $iv); /* Enkripsi Data, dimana hasil enkripsi harus di encode dengan base64.\ Hal ini dikarenakan web browser tidak dapat membaca karakter-karakter\ ASCII dalam bentuk simbol-simbol */ $buffer = $fileplain; $encrypted = mcrypt_generic($td, $buffer); $encrypted1 = base64_encode($iv) . ";" . base64_encode($encrypted); $encrypted2 = base64_encode($encrypted1); $filecipher = $encrypted2; /* Menghentikan proses enkripsi dan menutup modul */ mcrypt_generic_deinit($td); mcrypt_module_close($td); return $filecipher; }
function aes128_encode($data, $mode) { switch ($mode) { case "ECB": case "CBC": if ($mode === "ECB") { $cipher = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', MCRYPT_MODE_ECB, ''); } else { $cipher = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', MCRYPT_MODE_CBC, ''); } $iv = mcrypt_create_iv(mcrypt_enc_get_iv_size($cipher), MCRYPT_RAND); $key = substr('naveen', 0, mcrypt_enc_get_key_size($cipher)); if (mcrypt_generic_init($cipher, $key, $iv) != 1) { $cipherData = mcrypt_generic($cipher, $data); mcrypt_generic_deinit($cipher); mcrypt_module_close($cipher); if ($mode === "ECB") { $sanitizedCipherData = trim(base64_encode($cipherData)); } else { $sanitizedCipherData = trim(base64_encode($iv) . "_" . base64_encode($cipherData)); } return $sanitizedCipherData; } else { return false; } break; default: return false; break; } }
static function encrypt($input, $base64 = true) { if (!$input || !strlen($input) > 0) { return null; } if (!($td = mcrypt_module_open('rijndael-256', '', 'ctr', ''))) { return null; } if (!($iv = mcrypt_create_iv(32, MCRYPT_RAND))) { return null; } $content = serialize($input); if (mcrypt_generic_init($td, MSettings::$c_key, $iv) !== 0) { return null; } $content = mcrypt_generic($td, $content); $content = $iv . $content; $mac = self::pbkdf2($content, MSettings::$c_key, 1000, 32); $content .= $mac; mcrypt_generic_deinit($td); mcrypt_module_close($td); if ($base64) { $content = base64_encode($content); } return $content; }
/** * Encrypt data * * This method will encrypt data using a given key, vector, and cipher. * By default, this will encrypt data using the RIJNDAEL/AES 256 bit cipher. You * may override the default cipher and cipher mode by passing your own desired * cipher and cipher mode as the final key-value array argument. * * @param string $data The unencrypted data * @param string $key The encryption key * @param string $iv The encryption initialization vector * @param array $settings Optional key-value array with custom algorithm and mode * @return string */ public static function encrypt($data, $key, $iv, $settings = array()) { if ($data === '' || !extension_loaded('mcrypt')) { return $data; } //Merge settings with defaults $defaults = array('algorithm' => MCRYPT_RIJNDAEL_256, 'mode' => MCRYPT_MODE_CBC); $settings = array_merge($defaults, $settings); //Get module $module = mcrypt_module_open($settings['algorithm'], '', $settings['mode'], ''); //Validate IV $ivSize = mcrypt_enc_get_iv_size($module); if (strlen($iv) > $ivSize) { $iv = substr($iv, 0, $ivSize); } //Validate key $keySize = mcrypt_enc_get_key_size($module); if (strlen($key) > $keySize) { $key = substr($key, 0, $keySize); } //Encrypt value mcrypt_generic_init($module, $key, $iv); $res = @mcrypt_generic($module, $data); mcrypt_generic_deinit($module); return $res; }
/** * Store an encrypted cookie * * @param string $cookieName * @param mixed $cookieValue * @param int $expiry default stores just for the browser session */ public static function set($cookieName, $cookieValue, $expiry = 0) { if (isset($_COOKIE['synsec'])) { $synsec = $_COOKIE['synsec']; } else { $synsec = Tools::randomString('12'); } if ((!isset($_SERVER['HTTPS']) || $_SERVER['HTTPS'] != 'on') && (!isset($_SERVER['HTTP_X_FORWARDED_PROTO']) || $_SERVER['HTTP_X_FORWARDED_PROTO'] != 'https')) { $ssl = false; } else { $ssl = true; } setcookie('synsec', $synsec, time() + 60 * 60 * 24 * 30, '/', $_SERVER['HTTP_HOST'], $ssl, true); $synsec .= 'synErgy' . self::$token; /* Open the cipher */ $td = mcrypt_module_open('rijndael-256', '', 'ofb', ''); /* Create the IV and determine the keysize length, use MCRYPT_RAND * on Windows instead */ $iv = mcrypt_create_iv(mcrypt_enc_get_iv_size($td), MCRYPT_DEV_RANDOM); $ks = mcrypt_enc_get_key_size($td); /* Create key */ $key = substr(md5($synsec), 0, $ks); /* Intialize encryption */ mcrypt_generic_init($td, $key, $iv); /* Encrypt data */ $encrypted = mcrypt_generic($td, serialize($cookieValue)); # Store our secure cookie setcookie($cookieName, trim(base64_encode($iv . '|' . $encrypted)), $expiry, '/', $_SERVER['HTTP_HOST'], $ssl, true); /* Terminate encryption handler */ mcrypt_generic_deinit($td); }
private function encrypt($key, $data, $cipher = MCRYPT_3DES, $hash = 'sha256') { $td = mcrypt_module_open($cipher, '', MCRYPT_MODE_ECB, ''); if ($td === false) { return false; } $iv = mcrypt_create_iv(mcrypt_enc_get_iv_size($td), MCRYPT_DEV_URANDOM); if ($iv === false) { return false; } $ks = mcrypt_enc_get_key_size($td); switch ($hash) { case 'md5': $key = substr($this->md5($key), 0, $ks); case 'sha256': $key = substr($this->sha256($key), 0, $ks); case 'sha512': $key = substr($this->sha512($key), 0, $ks); case 'none': default: $key = substr($key, 0, $ks); } mcrypt_generic_init($td, $key, $iv); $ret = mcrypt_generic($td, $data); mcrypt_generic_deinit($td); mcrypt_module_close($td); return $iv . $ret; }
public function encrypt($string) { mcrypt_generic_init($this->cipher, $this->key, $this->iv); $cipherText = mcrypt_generic($this->cipher, $string); mcrypt_generic_deinit($this->cipher); return $cipherText; }
/** * @param string $string * @return string */ protected function mcrypt($string) { $this->encryptInit(); $encrypted = mcrypt_generic($this->getEncryptionDescriptor(), $string); $this->encryptDeinit(); return $encrypted; }
/** * 对明文进行加密 * @param string $text 需要加密的明文 * @return string 加密后的密文 */ public function encrypt($text, $appid) { try { //获得16位随机字符串,填充到明文之前 $random = $this->getRandomStr(); $text = $random . pack("N", strlen($text)) . $text . $appid; // 网络字节序 $size = mcrypt_get_block_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC); $module = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', MCRYPT_MODE_CBC, ''); $iv = substr($this->key, 0, 16); //使用自定义的填充方式对明文进行补位填充 $pkc_encoder = new Pkcs7Encoder(); $text = $pkc_encoder->encode($text); mcrypt_generic_init($module, $this->key, $iv); //加密 $encrypted = mcrypt_generic($module, $text); mcrypt_generic_deinit($module); mcrypt_module_close($module); //使用BASE64对加密后的字符串进行编码 return base64_encode($encrypted); } catch (Exception $e) { @error_log('Encrypt AES Error: ' . $e->getMessage(), 0); return FALSE; } }
function ssl_encode($data, $key = '') { // Use the Encrypt.php function get_key to encode the data. $key = $this->get_key($key); // Set a random salt $salt = substr(md5(mt_rand(), true), 8); $block = mcrypt_get_block_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC); $pad = $block - strlen($data) % $block; $data = $data . str_repeat(chr($pad), $pad); // Setup encryption parameters $td = mcrypt_module_open(MCRYPT_RIJNDAEL_128, "", MCRYPT_MODE_CBC, ""); $key_len = mcrypt_enc_get_key_size($td); $iv_len = mcrypt_enc_get_iv_size($td); $total_len = $key_len + $iv_len; $salted = ''; $dx = ''; // Salt the key and iv while (strlen($salted) < $total_len) { $dx = md5($dx . $key . $salt, true); $salted .= $dx; } $key = substr($salted, 0, $key_len); $iv = substr($salted, $key_len, $iv_len); mcrypt_generic_init($td, $key, $iv); $encrypted_data = mcrypt_generic($td, $data); mcrypt_generic_deinit($td); mcrypt_module_close($td); return chunk_split(base64_encode('Salted__' . $salt . $encrypted_data), 32, "\n"); }
/** * 对字符串进行加密 * * @param string $string 要加密的字符串 * * @return string */ public function encode($string) { mcrypt_generic_init($this->td, $this->key, $this->iv); $data = base64_encode(mcrypt_generic($this->td, $string)); mcrypt_generic_deinit($this->td); return $data; }
/** Encryption Procedure * @param mixed msg message/data * @param string k encryption key * @param bool base64 base64 encode result * * @return string iv+ciphertext+mac or * boolean false on error */ public function encrypt($msg, $base64 = false) { $k = $this->key; # open cipher module (do not change cipher/mode) if (!($td = mcrypt_module_open('rijndael-128', '', 'ctr', ''))) { return false; } $iv = mcrypt_create_iv(16, MCRYPT_RAND); # create iv if (mcrypt_generic_init($td, $k, $iv) !== 0) { # initialize buffers return false; } $msg = mcrypt_generic($td, $msg); # encrypt $msg = $iv . $msg; # prepend iv $mac = $this->pbkdf2($msg, $k, 1000, 16); # create mac $msg .= $mac; # append mac mcrypt_generic_deinit($td); # clear buffers mcrypt_module_close($td); # close cipher module if ($base64) { $msg = base64_encode($msg); # base64 encode? } return $msg; # return iv+ciphertext+mac }
/** * Encrypt * * @param string $data Data to encrypt * * @return string */ public function encrypt($data) { mcrypt_generic_init($this->module, $this->key, $this->iv); $encrypted = mcrypt_generic($this->module, $data); mcrypt_generic_deinit($this->module); return $encrypted; }
public function encrypt($input, $is_id = false) { static $_map = array(); if ($is_id) { $input = base_convert($input, 10, 36); } $hashkey = md5($input . $this->key); if (isset($_map[$hashkey])) { return $_map[$hashkey]; } $size = mcrypt_get_block_size(MCRYPT_3DES, 'ecb'); $input = $this->pkcs5_pad($input, $size); $key = str_pad($this->key, 24, '0'); $td = mcrypt_module_open(MCRYPT_3DES, '', 'ecb', ''); $iv = @mcrypt_create_iv(mcrypt_enc_get_iv_size($td), MCRYPT_RAND); @mcrypt_generic_init($td, $key, $iv); $data = mcrypt_generic($td, $input); mcrypt_generic_deinit($td); mcrypt_module_close($td); $tmp = ''; if ($is_id) { $len = strlen($data); for ($i = 0; $i < $len; $i++) { $tmp = $tmp . str_pad(dechex(ord($data[$i])), 2, 0, STR_PAD_LEFT); } $_map[$hashkey] = $tmp; return $tmp; } $_map[$hashkey] = $tmp; $data = base64_encode($data); return $data; }
/** Encryption Procedure * * @param mixed msg message/data * @param string k encryption key * @param string iv initialize vector * @param boolean base64 base64 encode result * * @return string iv+ciphertext+mac or * boolean false on error */ public static function encrypt($msg, $k, $iv, $base64 = false) { # open cipher module (do not change cipher/mode) if (!($td = mcrypt_module_open('rijndael-256', '', 'ctr', ''))) { return false; } $msg = serialize($msg); # serialize if (mcrypt_generic_init($td, $k, $iv) !== 0) { # initialize buffers return false; } $msg = mcrypt_generic($td, $msg); # encrypt $msg = $iv . $msg; # prepend iv $mac = Cryptastic::pbkdf2($msg, $k, 1000, 32); # create mac $msg .= $mac; # append mac mcrypt_generic_deinit($td); # clear buffers mcrypt_module_close($td); # close cipher module if ($base64) { $msg = base64_encode($msg); } # base64 encode? return $msg; # return iv+ciphertext+mac }
public function crypt($data) { mcrypt_generic_init($this->_cipher, $this->_key, $this->_iv); $result = base64_encode(mcrypt_generic($this->_cipher, $data)); mcrypt_generic_deinit($this->_cipher); return base64_encode($this->_iv . ':' . $result); }
/** * Encrypt a value * * @param mixed $varValue The value to encrypt * @param string $strKey An optional encryption key * * @return string The encrypted value */ public static function encrypt($varValue, $strKey = null) { // Recursively encrypt arrays if (is_array($varValue)) { foreach ($varValue as $k => $v) { $varValue[$k] = static::encrypt($v); } return $varValue; } elseif ($varValue == '') { return ''; } // Initialize the module if (static::$resTd === null) { static::initialize(); } if (!$strKey) { $strKey = \Config::get('encryptionKey'); } $iv = mcrypt_create_iv(mcrypt_enc_get_iv_size(static::$resTd), MCRYPT_RAND); mcrypt_generic_init(static::$resTd, md5($strKey), $iv); $strEncrypted = mcrypt_generic(static::$resTd, $varValue); $strEncrypted = base64_encode($iv . $strEncrypted); mcrypt_generic_deinit(static::$resTd); return $strEncrypted; }
static function ec($msg, $key, $urlencode = false, $base64 = false) { try { if (!($td = mcrypt_module_open('rijndael-256', '', 'ctr', ''))) { return false; } $msg = serialize($msg); //create iv $iv = self::gen_iv(); if (mcrypt_generic_init($td, $key, $iv) !== 0) { return false; } $msg = mcrypt_generic($td, $msg); $msg = $iv . $msg; $mac = self::pbkdf2(true); $msg .= $mac; // clear buffers and close module mcrypt_generic_deinit($td); mcrypt_module_close($td); //encode as needed.. if ($base64) { $msg = base64_encode($msg); } if ($urlencode) { $msg = rawurlencode($msg); } return $msg; } catch (Exception $e) { return false; } }
public function computeSign($sharedSecret) { if (!$this->isValid) { throw new Exception(__METHOD__ . ": Message was not validated."); } try { $bytesHash = sha1($this->GetSignatureBase(), true); $sharedSecret = pack('H*', $sharedSecret); // uprava pre PHP < 5.0 if (strlen($bytesHash) != 20) { $bytes = ""; for ($i = 0; $i < strlen($bytesHash); $i += 2) { $bytes .= chr(hexdec(substr($str, $i, 2))); } $bytesHash = $bytes; } $cipher = mcrypt_module_open(MCRYPT_RIJNDAEL_128, "", MCRYPT_MODE_ECB, ""); $iv = mcrypt_create_iv(mcrypt_enc_get_iv_size($cipher), MCRYPT_RAND); mcrypt_generic_init($cipher, $sharedSecret, $iv); $text = $this->pad(substr($bytesHash, 0, 16), mcrypt_get_block_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_ECB)); $bytesSign = mcrypt_generic($cipher, $text); mcrypt_generic_deinit($cipher); mcrypt_module_close($cipher); $sign = substr(strtoupper(bin2hex($bytesSign)), 0, 32); } catch (Exception $e) { return false; } return $sign; }
/** * @param string $api_key API ключ UserEcho * @param string $project_key Ключ UserEcho * @param array $user_info * * @return SSO KEY */ public static function get_sso_token($api_key, $project_key, $user_info) { $sso_key = ''; if ($uid = get_uid(false)) { $user = new users(); $user->GetUserByUID($uid); $iv = str_shuffle('memoKomo1234QWER'); $message = array('guid' => $uid, 'expires_date' => gmdate('Y-m-d H:i:s', time() + 86400), 'display_name' => $user->login, 'email' => $user->email, 'locale' => 'ru', 'verified_email' => true); // key hash, length = 16 $key_hash = substr(hash('sha1', $api_key . $project_key, true), 0, 16); $message_json = json_encode(encodeCharset('CP1251', 'UTF-8', $message)); // double XOR first block message_json for ($i = 0; $i < 16; ++$i) { $message_json[$i] = $message_json[$i] ^ $iv[$i]; } // fill tail of message_json by bytes equaled count empty bytes (to 16) $pad = 16 - strlen($message_json) % 16; $message_json = $message_json . str_repeat(chr($pad), $pad); // encode json $cipher = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', 'cbc', ''); mcrypt_generic_init($cipher, $key_hash, $iv); $encrypted_bytes = mcrypt_generic($cipher, $message_json); mcrypt_generic_deinit($cipher); // encode bytes to url safe string $sso_key = urlencode(base64_encode($encrypted_bytes)); } return $sso_key; }
/** * 对明文进行加密 * @param string $text 需要加密的明文 * @return string 加密后的密文 */ public function encrypt($text, $appid) { try { //获得16位随机字符串,填充到明文之前 $random = $this->getRandomStr(); $text = $random . pack("N", strlen($text)) . $text . $appid; // 网络字节序 $size = mcrypt_get_block_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC); $module = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', MCRYPT_MODE_CBC, ''); $iv = substr($this->key, 0, 16); //使用自定义的填充方式对明文进行补位填充 $pkc_encoder = new PKCS7Encoder(); $text = $pkc_encoder->encode($text); mcrypt_generic_init($module, $this->key, $iv); //加密 $encrypted = mcrypt_generic($module, $text); mcrypt_generic_deinit($module); mcrypt_module_close($module); //print(base64_encode($encrypted)); //使用BASE64对加密后的字符串进行编码 return array(ErrorCode::$OK, base64_encode($encrypted)); } catch (Exception $e) { //print $e; return array(ErrorCode::$EncryptAESError, NULL); } }
/** * Encryption Procedure * * @param mixed $msg message/data * @param string $key encryption key * @param boolean $base64 base64 encode result * @param string $algorythm mcrypt algorithm * @param string $mode mcrypt mode * * @throws \Exception * @return string iv+ciphertext+mac */ public function encrypt($msg, $key, $base64 = false, $algorythm, $mode) { if (!in_array($algorythm, mcrypt_list_algorithms())) { throw new Exception('Wrong mcrypt algorithm. Use mcrypt_list_algorithms() for list available algorythm.'); } if (!in_array($mode, mcrypt_list_modes())) { throw new Exception('Wrong mcrypt mode. Use mcrypt_list_modes() for list available mode.'); } if (!($td = mcrypt_module_open($algorythm, '', $mode, ''))) { throw new Exception('Can not open mcrypt module'); } $msg = serialize($msg); $initializationVector = mcrypt_create_iv(mcrypt_enc_get_iv_size($td), MCRYPT_DEV_RANDOM); if (mcrypt_generic_init($td, $key, $initializationVector) !== 0) { throw new Exception('Can not init mcrypt'); } try { $msg = mcrypt_generic($td, $msg); $msg = $initializationVector . $msg; $mac = $this->pbkdf2($msg, $key, 1000, 32); $msg .= $mac; mcrypt_generic_deinit($td); mcrypt_module_close($td); if ($base64) { $msg = base64_encode($msg); } return $msg; } catch (Exception $e) { return 'Caught exception: ' . $e->getMessage(); } }
public function encryptMsg($text) { $token = $this->account['token']; $encodingaeskey = $this->account['encodingaeskey']; $appid = $this->account['key']; $key = base64_decode($encodingaeskey . '='); $text = random(16) . pack("N", strlen($text)) . $text . $appid; $size = mcrypt_get_block_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC); $module = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', MCRYPT_MODE_CBC, ''); $iv = substr($key, 0, 16); $block_size = 32; $text_length = strlen($text); $amount_to_pad = $block_size - $text_length % $block_size; if ($amount_to_pad == 0) { $amount_to_pad = $block_size; } $pad_chr = chr($amount_to_pad); $tmp = ''; for ($index = 0; $index < $amount_to_pad; $index++) { $tmp .= $pad_chr; } $text = $text . $tmp; mcrypt_generic_init($module, $key, $iv); $encrypted = mcrypt_generic($module, $text); mcrypt_generic_deinit($module); mcrypt_module_close($module); $encrypt_msg = base64_encode($encrypted); $signature = $this->buildSignature($encrypt_msg); return array($signature, $encrypt_msg); }
/** * {@inheritdoc} */ public function encrypt($data) { $this->init(); $data = trim(base64_encode(mcrypt_generic($this->module, $data))); $this->close(); return $data; }