简体   繁体   English

base64_encode / decode无法正常工作

[英]base64_encode/decode is not working correctly

I am trying to use base64_encode to display the ids differently. 我正在尝试使用base64_encode以不同的方式显示ID。 I am also trying to add a large number to the id so the string will look longer. 我还尝试向ID添加一个大数字,以使字符串看起来更长。

The problem is I encrypt a number to make it a string. 问题是我将数字加密为字符串。 Then when I decrypt the same string I expect the same value but in my case it is returning different values. 然后,当我解密相同的字符串时,我期望使用相同的值,但就我而言,它返回的是不同的值。

Why I am not seeing the same values? 为什么我看不到相同的值?

This is my code: 这是我的代码:

define('IDS_SALT', 852045641596357);

function simple_encode($id){
    $data = '';
    $id += IDS_SALT;
    $data = base64_encode($id);
    $data = str_replace(array('+','/','='),array('-','_','.'),$data);
    return $data;
}


function simple_decode($code){
    $data = '';
    $code = str_replace(array('-','_','.'),array('+','/','='),$code);
    $id = base64_decode($code);
    $id -= IDS_SALT;
    return $id;
}

//this is returning "OC41MjA0NTY0MTU5NjM2RSsxNA.."
echo 'Encrypted: ';
echo  simple_encode(7);
echo '<br />';

//this is returning 3 a NOT 7. it should return 7
echo 'Encrypted: ';
echo  simple_decode(  simple_encode(7)  );
echo '<br />';

I just ran the same code (exactly as above) on my test server here - I received different results: 我只是在这里的测试服务器上运行了相同的代码(与上面完全一样)-我收到了不同的结果:

Encrypted: ODUyMDQ1NjQxNTk2MzY0 加密的:ODUyMDQ1NjQxNTk2MzY0

Encrypted: 7 加密:7

Which looks to be correct? 哪个看起来正确?

Is there any other code in your test script that could be interfering? 测试脚本中是否还有其他可能会干扰的代码?

Steve 史蒂夫

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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