PHP实现JS颜文字编码解码过程-aaencode+aadecode

Eller · · 41 次点击 · · 开始浏览    

PHP实现JS颜文字加密解密过程-aaencode

调试某网站JS时出现了一堆萌萌哒的代码,且能执行成功,对此感到好奇进行研究并实现了PHP端的编码解码过程。

例如,如下代码在浏览器控制台执行后会输出一段文本。

゚ω゚ノ= /`m´)ノ ~┻━┻ //*´∇`*/ ['_']; o=(゚ー゚) =_=3; c=(゚Θ゚) =(゚ー゚)-(゚ー゚); (゚Д゚) =(゚Θ゚)= (o^_^o)/ (o^_^o);(゚Д゚)={゚Θ゚: '_' ,゚ω゚ノ : ((゚ω゚ノ==3) +'_') [゚Θ゚] ,゚ー゚ノ :(゚ω゚ノ+ '_')[o^_^o -(゚Θ゚)] ,゚Д゚ノ:((゚ー゚==3) +'_')[゚ー゚] }; (゚Д゚) [゚Θ゚] =((゚ω゚ノ==3) +'_') [c^_^o];(゚Д゚) ['c'] = ((゚Д゚)+'_') [ (゚ー゚)+(゚ー゚)-(゚Θ゚) ];(゚Д゚) ['o'] = ((゚Д゚)+'_') [゚Θ゚];(゚o゚)=(゚Д゚) ['c']+(゚Д゚) ['o']+(゚ω゚ノ +'_')[゚Θ゚]+ ((゚ω゚ノ==3) +'_') [゚ー゚] + ((゚Д゚) +'_') [(゚ー゚)+(゚ー゚)]+ ((゚ー゚==3) +'_') [゚Θ゚]+((゚ー゚==3) +'_') [(゚ー゚) - (゚Θ゚)]+(゚Д゚) ['c']+((゚Д゚)+'_') [(゚ー゚)+(゚ー゚)]+ (゚Д゚) ['o']+((゚ー゚==3) +'_') [゚Θ゚];(゚Д゚) ['_'] =(o^_^o) [゚o゚] [゚o゚];(゚ε゚)=((゚ー゚==3) +'_') [゚Θ゚]+ (゚Д゚) .゚Д゚ノ+((゚Д゚)+'_') [(゚ー゚) + (゚ー゚)]+((゚ー゚==3) +'_') [o^_^o -゚Θ゚]+((゚ー゚==3) +'_') [゚Θ゚]+ (゚ω゚ノ +'_') [゚Θ゚]; (゚ー゚)+=(゚Θ゚); (゚Д゚)[゚ε゚]='\\'; (゚Д゚).゚Θ゚ノ=(゚Д゚+ ゚ー゚)[o^_^o -(゚Θ゚)];(o゚ー゚o)=(゚ω゚ノ +'_')[c^_^o];(゚Д゚) [゚o゚]='\"';(゚Д゚) ['_'] ( (゚Д゚) ['_'] (゚ε゚+(゚Д゚)[゚o゚]+ (゚Д゚)[゚ε゚]+(゚Θ゚)+ (゚ー゚)+ (o^_^o)+ (゚Д゚)[゚ε゚]+(゚Θ゚)+ ((゚ー゚) + (゚Θ゚))+ ((゚ー゚) + (o^_^o))+ (゚Д゚)[゚ε゚]+(゚Θ゚)+ ((゚ー゚) + (゚Θ゚))+ ((o^_^o) +(o^_^o))+ (゚Д゚)[゚ε゚]+(゚Θ゚)+ ((o^_^o) +(o^_^o))+ (o^_^o)+ (゚Д゚)[゚ε゚]+(゚Θ゚)+ ((゚ー゚) + (゚Θ゚))+ ((゚ー゚) + (o^_^o))+ (゚Д゚)[゚ε゚]+(゚Θ゚)+ ((゚ー゚) + (゚Θ゚))+ (゚ー゚)+ (゚Д゚)[゚ε゚]+(゚Θ゚)+ (゚ー゚)+ ((゚ー゚) + (゚Θ゚))+ (゚Д゚)[゚ε゚]+((゚ー゚) + (゚Θ゚))+ ((o^_^o) +(o^_^o))+ (゚Д゚)[゚ε゚]+(゚Θ゚)+ ((゚ー゚) + (゚Θ゚))+ (゚ー゚)+ (゚Д゚)[゚ε゚]+(゚Θ゚)+ ((゚ー゚) + (゚Θ゚))+ ((゚ー゚) + (o^_^o))+ (゚Д゚)[゚ε゚]+(゚Θ゚)+ (゚ー゚)+ ((゚ー゚) + (o^_^o))+ (゚Д゚)[゚ε゚]+((゚ー゚) + (゚Θ゚))+ (c^_^o)+ (゚Д゚)[゚ε゚]+(゚ー゚)+ ((o^_^o) - (゚Θ゚))+ (゚Д゚)[゚ε゚]+(o゚ー゚o)+ ((゚ー゚) + (゚Θ゚))+ ((゚ー゚) + (゚Θ゚))+ (゚Θ゚)+ (c^_^o)+ (゚Д゚)[゚ε゚]+(o゚ー゚o)+ ((゚ー゚) + (゚Θ゚))+ (゚Д゚) .゚Θ゚ノ+ ((゚ー゚) + (゚ー゚))+ (゚Д゚) .゚Θ゚ノ+ (゚Д゚)[゚ε゚]+(o゚ー゚o)+ ((゚ー゚) + (゚Θ゚))+ (゚Θ゚)+ (゚ー゚)+ (o^_^o)+ (゚Д゚)[゚ε゚]+(o゚ー゚o)+ ((o^_^o) +(o^_^o))+ ((o^_^o) +(o^_^o))+ (c^_^o)+ (゚Д゚) .゚Д゚ノ+ (゚Д゚)[゚ε゚]+(o゚ー゚o)+ ((o^_^o) +(o^_^o))+ (゚Д゚) .゚Д゚ノ+ (c^_^o)+ ((゚ー゚) + (゚Θ゚))+ (゚Д゚)[゚ε゚]+(゚ー゚)+ ((o^_^o) - (゚Θ゚))+ (゚Д゚)[゚ε゚]+((゚ー゚) + (゚Θ゚))+ (゚Θ゚)+ (゚Д゚)[゚ε゚]+((゚ー゚) + (o^_^o))+ (o^_^o)+ (゚Д゚)[゚o゚]) (゚Θ゚)) ('_');

 

