微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何在Linux上对base64进行编码,使其与Windows“ Unicode.GetBytes.ToBase64String”匹配?

这个问题已经在这里有了答案:            >            Different output between Powershell ToBase64String & Linux base64                                    1个
这个问题可能已经回答了,但是我找不到

如何在bash中“ base64编码”字符串,使其与“ Windows期望”匹配,即“ Unicode.GetBytes.ToBase64String”

语境

Powershell帮助文本包含此示例

$command='dir'
$bytes = [System.Text.Encoding]::Unicode.GetBytes($command)
$encodedCommand = [Convert]::ToBase64String($bytes)
echo $encodedCommand
powershell.exe -encodedCommand $encodedCommand

Windows输出

编码的输出是这样的:

ZABpAHIA

Linux上的结果截然不同

如果我在linux上做同样的事情,我会得到:

echo "dir" | base64
ZGlyCg==

编码?

我尝试使用“ iconv”转换编码,但是没有运气,即结果相同.

echo dir | iconv -f ASCII -t UTF-8 | base64

差异性

因此,您可以看到两个区别:

>字母数字字符不同
>尾随“ ===”出现在Linux上

解决方法:

Powershell中的Unicode是UTF16 LE,而不是UTF8.

Unicode: Encodes in UTF-16 format using the little-endian byte order.

来源:Set-Content for FileSystem

我不使用linux,但是如果您的utf8-sample可以正常工作,则可以尝试:

iconv -f ASCII -t UTF-16 mycmd.txt |base64

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