PHP:从Encode解码?

时间:2012-10-20 10:52:57

标签: php base64 decode encode

我正在使用可以选择编码链接的Flash播放器。我可以使用下面的代码成功编码它们,但我找不到解码的方法,并且它们不提供该选项。

链接正在与播放器一起工作,当我需要编辑它时,我的问题就开始了。而不是普通链接,我看到编码版本。

<?php
    function uppod_enc_replace($str){
            $uppod_tmp_a=Array('G','d','R','0','M','Y','4','v','6','u','t','i','f','c','s','l','B','5','n','2','V','Z','J','m','L','=');
            $uppod_tmp_b=Array('1','w','Q','o','9','U','a','N','x','D','X','7','z','H','y','3','e','g','T','W','b','8','k','I','p','r');
            $uppod_tmp=$str;
            $uppod_tmp_from=$uppod_tmp_a;
            $uppod_tmp_to=$uppod_tmp_b;

            for($i=0;$i<sizeof($uppod_tmp_from);$i++){

                $uppod_tmp=uppod_enc_replace_ab($uppod_tmp_from[$i],$uppod_tmp_to[$i],$uppod_tmp);
            }
            return $uppod_tmp;
    }
    function uppod_enc_replace_ab($a,$b,$tmp){
            $tmp=preg_replace('/'.$a.'/','___',$tmp);
            $tmp=preg_replace('/'.$b.'/',$a,$tmp);
            $tmp=preg_replace('/___/',$b,$tmp);
            return $tmp;
    }
    function uppod_encode($str,$lkey=''){
            $tmp=uppod_enc_replace(base64_encode($str));
            if($lkey!=''){
                    $tmpn=rand(0,strlen($tmp));
                    $tmp=substr($tmp,0,$tmpn).uppod_enc_replace(base64_encode($lkey)).substr($tmp,$tmpn);
            }
            return $tmp;
    }
?>

到目前为止我尝试了什么:

switching places 
                    $uppod_tmp_from=$uppod_tmp_a;
                    $uppod_tmp_to=$uppod_tmp_b;
changing base64_encode to base64_decode 

测试:

stackoverflow.com
encodes to:
HlQhUWXNwIbs8I6NwsgjVWor

changing base64_encode to base64_decode and switching places with $uppod_tmp_from=$uppod_tmp_a; $uppod_tmp_to=$uppod_tmp_b;
decodes to:
n!RB▒▒▒▒▒▒#Yj+[

2 个答案:

答案 0 :(得分:2)

略微修改uppod_enc_replace函数:

function uppod_enc_replace($str, $direction = 'encode'){
    $uppod_tmp_a=Array('G','d','R','0','M','Y','4','v','6','u','t','i','f','c','s','l','B','5','n','2','V','Z','J','m','L','=');
    $uppod_tmp_b=Array('1','w','Q','o','9','U','a','N','x','D','X','7','z','H','y','3','e','g','T','W','b','8','k','I','p','r');
    $uppod_tmp=$str;
    if($direction == 'encode'){
        $uppod_tmp_from = $uppod_tmp_a;
        $uppod_tmp_to = $uppod_tmp_b;
    }else{
        $uppod_tmp_to = $uppod_tmp_a;
        $uppod_tmp_from = $uppod_tmp_b;
    }

    for($i=0;$i<sizeof($uppod_tmp_from);$i++){
        $uppod_tmp=uppod_enc_replace_ab($uppod_tmp_from[$i],$uppod_tmp_to[$i],$uppod_tmp);
    }
    return $uppod_tmp;
}

解码功能如下所示:

function uppod_decode($str, $lkey = ''){
    if($lkey!=''){
        $eKey = uppod_enc_replace(base64_decode($lkey));
        $str = str_replace($eKey, '', $str);
    }
    return base64_decode(uppod_enc_replace($str, 'decode'));
}

测试用例:

$url = 'http://www.google.com/';

$encoded = uppod_encode($url);
$decoded = uppod_decode($encoded);

var_dump($decoded);

$key = base64_encode('p45sw0rd');

$encoded2 = uppod_encode($url, $key);
$decoded2 = uppod_decode($encoded2, $key);

var_dump($decoded2);

// outputs:
// string(22) "http://www.google.com/"
// string(22) "http://www.google.com/"

答案 1 :(得分:0)

编码脚本正在执行:

  1. BASE64_ENCODE()
  2. 切换一些字母
  3. 所以要解码它,你需要:

    1. 切换回字母
    2. 之后的base64_decode()