当前位置: 首页 > 后端技术 > PHP

PHP实现摩斯电码加解密

时间:2023-03-30 02:36:20 PHP

摩斯电码加解密demo'/','short'=>'.','long'=>'-'];/***@vararray摩尔斯电码转换*/protectedstatic$standard=['A'=>'01','B'=>'1000','C'=>'1010','D'=>'100','E'=>'0','F'=>'0010','G'=>'110','H'=>'0000','I'=>'00','J'=>'0111','K'=>'101','L'=>'0100','M'=>'11','N'=>'10','O'=>'111','P'=>'0110','Q'=>'1101','R'=>'010','S'=>'000','T'=>'1','U'=>'001','V'=>'0001','W'=>'011','X'=>'1001','Y'=>'1011','Z'=>'1100','0'=>'11111','1'=>'01111','2'=>'00111','3'=>'00011','4'=>'00001','5'=>'00000','6'=>'10000','7'=>'11000','8'=>'11100','9'=>'11110','.'=>'010101',','=>'110011','?'=>'001100','\''=>'011110','!'=>'101011','/'=>'10010','('=>'10110',')'=>'101101','&'=>'01000','=>'=>'111000',';'=>'101010','='=>'10001','+'=>'01010','-'=>'100001','_'=>'001101','"'=>'010010','$'=>'0001001','@'=>'011010',];/***@varnullMoss转换结果*/protectedstatic$standardReverse=null;/***默认参数配置*/protected静态函数defaultOption($option=null){$option=$选项||[];返回[isset($option['space'])?$option['space']:self::$option['space'],isset($option['short'])?$option['short']:self::$option['short'],isset($option['long'])?$option['long']:self::$option['long']];}/***使用utf分割字符串*/protectedstaticfunctionmbStrSplit($str){$len=1;$开始=0;$strlen=mb_strlen($str);while($strlen){$array[]=mb_substr($str,$start,$len,"utf8");$str=mb_substr($str,$len,$strlen,"utf8");$strlen=mb_strlen($str);}返回$array;}/***将Unicode字符转换为utf8字符*/protectedstaticfunctionunicodeToUtf8($unicode_str){$utf8_str='';$code=intval(hexdec($unicode_str));//注意转换后的code必须是整数,这样才能正确的按位运算$ord_1=decbin(0xe0|($code>>12));$ord_2=decbin(0x80|(($code>>6)&0x3f));$ord_3=decbin(0x80|($code&0x3f));$utf8_str=chr(bindec($ord_1))。chr(bindec($ord_2))。chr(bindec($ord_3));返回$utf8_str;}受保护的静态函数charCodeAt($str,$index){$char=mb_substr($str,$index,1,'UTF-8');如果(mb_check_encoding($char,'UTF-8')){$ret=mb_convert_encoding($char,'UTF-32BE','UTF-8');返回十六进制(bin2hex($ret));}else{返回空值;}}/***摩斯代码转换文档*/protectedstaticfunctionmorseHexUnicode($mor){$mor=bindec($mor);如果(!$mor){返回'';}else{$mor=dechex($mor);}返回self::unicodeToUtf8($mor);}/***unicode转二进制*/publicstaticfunctionunicodeHexMorse($ch){$r=[];$length=mb_strlen($ch,'UTF8');对于($i=0;$i<$length;$i++){$r[$i]=substr(('00'.dechex(self::charCodeAt($ch,$i))),-4);}$r=join('',$r);返回base_convert(hexdec($r),10,2);}/***加密摩尔斯电码*/publicstaticfunctionencode($text,$option=null){$option=self::defaultOption($option);//默认参数$morse=[];//最终的莫尔斯结果//删除空格,转换为大写,拆分为数组$text=self::mbStrSplit(strtoupper(str_replace('','',$text)));foreach($textas$key=>$ch){$r=@self::$standard[$ch];如果(!$r){$r=self::unicodeHexMorse($ch);//没有找到,说明是非标准字符,使用unicode}$morse[]=str_replace('1',$option[2],str_replace('0',$option[1],$r));}returnjoin($option[0],$morse);}/***解密摩尔斯电码*/publicstaticfunctiondecode($morse,$option=null){if(self::$standardReverse===null){foreach(self::$standardas$key=>$值){self::$standardReverse[$value]=$key;$option=self::defaultOption($option);$味精=[];$morse=explode($option[0],$morse);//拆分成数组foreach($morseas$key=>$mor){$mor=str_replace('','',$mor);$mor=str_replace($option[2],'1',str_replace($option[1],'0',$mor));$r=@self::$standardReverse[$mor];如果(!$r){$r=self::morseHexUnicode($mor);//找不到,说明是morse的非标准字符,使用unicode解析方式。}$味精[]=$r;}returnjoin('',$msg);}}摩尔斯电码加密:$txt=''//要加密的字符$morse=Morse::encode($txt);摩尔斯电码解密:$morse=''//待解密的摩尔斯电码$morse=Morse::decode($morse);例子如下:$txt='test';$morse=Morse::encode($txt);echo$morse。"
";$txt=Morse::decode($morse);echo$txt;输入结果如下: