base64_encode函数在相同输入下输出不同结果?

huangapple go评论60阅读模式
英文:

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) &quot;
�P��@��-�k� ��&quot;
string(33) &quot;�P��@��-�k� ��&quot;
string(32) &quot;0abc50c0ec40e10cf32d876ba220fc9b&quot;
string(66) &quot;efbfbd50efbfbdefbfbd40efbfbd0cefbfbd2defbfbd6befbfbd20efbfbdefbfbd&quot;

huangapple
  • 本文由 发表于 2023年3月1日 15:38:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/75600740.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定