对酷狗的flash播放器进行反编译,发现这段krc解密的ActionScript代码:
public function loaderCompleteHandler(param1:ByteArray) : void{ this.newkeyBytes = [64, 71, 97, 119, 94, 50, 116, 71, 81, 54, 49, 45, 206, 210, 110, 105]; var result:* = param1; var krcBytes:* = new ByteArray(); result.position = 4; result.readBytes(krcBytes); var l:* = krcBytes.length; var i:int; while (i < l) { krcBytes[i] = krcBytes[i] ^ this.newkeyBytes[i % this.newkeyBytes.length]; i = (i + 1); } try { krcBytes.uncompress(); } catch (error:SecurityError) { errorfun("uncompressError"); return; } krcBytes.position = 0; this.myLyric.dataStr = krcBytes.readUTFBytes(krcBytes.length); this.successfun(); return; }
根据这段代码可以很快转换成php解析代码:
$enKey = array(64, 71, 97, 119, 94, 50, 116, 71, 81, 54, 49, 45, 206, 210, 110, 105); $krc_content = substr($krc_content,4); $len = strlen($krc_content); $krc_compress = ''; for ($k = 0; $k < $len; $k++){ $krc_compress .= chr(ord($krc_content[$k]) ^ $enKey[$k % 16]); } $krc_text = gzuncompress($krc_compress);
上面的$krc_content为krc歌词压缩加密的文本内容,得到了解析后的krc明文文本歌词$krc_text,然后就可以很容易的转换成lrc歌词了,如下:
function krc2lrc($krc_content,$glue = "n"){ $enKey = array(64, 71, 97, 119, 94, 50, 116, 71, 81, 54, 49, 45, 206, 210, 110, 105); $krc_content = substr($krc_content,4); $len = strlen($krc_content); $krc_compress = ''; for ($k = 0; $k < $len; $k++){ $krc_compress .= chr(ord($krc_content[$k]) ^ $enKey[$k % 16]); } $krc_text = gzuncompress($krc_compress); preg_match_all("/[(ar|ti|offset):(.+)]/",$krc_text,$matches); $lrc_text = implode($glue,$matches[0]).$glue; preg_match_all("/[(d{0,8}),(d{0,8})](.+)/",$krc_text,$matches); unset($krc_text,$krc_compress,$krc_content,$enKey); if(!empty($matches[1])){ foreach($matches[1] as $k=>$v){ list($seconds, $millis) = explode('.', $v / 1000); $lrc_time = date('i:s',1388534400 + $seconds).'.'.substr($millis,0,2); $lrc_cont = preg_replace("//",'',$matches[3][$k]); $lrc_text .= "[{$lrc_time}]{$lrc_cont}{$glue}"; } } return $lrc_text; }
简单示例如下:
$content = file_get_contents('1.krc'); echo krc2lrc($content,'
');