这里附上原作者的aaencode编码地址,点击这里

网页源码里可以查看到具体的编码过程,摘录如下。

function aaencode( text )
{
    var t;
    var b = [
		"(c^_^o)",
		"(゚Θ゚)",
		"((o^_^o) - (゚Θ゚))",
		"(o^_^o)",
		"(゚ー゚)",
		"((゚ー゚) + (゚Θ゚))",
		"((o^_^o) +(o^_^o))",
		"((゚ー゚) + (o^_^o))",
		"((゚ー゚) + (゚ー゚))",
		"((゚ー゚) + (゚ー゚) + (゚Θ゚))",
		"(゚Д゚) .゚ω゚ノ",
		"(゚Д゚) .゚Θ゚ノ",
		"(゚Д゚) ['c']",
		"(゚Д゚) .゚ー゚ノ",
		"(゚Д゚) .゚Д゚ノ",
		"(゚Д゚) [゚Θ゚]"
        ];
	var r = "゚ω゚ノ= /`m´)ノ ~┻━┻   //*´∇`*/ ['_']; o=(゚ー゚)  =_=3; c=(゚Θ゚) =(゚ー゚)-(゚ー゚); "; 
	
	if( /ひだまりスケッチ×(365|356)\s*来週も見てくださいね[!!]/.test( text ) ){
		r += "X=_=3; ";
		r += "\r\n\r\n    X / _ / X < \"来週も見てくださいね!\";\r\n\r\n";
	}
    r += "(゚Д゚) =(゚Θ゚)= (o^_^o)/ (o^_^o);"+
        "(゚Д゚)={゚Θ゚: '_' ,゚ω゚ノ : ((゚ω゚ノ==3) +'_') [゚Θ゚] "+
        ",゚ー゚ノ :(゚ω゚ノ+ '_')[o^_^o -(゚Θ゚)] "+
        ",゚Д゚ノ:((゚ー゚==3) +'_')[゚ー゚] }; (゚Д゚) [゚Θ゚] =((゚ω゚ノ==3) +'_') [c^_^o];"+
        "(゚Д゚) ['c'] = ((゚Д゚)+'_') [ (゚ー゚)+(゚ー゚)-(゚Θ゚) ];"+
        "(゚Д゚) ['o'] = ((゚Д゚)+'_') [゚Θ゚];"+
        "(゚o゚)=(゚Д゚) ['c']+(゚Д゚) ['o']+(゚ω゚ノ +'_')[゚Θ゚]+ ((゚ω゚ノ==3) +'_') [゚ー゚] + "+
        "((゚Д゚) +'_') [(゚ー゚)+(゚ー゚)]+ ((゚ー゚==3) +'_') [゚Θ゚]+"+
        "((゚ー゚==3) +'_') [(゚ー゚) - (゚Θ゚)]+(゚Д゚) ['c']+"+
        "((゚Д゚)+'_') [(゚ー゚)+(゚ー゚)]+ (゚Д゚) ['o']+"+
        "((゚ー゚==3) +'_') [゚Θ゚];(゚Д゚) ['_'] =(o^_^o) [゚o゚] [゚o゚];"+
        "(゚ε゚)=((゚ー゚==3) +'_') [゚Θ゚]+ (゚Д゚) .゚Д゚ノ+"+
        "((゚Д゚)+'_') [(゚ー゚) + (゚ー゚)]+((゚ー゚==3) +'_') [o^_^o -゚Θ゚]+"+
        "((゚ー゚==3) +'_') [゚Θ゚]+ (゚ω゚ノ +'_') [゚Θ゚]; "+
        "(゚ー゚)+=(゚Θ゚); (゚Д゚)[゚ε゚]='\\\\'; "+
        "(゚Д゚).゚Θ゚ノ=(゚Д゚+ ゚ー゚)[o^_^o -(゚Θ゚)];"+ 
		"(o゚ー゚o)=(゚ω゚ノ +'_')[c^_^o];"+//TODO
        "(゚Д゚) [゚o゚]='\\\"';"+ 
        "(゚Д゚) ['_'] ( (゚Д゚) ['_'] (゚ε゚+";
    r += "(゚Д゚)[゚o゚]+ ";
    for( var i = 0; i < text.length; i++ ){
        n = text.charCodeAt( i );
        t = "(゚Д゚)[゚ε゚]+";
		if( n <= 127 ){
			t += n.toString( 8 ).replace( /[0-7]/g, function(c){ return b[ c ] + "+ "; } );
		}else{
			var m = /[0-9a-f]{4}$/.exec( "000" + n.toString(16 ) )[0];
			t += "(o゚ー゚o)+ " + m.replace( /[0-9a-f]/gi, function(c){ return b[ parseInt( c,16 ) ] + "+ "; } );
		}
        r += t;

    }
    r += "(゚Д゚)[゚o゚]) (゚Θ゚)) ('_');";
    return r;
}

 

