/** * 解密 * @method decode * @return string * @author NewFuture */ public static function decode($str) { $str = base64_decode($str); if ($key = Kv::get('RSA_pri_key')) { $pri_key = openssl_pkey_get_private($key); return openssl_private_decrypt($str, $decrypted, $pri_key) ? $decrypted : false; } return false; }
/** * 获取cookie配置 * @method config * @param [string] $name [配置变量名] * @return [mixed] [description] * @author NewFuture */ private static function config($name) { if (!($config = self::$_config)) { $config = Config::get('cookie'); if (!($key = Kv::get('COOKIE_aes_key'))) { /*重新生成加密密钥*/ $key = Random::word(32); Kv::set('COOKIE_aes_key', $key); } $config['key'] = $key; self::$_config = $config; } return isset($config[$name]) ? $config[$name] : null; }
/** * cipher_table($key) * 获取密码表 * 现在缓存中查询,如果存在,则直接读取,否则重新生成 * @param $key 加密的密钥 * @return array 密码映射表 */ private static function _cipherTable($key) { $tableName = 'et_' . urlencode($key); //缓存表名称 if ($table = Kv::get($tableName)) { /*读取缓存中的密码表*/ $table = unserialize($table); } else { /*密码表不存在则重新生成*/ //对所有数字,逐个进行AES加密生成密码表 $td = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', MCRYPT_MODE_ECB, ''); mcrypt_generic_init($td, $key, '0000000000000000'); for ($i = 0; $i < 10000; ++$i) { $table[] = mcrypt_generic($td, $i); } mcrypt_generic_deinit($td); sort($table); //根据加密后内容排序得到密码表 Kv::set($tableName, serialize($table)); //缓存密码表 } return $table; }
/** * cipher_table($key) * 获取密码表 * 现在缓存中查询,如果存在,则直接读取,否则重新生成 * @param $key 加密的密钥 * @return array 密码映射表 */ private static function _cipherTable($key) { $tableName = 'et_' . $key; //缓存表名称 $table = Kv::get($tableName); //读取缓存中的密码表 if (!$table) { //密码表不存在则重新生成 //对所有数字,逐个进行AES加密生成密码表 $td = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', MCRYPT_MODE_ECB, ''); mcrypt_generic_init($td, $key, '0000000000000000'); for ($i = 0; $i < 10000; ++$i) { $table[] = mcrypt_generic($td, $i); } mcrypt_generic_deinit($td); sort($table); //根据加密后内容排序得到密码表 Kv::set($tableName, $table); //缓存密码表 } return $table; }
/** * 获取加密密钥 * @method key * @return [type] [description] * @author NewFuture */ public static function key() { if (!($key = Kv::get('COOKIE_aes_key'))) { /*重新生成加密密钥*/ $key = Random::word(32); Kv::set('COOKIE_aes_key', $key); } return $key; }