如果包含Unicode字符,则无法在JavaScript中解码PHP中的编码字符串

时间:2019-06-09 15:41:50

标签: javascript php utf-8 encode rot13

我正在尝试使用类似于Rot13的算法在PHP中编码字符串,然后在JavaScript中解码该字符串并进行搜索和替换。它可以与ASCII字符一起正常工作,但不适用于Unicode。

我把附带的代码弄乱了,但是无法正常工作。

<?php

function strRot($str, $n) {
    $len = mb_strlen($str);
    $min = 0;
    $max = 99999999;
    $final = '';

    for ($i = 0; $i < $len; $i++) {
        $current = mb_ord($str[$i]);
        $val = $current+$n;

        if ($val >= $max) {
            $val = $val - $max;
        }

        if ($val <= $min) {
            $val = $val + $min;
        }

        $final .= mb_chr($val);
    }

    return $final;
}

?><!doctype html>
<html lang="en">
<head>
    <!-- Required meta tags -->
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">

    <!-- Bootstrap CSS -->
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.2.1/css/bootstrap.min.css" integrity="sha384-GJzZqFGwb1QTTN6wy59ffF1BuGJpLSa9DkKMp0DgiMDm4iYMj70gZWKYbI706tWS" crossorigin="anonymous">

    <title>Hello, world!</title>
</head>
<body>
    <h1>Hello, world!</h1>
    <h2>Ü and ?. 棕色的狐狸跳了起来.</h2>

    <p>The Hello, world! expression will be replaced.</p>
    <p>Ü and ?. 棕色的狐狸跳了起来. Should be replaced too.</p>

    <!-- Optional JavaScript -->
    <!-- jQuery first, then Popper.js, then Bootstrap JS -->
    <script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.6/umd/popper.min.js" integrity="sha384-wHAiFfRlMFy6i5SRaxvfOCifBUQy1xHdJ/yoi7FRNXMRBu5WHdZYu1hA6ZOblgut" crossorigin="anonymous"></script>
    <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.2.1/js/bootstrap.min.js" integrity="sha384-B0UglyR+jN6CkvvICOB2joaf5I4l3gm9GU6Hc1og6Ls7i6U/mkkaduKaBhlAXv9k" crossorigin="anonymous"></script>

    <script id="scriptId" type="text/javascript">
        var data = [
            ["Hello, world!", "<?php echo base64_encode(strRot('I got replaced.', 1000)); ?>"],
            ["Ü and ?. 棕色的狐狸跳了起来.", "<?php echo base64_encode(strRot('? before Ü and 棕色的.', 1000)); ?>"]
        ];

        function b64DecodeUnicode(str) {
            // Going backwards: from bytestream, to percent-encoding, to original string.
            return decodeURIComponent(atob(str).split('').map(function(c) {
                return '%' + ('00' + c.charCodeAt(0).toString(16)).slice(-2);
            }).join(''));
        }

        function strRot(str, n)
        {
            var min = 0;
            var max = 99999999;
            var final = '';

            for (var i in str) {
                var current = str.charCodeAt(i);
                var val = current+n;

                if (val >= max) {
                    val = val - max;
                }

                if (val <= min) {
                    val = val + min;
                }

                final += String.fromCharCode(val);
            }

            return final;
        }

        function replace() {
            for (index in data) {
                //var regex = new RegExp(data[index][0], "ug");
                jQuery("html *:not(script[id=scriptId])").children().each(function () {
                    jQuery(this).html(jQuery(this).html().replace(
                        data[index][0],
                        strRot(b64DecodeUnicode(data[index][1]), -1000)
                    ));
                });
            }
        }

        replace();
    </script>

</body>
</html>

一旦JS运行,就应该用解码后的数据[index] [1]替换data [index] [0]。

2 个答案:

答案 0 :(得分:0)

(我没有足够的声誉来发表评论,所以诉诸于使用答案...)

不确定是否会有所不同,但是在HTML“ h2”标头中,您的Unicode表达式为...

Ü an ?. 棕色的狐狸跳了起来.

...在data []中,是...

Ü and ?. 棕色的狐狸跳了起来.

假定“ an”和“ and”应该相同?

答案 1 :(得分:0)

我找到了一个解决方案:

var data = [
            ["Hello, world!", "<?php echo base64_encode(strRot(rawurlencode('I got replaced.'), 1000)); ?>"],
            ["Ü and ?. 棕色的狐狸跳了起来.", "<?php echo base64_encode(strRot(rawurlencode('? before Ü and 棕色的.'), 1000)); ?>"]
        ];

// Then, in replace():

decodeURIComponent(strRot(b64DecodeUnicode(data[index][1]), -1000))

之所以有用,是因为它在旋转所有Unicode字符之前会将其转义。 唯一的问题是,由于转义,字符串大小增加了一些开销。