代码是JS的,在后端PHP上运行是一件比较麻烦的事,考虑V8配置在win/linux环境兼容比较麻烦。本想网上找找有类似的直接拿来用之,可悲没有找到,只好手动将其代码译为PHP语法,并实现解码过程。

首先实现两个PHP没有的JS常用函数,兼容中文。

function charCodeAt($str, $index)
{
    $char = mb_substr($str, $index, 1, 'UTF-8');
    if (mb_check_encoding($char, 'UTF-8'))
    {
        $ret = mb_convert_encoding($char, 'UTF-32BE', 'UTF-8');
        return hexdec(bin2hex($ret));
    }
    else
    {
        return null;
    }
}

function uchr ($codes) {
    if (is_scalar($codes)) $codes= func_get_args();
    $str= '';
    foreach ($codes as $code){
    	$buf = html_entity_decode('&#'.$code.';',ENT_NOQUOTES,'UTF-8');
		$buf == '&#'.$code.';' && ($buf = mb_convert_encoding('&#' . intval($code) . ';', 'UTF-8', 'HTML-ENTITIES'));
    	$str.= $buf;
    }
    return $str;
}

 

接着是编码和解码的函数过程。

//简单实现没有封装,这个$b只能放到最外面了,比较丑w(゚Д゚)w
$b = [
	"(c^_^o)",
	"(゚Θ゚)",
	"((o^_^o) - (゚Θ゚))",
	"(o^_^o)",
	"(゚ー゚)",
	"((゚ー゚) + (゚Θ゚))",
	"((o^_^o) +(o^_^o))",
	"((゚ー゚) + (o^_^o))",
	"((゚ー゚) + (゚ー゚))",
	"((゚ー゚) + (゚ー゚) + (゚Θ゚))",
	"(゚Д゚) .゚ω゚ノ",
	"(゚Д゚) .゚Θ゚ノ",
	"(゚Д゚) ['c']",
	"(゚Д゚) .゚ー゚ノ",
	"(゚Д゚) .゚Д゚ノ",
	"(゚Д゚) [゚Θ゚]"
    ];


