简体   繁体   中英

encode by php and decode by js

because of gfw(great firewall) in our country , I have to encode content within http transfer (https is better , but its a second choice).

my way is use base64 encode by php and decode by js , then show in an iframe. but there's some problem in FF.

is there any better way to show base64 encoded string in browser , or another way to encode/decode ?

Try Something this:

    function get_rnd_iv($iv_len)
    {
        $iv = '';
        while ($iv_len-- > 0) {
            $iv .= chr(mt_rand() & 0xff);
        }
        return $iv;
    }

    function md5_encrypt($string_value, $salt_key, $iv_len = 16)
    {
        $string_value .= "\x13";
        $n = strlen($string_value);
        if ($n % 16) $string_value .= str_repeat("\0", 16 - ($n % 16));
        $i = 0;
        $enc_text = get_rnd_iv($iv_len);
        $iv = substr($salt_key ^ $enc_text, 0, 512);
        while ($i < $n) {
            $block = substr($string_value, $i, 8) ^ pack('H*', md5($iv));
            $enc_text .= $block;
            $iv = substr($block . $iv, 0, 512) ^ $salt_key;
            $i += 16;
        }
        return urlencode(base64_encode($enc_text));
    }

    function md5_decrypt($enc_text, $salt_key, $iv_len = 16)
    {
        $enc_text = urldecode(base64_decode($enc_text));
        $n = strlen($enc_text);
        $i = $iv_len;
        $string_value = '';
        $iv = substr($salt_key ^ substr($enc_text, 0, $iv_len), 0, 512);
        while ($i < $n) {
            $block = substr($enc_text, $i, 8);
            $string_value .= $block ^ pack('H*', md5($iv));
            $iv = substr($block . $iv, 0, 512) ^ $salt_key;
            $i += 16;
        }
        return preg_replace('/\\x13\\x00*$/', '', $string_value);
    }

You could just set the innerHTML property of the body of the page...

As long as the encoded string that the JS is producing is valid HTML for the whole body, it should work fine, and then you don't have to do anything messy with iframes or whatever.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM