为什么我的加密/解密对有时只返回原始明文?

发布于 2024-12-21 04:29:29 字数 995 浏览 6 评论 0原文

我正在尝试加密格式 = LETTERS1234 的用户 ID 它的左侧可能包含一堆字母,并且最终会包含至少 1 个数字

我使用此代码创建了一个简单的测试网站:

<?php
    $key = 'rsZOkGnJiQKf6zkZ';
    $token = $_REQUEST['c'];
    if(!$token){
        $string = 'bgoogl1';
        $enc = urlencode(mcrypt_encrypt(MCRYPT_RIJNDAEL_256, md5($key),
                                        strtolower($string), MCRYPT_MODE_CBC,
                                        md5(md5($key))));
        header('Location: http://website.com/test.php?c='.$enc);
    } else {
        echo rtrim(mcrypt_decrypt(MCRYPT_RIJNDAEL_256, md5($key),
                                  urldecode($token), MCRYPT_MODE_CBC,
                                  md5(md5($key))),"\0");
    }
?>

结果大多数情况下都会为我提供任何 $string$string,但对于这种情况和其他一些 $string 情况,它将失败并输出如下内容:

Ia$ÿ/ckY@Š«° =Û™¼îa¸ï³éð1œ_¹0Àã ¼/÷d‡¬ÐöT¨i“\M~¯D”¯“ÚÍ

我该如何解决这个问题?

PS 我也尝试过 rawurlencode 和 base64_encode 并解码。

I am trying to encrypt a user id of format = LETTERS1234
it could contain a bunch of letters in the left and will end up with at least 1 number

I made a simple test website using this code:

<?php
    $key = 'rsZOkGnJiQKf6zkZ';
    $token = $_REQUEST['c'];
    if(!$token){
        $string = 'bgoogl1';
        $enc = urlencode(mcrypt_encrypt(MCRYPT_RIJNDAEL_256, md5($key),
                                        strtolower($string), MCRYPT_MODE_CBC,
                                        md5(md5($key))));
        header('Location: http://website.com/test.php?c='.$enc);
    } else {
        echo rtrim(mcrypt_decrypt(MCRYPT_RIJNDAEL_256, md5($key),
                                  urldecode($token), MCRYPT_MODE_CBC,
                                  md5(md5($key))),"\0");
    }
?>

The result will give me most of the time the right output for any $string, but for this case and some other $string cases, it will fail and output something like this:

Ia$ÿ/ckY@Š«° =Û™¼îa¸ï³éð1œ_¹0Àã ¼/÷d‡¬ÐöT¨i“\M~¯D”¯“ÚÍ

How could I fix this?

P.S. I've tried with rawurlencode and base64_encode and decode as well.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

黯然 2024-12-28 04:29:29

一般来说,如果你想在HTTP请求中传递二进制数据,你将将其转换为十六进制

$data = mcrypt_encrypt(MCRYPT_RIJNDAEL_256,md5($key),strtolower($string),MCRYPT_MODE_CBC,md5(md5($key)));
$hex = bin2hex($data);

然后,您可以将其打包回二进制文件:

$bin = pack("H*", $hex);

注意:如果您对特定情况有问题,这将有助于知道是哪一个。

Generally speaking, if you want to pass binary data in a HTTP request, you will convert it to hexadecimal instead:

$data = mcrypt_encrypt(MCRYPT_RIJNDAEL_256,md5($key),strtolower($string),MCRYPT_MODE_CBC,md5(md5($key)));
$hex = bin2hex($data);

Then, you can pack it back to binary:

$bin = pack("H*", $hex);

Note: If you have issues with a specific case, it would help to know which one.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文