英文:
Different output of base64_encode function with same input?
问题
使用base64_encode
函数时,我发现相同输入会得到不同输出。以下是我的代码:
$authentication_key="NKu84HvQaPRr";
$iv="abcdef987654";
$input_str="";
$encrypt=openssl_encrypt($input_str, 'AES-128-CBC', $authentication_key, 1, $iv);
$request_str = base64_encode($encrypt);
$request_str1 = base64_encode("�P��@��-�k� ��");
openssl_encrypt
的输出是:�P��@��-�k� ��
$request_str
的输出是:CrxQwOxA4QzzLYdroiD8mw==
$request_str1
的输出是:77+9UO+/ve+/vUDvv70M77+9Le+/vWvvv70g77+977+9
我需要帮助理解base64_encode
在相同输入情况下输出差异的原因。
谢谢。
英文:
While using base64_encode
function I am getting different outputs with the same input. Here is my code :
$authentication_key="NKu84HvQaPRr";
$iv="abcdef987654";
$input_str="";
$encrypt=openssl_encrypt($input_str, 'AES-128-CBC', $authentication_key, 1, $iv);
$request_str = base64_encode($encrypt);
$request_str1 = base64_encode("�P��@��-�k� ��");
openssl_encrypt
yields output: �P��@��-�k� ��
$request_str
outputs: CrxQwOxA4QzzLYdroiD8mw==
$request_str1
outputs 77+9UO+/ve+/vUDvv70M77+9Le+/vWvvv70g77+977+9
I need help in understanding this difference in output of base64_encode
with the same input.
Thanks.
答案1
得分: 2
不同的输出是由不同的输入引起的。
执行反向操作并比较:
$a = base64_decode('CrxQwOxA4QzzLYdroiD8mw==');
$b = base64_decode('77+9UO+/ve+/vUDvv70M77+9Le+/vWvvv70g77+977+9');
加密字符串是二进制数据,无法可靠地可视化原始二进制数据。但你可以转储为十六进制:
var_dump($a, $b, bin2hex($a), bin2hex($b));
结果:
string(16) "�P��@��-�k� ��"
string(33) "�P��@��-�k� ��"
string(32) "0abc50c0ec40e10cf32d876ba220fc9b"
string(66) "efbfbd50efbfbdefbfbd40efbfbd0cefbfbd2defbfbd6befbfbd20efbfbdefbfbd"
英文:
Different output is caused by different input.
Do the reverse operation and compare:
$a = base64_decode('CrxQwOxA4QzzLYdroiD8mw==');
$b = base64_decode('77+9UO+/ve+/vUDvv70M77+9Le+/vWvvv70g77+977+9');
Encrypted strings are binary data and you cannot reliably visualise raw binary data. But you can dump to hexadecimal:
var_dump($a, $b, bin2hex($a), bin2hex($b));
<!---->
string(16) "
�P��@��-�k� ��"
string(33) "�P��@��-�k� ��"
string(32) "0abc50c0ec40e10cf32d876ba220fc9b"
string(66) "efbfbd50efbfbdefbfbd40efbfbd0cefbfbd2defbfbd6befbfbd20efbfbdefbfbd"
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论