如何在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.
我不使用linux,但是如果您的utf8-sample可以正常工作,则可以尝试:
iconv -f ASCII -t UTF-16 mycmd.txt |base64