//编码的过程
function aaencode($text){
	global $b;
	
	$r = "゚ω゚ノ= /`m´)ノ ~┻━┻   //*´∇`*/ ['_']; o=(゚ー゚)  =_=3; c=(゚Θ゚) =(゚ー゚)-(゚ー゚); ";
	if(preg_match('/ひだまりスケッチ×(365|356)\s*来週も見てくださいね[!!]/', $text)){
		$r .= "X=_=3; ";
		$r .= "\r\n\r\n    X / _ / X < \"来週も見てくださいね!\";\r\n\r\n";
	}
	
	$r .= "(゚Д゚) =(゚Θ゚)= (o^_^o)/ (o^_^o);".
        "(゚Д゚)={゚Θ゚: '_' ,゚ω゚ノ : ((゚ω゚ノ==3) +'_') [゚Θ゚] ".
        ",゚ー゚ノ :(゚ω゚ノ+ '_')[o^_^o -(゚Θ゚)] ".
        ",゚Д゚ノ:((゚ー゚==3) +'_')[゚ー゚] }; (゚Д゚) [゚Θ゚] =((゚ω゚ノ==3) +'_') [c^_^o];".
        "(゚Д゚) ['c'] = ((゚Д゚)+'_') [ (゚ー゚)+(゚ー゚)-(゚Θ゚) ];".
        "(゚Д゚) ['o'] = ((゚Д゚)+'_') [゚Θ゚];".
        "(゚o゚)=(゚Д゚) ['c']+(゚Д゚) ['o']+(゚ω゚ノ +'_')[゚Θ゚]+ ((゚ω゚ノ==3) +'_') [゚ー゚] + ".
        "((゚Д゚) +'_') [(゚ー゚)+(゚ー゚)]+ ((゚ー゚==3) +'_') [゚Θ゚]+".
        "((゚ー゚==3) +'_') [(゚ー゚) - (゚Θ゚)]+(゚Д゚) ['c']+".
        "((゚Д゚)+'_') [(゚ー゚)+(゚ー゚)]+ (゚Д゚) ['o']+".
        "((゚ー゚==3) +'_') [゚Θ゚];(゚Д゚) ['_'] =(o^_^o) [゚o゚] [゚o゚];".
        "(゚ε゚)=((゚ー゚==3) +'_') [゚Θ゚]+ (゚Д゚) .゚Д゚ノ+".
        "((゚Д゚)+'_') [(゚ー゚) + (゚ー゚)]+((゚ー゚==3) +'_') [o^_^o -゚Θ゚]+".
        "((゚ー゚==3) +'_') [゚Θ゚]+ (゚ω゚ノ +'_') [゚Θ゚]; ".
        "(゚ー゚)+=(゚Θ゚); (゚Д゚)[゚ε゚]='\\\\'; ".
        "(゚Д゚).゚Θ゚ノ=(゚Д゚+ ゚ー゚)[o^_^o -(゚Θ゚)];".
		"(o゚ー゚o)=(゚ω゚ノ +'_')[c^_^o];".
        "(゚Д゚) [゚o゚]='\\\"';".
        "(゚Д゚) ['_'] ( (゚Д゚) ['_'] (゚ε゚+";
	$r .= "(゚Д゚)[゚o゚]+ ";

	for( $i = 0; $i < mb_strlen($text); $i++ ){
        $n = charCodeAt($text,$i);
        $t = "(゚Д゚)[゚ε゚]+";
		if( $n <= 127 ){
			$t .= preg_replace_callback('/[0-7]/', function($c)use ($b){
				return $b[$c[0]]."+ ";
			}, ((string)decoct($n)));
		}else{
			if(preg_match('/[0-9a-f]{4}$/', '000'.((string)dechex($n)),$result)){
				$m = $result[0];
			}else{
				$m = '';
			}
			$t .= "(o゚ー゚o)+ " . preg_replace_callback('/[0-9a-f]/i',function($c)use ($b){
				return $b[ hexdec($c[0]) ] . "+ ";
			},$m);
		}
        $r .= $t;
    }
	$r .= "(゚Д゚)[゚o゚]) (゚Θ゚)) ('_');";
	return $r;
}

