企业🤖AI智能体构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
//>unicode解码函数,如\u6d4b\u8bd5\u6570\u503c转为测试数值(unicode值) public function unicode_decode($str){ $str = str_replace("\\","%",$str); $str = rawurldecode($str); $r = []; preg_match_all("/(?:%u.{4})|.{4};|&#\d+;|.+/U",$str,$r); $ar = $r[0]; foreach($ar as $k=>$v) { if (substr($v, 0, 2) === "%u") { $ar[$k] = iconv("UCS-2", "utf-8", pack("H4", substr($v, -4))); } elseif (substr($v, 0, 3) === "") { $ar[$k] = iconv("UCS-2", "utf-8", pack("H4", substr($v, 3, -1))); } elseif (substr($v, 0, 2) === "&#") { $ar[$k] = iconv("UCS-2", "utf-8", pack("n", substr($v, 2, -1))); } } return join("",$ar); } //>SGML 类语言的转义(中文) public function sgml_encode($str,$encoding='utf-8',$prefix ='&#',$postfix=';'){ $str = iconv($encoding, 'UCS-2//IGNORE', $str); $arrstr = str_split($str, 2); $unistr = ''; for($i = 0, $len = count($arrstr); $i < $len; $i++) { $dec = hexdec(bin2hex($arrstr[$i])); $unistr .= $prefix . $dec . $postfix; } return $unistr; } //>字符串SBH式加密[暂不支持中文](字符串) public function SBHJM($str){ $str = self::sbh_split($str); $num = []; //>数字容器 $text = []; //>其他字符容器 //>分离数字与非数字 foreach ($str as $lab => $val){ is_numeric($val)?$num[] = array($lab,$val):$text[] = array($lab,$val); } //>定义函数常量 $fenge = "^:^"; $rand = []; $rand[] = "%"; $rand[] = "^"; $rand[] = "&"; $rand[] = "*"; $rand[] = "("; $rand[] = ")"; //>数字处理 if (empty($num)){ $num_obj = "m86j10x".$fenge."i31gk"; }else { $num_obj_0 = []; $num_obj_1 = []; $num_obj_1[] = "1"; foreach ($num as $val){ $num_obj_0[] = $val[0].$rand[rand(0,5)]; $num_obj_1[] = $val[1]; } //>判断位数处理 $duan = ceil(count($num_obj_1)/9); $num_obj_tmp = []; for ($i=0;$i<$duan;$i++){ $obj = []; for ($j=($i*9);$j<(($i+1)*9);$j++){ $obj[] = isset($num_obj_1[$j])?$num_obj_1[$j]:''; } $num_obj_tmp[] = decoct(implode($obj)); } $num_obj_tmp = implode(".",$num_obj_tmp); $num_obj = strrev(implode($num_obj_0)).$fenge.$num_obj_tmp; } //>非数字处理 if (empty($text)){ $text_obj = "&*%~!@"; }else { $text_obj = []; foreach ($text as $lab => $val){ $text_obj[] = $val[1].$val[0]; } $text_obj = strrev(implode($text_obj)); } $secret = $num_obj.$fenge.$text_obj; return $secret; } //>字符串SBH式解密[暂不支持中文](字符串) public function unSBHJM($str){ $obj = []; $fenge = "^:^"; $str = explode($fenge,$str); if ($str[0]!="m86j10x"&&$str[1]!="i31gk"){ $num_obj_0 = explode("~",preg_replace("/[\%\^\&\*\(\)]/","~",strrev($str[0]))); $tmp = []; foreach ($num_obj_0 as $val){ $val === ""? NULL :$tmp[]=$val; } $num_obj_0 = $tmp; $num_obj_1 = explode(".",$str[1]); $num_obj_tmp = []; foreach ($num_obj_1 as $lab => $val){ $val_8 = octdec($val); //>八进制转十进制 if ($lab>=(count($num_obj_1)-1)||strlen($val_8)==9){ $num_obj_tmp[] = $val_8; }else { $bqz = 9-strlen($val_8); //>补全数 $num = []; for ($i=0;$i<$bqz;$i++){ $num[] = "0"; } $num_obj_tmp[] = implode($num).$val_8; } } //>尾数补全 $tmp_lab = strlen(implode($num_obj_tmp)); //>键字符数 $tmp_val = count($num_obj_0); //>值字符数 if ($tmp_lab<=$tmp_val){ $bqz = $tmp_val - $tmp_lab; $num = []; for ($i=0;$i<=$bqz;$i++){ $num[] = "0"; } $num_obj_tmp[(count($num_obj_tmp)-1)] = implode($num).$num_obj_tmp[(count($num_obj_tmp)-1)]; } $num_obj_tmp = self::sbh_split(substr(implode($num_obj_tmp),1)); foreach ($num_obj_0 as $lab => $val){ $obj[$val] = $num_obj_tmp[$lab]; } } if ($str[2]!="&*%~!@"){ preg_match_all("/[0-9]+[a-zA-Z\/\~\!\@\#\$\%\^\&\*\(\)\_\-\=\+\{\}\'\"\?\|\<\>\,\.\;\:\[\]\\\]/",$str[2],$text_obj); $text_obj_0 = []; $text_obj_1 = []; foreach ($text_obj[0] as $val){ preg_match("/[a-zA-Z\/\~\!\@\#\$\%\^\&\*\(\)\_\-\=\+\{\}\'\"\?\|\<\>\,\.\;\:\[\]\\\]/",$val,$tmp); $text_obj_0[] = strrev(str_replace($tmp[0],"",$val)); $text_obj_1[] = $tmp[0]; } foreach ($text_obj_0 as $lab => $val){ $obj[$val] = $text_obj_1[$lab]; } } ksort($obj); return implode($obj); } //>64加密算法 function encode_64($data, $key){ $key = md5($key); $x = 0; $len = strlen($data); $l = strlen($key); $char = ''; for ($i = 0; $i < $len; $i++){ if ($x == $l){ $x = 0; } $char .= $key{$x}; $x++; } $str = ''; for ($i = 0; $i < $len; $i++){ $str .= chr(ord($data{$i}) + (ord($char{$i})) % 256); } return base64_encode($str); } //>64解密算法 function decode_64($data, $key){ $key = md5($key); $x = 0; $data = base64_decode($data); $len = strlen($data); $l = strlen($key); $char = ''; for ($i = 0; $i < $len; $i++){ if ($x == $l) { $x = 0; } $char .= substr($key, $x, 1); $x++; } $str = ''; for ($i = 0; $i < $len; $i++){ if (ord(substr($data, $i, 1)) < ord(substr($char, $i, 1))){ $str .= chr((ord(substr($data, $i, 1)) + 256) - ord(substr($char, $i, 1))); }else{ $str .= chr(ord(substr($data, $i, 1)) - ord(substr($char, $i, 1))); } } return $str; } //>APP数据加解密(传输字段,加密密钥,操作类型) public function appCode($string, $auth_key, $operation='ENCODE') { $key = md5($auth_key); $key_length = strlen($key); //base64_decode 默认是 iso-8859-1 转 UTF-8 用 utf8_decode(base64_decode($string)) $string = $operation == 'DECODE' ? utf8_decode(base64_decode($string)) : substr(md5($string.$key), 0, 8).$string; $string_length = strlen($string); $rndkey = $box = array(); $result = ''; for($i = 0; $i <= 255; $i++) { $rndkey[$i] = ord($key[$i % $key_length]); $box[$i] = $i; } for($j = $i = 0; $i < 256; $i++) { $j = ($j + $box[$i] + $rndkey[$i]) % 256; $tmp = $box[$i]; $box[$i] = $box[$j]; $box[$j] = $tmp; } for($a = $j = $i = 0; $i < $string_length; $i++) { $a = ($a + 1) % 256; $j = ($j + $box[$a]) % 256; $tmp = $box[$a]; $box[$a] = $box[$j]; $box[$j] = $tmp; $result .= chr(ord($string[$i]) ^ ($box[($box[$a] + $box[$j]) % 256])); } if($operation == 'DECODE') { if(substr($result, 0, 8) == substr(md5(substr($result, 8).$key), 0, 8)) { return substr($result, 8); } else { return ''; } } else { //base64_encode 默认是 iso-8859-1 转 UTF-8 用 base64_encode(utf8_encode($result)) return str_replace('=', '',base64_encode(utf8_encode($result))); } } //>将字符串内容完全分离成单个字符数组(字符串) private function sbh_split($string){ return preg_split('/(?<!^)(?!$)/u',$string); } //>私钥加密函数(待加密字符,私钥内容) public function ras_encond_pri($data, $private_key) { $data = str_split($data, 117); $outstr = ''; foreach ($data as $val) { openssl_private_encrypt($val, $encryptData, $private_key); $outstr .= $encryptData; } return base64_encode($outstr); } //>公钥解密函数(密文,公钥内容) public function ras_decond_pub($data,$public_key) { $data = str_split(base64_decode($data), 128); $outstr = ''; foreach ($data as $val) { openssl_public_decrypt($val, $output, $public_key); $outstr .= $output; } return $outstr; } //>公钥加密函数(待加密字符,公钥内容) public function ras_encond_pub($data, $public_key) { $data = str_split($data, 117); $outstr = ''; foreach ($data as $val) { openssl_public_encrypt($val, $encryptData, $public_key); $outstr .= $encryptData; } return base64_encode($outstr); } //>私钥解密函数(密文,私钥内容) public function ras_decond_pri($data, $private_key) { $data = str_split(base64_decode($data), 128); $outstr = ''; foreach ($data as $val) { openssl_private_decrypt($val, $output, $private_key); $outstr .= $output; } return $outstr; }##### 类调用方法 ~~~ $T_C = new char\tool(); ~~~