Base64 未正确编码/解码

问题描述 投票:0回答:1

我有一个名为 Pastebin 的网站,这是原始 Pastebin 的简约版本,但我在粘贴代码时遇到问题。我的服务器不接受它。

我尝试用base64对其进行编码,这有效,并将数据存储为base 64,当我通过工具对其进行解码时,它完美解码,但是当我在生成的pastebin链接中查看它时,它显示为

�������Y� ��T��T��ԑTUQT
,我想不通。我有一个可以完美编码和解码的主要工作版本,但它不仅显示代码,还显示其中的 html。因此,如果我的代码中有
<h1>Test</h1>
,它会将其显示为标题而不是纯代码。我试图再次找到它,但它在我的修改中丢失了。下面是我当前(不工作)的代码,该代码存在 Base64 编码/解码/显示错误,我不知道是哪个。

JS

    window.onload = function() {
        document.querySelector('#pasteForm').addEventListener('submit', function(e) {
            e.preventDefault();
            var textArea = document.querySelector('#textToPaste');
            var formData = new FormData(e.target);

            var text = formData.get('text').replace(/\n/g, '<br>').replace(/^ /gm, '&nbsp;').replace(/^\t/gm, '&emsp;');
            var encodedText = btoa(text); // Encode the text in base64
            formData.set('text', '<pre>' + encodedText + '</pre>'); // Use the encoded text

            var xhr = new XMLHttpRequest();
            xhr.open('POST', 'create.php', true);

            xhr.onload = function() {
                if (xhr.status === 200) {
                    var password = formData.get('password');
                    if (password) {
                        document.querySelector('#linkBox').value = xhr.responseURL + '?password=' + encodeURIComponent(password);
                    } else {
                        document.querySelector('#linkBox').value = xhr.responseURL;
                    }
                    document.querySelector('#overlay').style.display = 'block';
                    document.querySelector('#popup').style.display = 'block';
                } else {
                    console.error('An error occurred');
                }
            };

            xhr.send(formData);
        });

        document.querySelector('#closeButton').addEventListener('click', function() {
            document.querySelector('#overlay').style.display = 'none';
            document.querySelector('#popup').style.display = 'none';
        });

        document.querySelector('#copyButton').addEventListener('click', function() {
            var linkBox = document.querySelector('#linkBox');
            linkBox.select();
            document.execCommand('copy');

            var copyButtonIcon = document.querySelector('#copyButton img');
            var originalIconSrc = copyButtonIcon.src;
            copyButtonIcon.src = 'https://cdn.cirrus.center/main/icons/check.png';

            setTimeout(function() {
                copyButtonIcon.src = originalIconSrc;
            }, 2000);
        });
    };

PHP

<?php
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    if (!file_exists('paste')) {
        mkdir('paste');

        $redirectIndex = '<?php' . PHP_EOL;
        $redirectIndex .= 'header("Location: ..");' . PHP_EOL;
        $redirectIndex .= 'exit;' . PHP_EOL;
        file_put_contents('paste/index.php', $redirectIndex);
    }

    $dir = uniqid();
    mkdir("paste/$dir");

    $decodedText = '';
    if (isset($_POST['text']) && $_POST['text'] !== '') {
        $decodedText = base64_decode($_POST['text']); // Decode the base64 text
        $decodedText = mb_convert_encoding($decodedText, 'UTF-8', 'auto'); // Convert the encoding to UTF-8
    }

    $index = '<?php' . PHP_EOL;

    if (isset($_POST['password']) && $_POST['password'] !== '') {
        $index .= 'if (!isset($_GET["password"]) || $_GET["password"] !== "' . $_POST['password'] . '") {' . PHP_EOL;
        $index .= '    echo "<h1>Incorrect password</h1>";' . PHP_EOL;
        $index .= '    exit;' . PHP_EOL;
        $index .= '}' . PHP_EOL;
    }

    $index .= 'echo <<<EOT' . PHP_EOL; // Use the decoded text
    $index .= $decodedText . PHP_EOL;
    $index .= 'EOT;' . PHP_EOL;

    if (isset($_POST['burn'])) {
        $index .= 'file_put_contents(__FILE__, "<h1>This paste has been deleted</h1>");' . PHP_EOL;
        $index .= 'exit;' . PHP_EOL;
    }

    $index .= '?>' . PHP_EOL;

    $index .= '<!DOCTYPE html>' . PHP_EOL;
    $index .= '<html>' . PHP_EOL;
    $index .= '<head>' . PHP_EOL;
    $index .= '    <title>' . ($_POST['title'] ?? 'Pastebin') . '</title>' . PHP_EOL;
    $index .= '</head>' . PHP_EOL;
    $index .= '<body>' . PHP_EOL;
    $index .= '' . PHP_EOL;
    $index .= '</body>' . PHP_EOL;
    $index .= '</html>';

    file_put_contents("paste/$dir/index.php", $index);

    header("Location: /paste/$dir");
    echo "/paste/$dir";
}
javascript php http encoding base64
1个回答
0
投票

这是我所看到的,尝试一下并告诉我。我可以根据需要修改这个答案。

在 JavaScript 中,您似乎传递了一个表单字段,如下所示:

formData.set('text', '<pre>' + encodedText + '</pre>'); // Use the encoded text

然后它使用 PHP 代码向服务器发送表单,并且表单字段“文本”的解码如下:

$decodedText = base64_decode($_POST['text']); // Decode the base64 text
decodedText = mb_convert_encoding($decodedText, 'UTF-8', 'auto'); // Convert the encoding to UTF-8

这里有一些潜在的问题:

1-我认为你需要在编码时删除“pre”标签,所以:

formData.set('text', encodedText); // Use the encoded text

2-在 javascript 端,您使用 btoa,但在 PHP 端,您使用 base64_decode。这两个命令可能不兼容,因此您可以使用工具来验证 PHP base64_decode 是否正确解码 javascript btoa。

3-不确定是否需要使用mb_convert_encoding。尝试删除它。

让我知道什么对你有用。

© www.soinside.com 2019 - 2024. All rights reserved.