//解码的过程(っ•̀ω•́)っ✎⁾⁾ 
function aadecode($text){
	global $b;
	$text = strtr($text, ["(゚Д゚)[゚o゚]) (゚Θ゚)) ('_');" => '']);

	for($i=0;$i<count($b);$i++){
		$buf = $b[$i];
		
		if($i <=7){
			//8进制逆向
			$str = (($i));
			$text = strtr($text, [$buf."+ "=>$str]);
		}else{
			//16进制逆向
			$text = strtr($text, [$buf."+ " => dechex($i)]);
		}
		
	}
	
	$text = preg_replace_callback('/\(゚Д゚\)\[゚ε゚\]\+(\d+)/', function($c){
		return uchr(octdec($c[1]));
	}, $text);
	$text = preg_replace_callback('/\(゚Д゚\)\[゚ε゚\]\+\(o゚ー゚o\)\+\s+([0-9a-f]{4})/', function($c){
		return uchr(hexdec($c[1]));
	}, $text);
	
	$pre = [
		"゚ω゚ノ= /`m´)ノ ~┻━┻   //*´∇`*/ ['_']; o=(゚ー゚)  =_=3; c=(゚Θ゚) =(゚ー゚)-(゚ー゚); ",
		"X=_=3; ",
		"\r\n\r\n    X / _ / X < \"来週も見てくださいね!\";\r\n\r\n",
		"(゚Д゚) =(゚Θ゚)= (o^_^o)/ (o^_^o);",
        "(゚Д゚)={゚Θ゚: '_' ,゚ω゚ノ : ((゚ω゚ノ==3) +'_') [゚Θ゚] ",
        ",゚ー゚ノ :(゚ω゚ノ+ '_')[o^_^o -(゚Θ゚)] ",
        ",゚Д゚ノ:((゚ー゚==3) +'_')[゚ー゚] }; (゚Д゚) [゚Θ゚] =((゚ω゚ノ==3) +'_') [c^_^o];",
        "(゚Д゚) ['c'] = ((゚Д゚)+'_') [ (゚ー゚)+(゚ー゚)-(゚Θ゚) ];",
        "(゚Д゚) ['o'] = ((゚Д゚)+'_') [゚Θ゚];",
        "(゚o゚)=(゚Д゚) ['c']+(゚Д゚) ['o']+(゚ω゚ノ +'_')[゚Θ゚]+ ((゚ω゚ノ==3) +'_') [゚ー゚] + ",
        "((゚Д゚) +'_') [(゚ー゚)+(゚ー゚)]+ ((゚ー゚==3) +'_') [゚Θ゚]+",
        "((゚ー゚==3) +'_') [(゚ー゚) - (゚Θ゚)]+(゚Д゚) ['c']+",
        "((゚Д゚)+'_') [(゚ー゚)+(゚ー゚)]+ (゚Д゚) ['o']+",
        "((゚ー゚==3) +'_') [゚Θ゚];(゚Д゚) ['_'] =(o^_^o) [゚o゚] [゚o゚];",
        "(゚ε゚)=((゚ー゚==3) +'_') [゚Θ゚]+ (゚Д゚) .゚Д゚ノ+",
        "((゚Д゚)+'_') [(゚ー゚) + (゚ー゚)]+((゚ー゚==3) +'_') [o^_^o -゚Θ゚]+",
        "((゚ー゚==3) +'_') [゚Θ゚]+ (゚ω゚ノ +'_') [゚Θ゚]; ",
        "(゚ー゚)+=(゚Θ゚); (゚Д゚)[゚ε゚]='\\\\'; ",
        "(゚Д゚).゚Θ゚ノ=(゚Д゚+ ゚ー゚)[o^_^o -(゚Θ゚)];",
		"(o゚ー゚o)=(゚ω゚ノ +'_')[c^_^o];",
        "(゚Д゚) [゚o゚]='\\\"';",
        "(゚Д゚) ['_'] ( (゚Д゚) ['_'] (゚ε゚+",
        "(゚Д゚)[゚o゚]+ ",
	];
	$rA = [];
	foreach($pre as $val){
		$rA[$val] = '';
	}
	$text = strtr($text,$rA);
	return $text;
}

别忘了加上之前提到的两个函数,调用如下。

$crytext = aaencode('console.log("唐宋元明清");');
echo $crytext;//゚ω゚ノ= /`m´)ノ ~┻━┻   //*´∇`*/ ['_']; o=(゚ー゚)......

echo aadecode($crytext);//console.log("唐宋元明清");

 

先整理到此,后续补充。

本文来自:开源中国博客

感谢作者:Eller

查看原文:PHP实现JS颜文字编码解码过程-aaencode+aadecode

41 次点击  
加入收藏 微博
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet