【问题标题】:How to get base64 encoding the same for AutoIt and PowerShell?如何为 AutoIt 和 PowerShell 获得相同的 base64 编码?
【发布时间】:2011-10-27 05:52:45
【问题描述】:

我使用Ward's AutoIt Machine Code Algorithm Collection 在 AutoIt 中获取字符串的 base64 编码:

#Include "Base64.au3"

Dim $Encode = _Base64Encode("ps")
MsgBox(0, 'Base64 Encode Data', $Encode)

结果:

cHM=

PowerShell代码获取相同字符串“ps”的base64编码:

$commands = 'ps'
$bytes = [System.Text.Encoding]::Unicode.GetBytes($commands)
$encodedString = [Convert]::ToBase64String($bytes)
$encodedString

我得到的是:

cABzAA==

PowerShell 的结果是我想要的。如何使用 AutoIt 获得相同的结果?我猜这是字符编码问题。

【问题讨论】:

    标签: powershell encoding base64 autoit


    【解决方案1】:

    使用 ASCII 编码而不是 Unicode:

    $bytes = [System.Text.Encoding]::ASCII.GetBytes($commands)
    

    【讨论】:

    • 嗨 MrKWatkins,对我来说,PowerShell 的结果是正确的。我需要 AutoIt 的这个结果。所以我无法更改 PowerShell 代码。
    • 啊,抱歉,误读了。不知道 AutoIt,但是它有没有一种方法可以用于 base 64 编码字节而不是字符串?在这种情况下,您可以自己使用 Unicode 编码手动将字符串转换为字节并将其传入。
    【解决方案2】:

    当我运行这个脚本时:

    #Include "Base64.au3"
    
    $Decode = _Base64Decode("cABzAA==")
    ConsoleWrite($Decode & @CRLF)
    

    我得到结果:0x70007300。基本上,这意味着有一个'70'字符(p),一个'00'字符(nul),一个'73'字符(s),'00'字符。您可以使用如下函数在 AutoIt 中轻松地重新创建此行为:

    #Include "Base64.au3"
    
    Dim $Encode = _Base64WEncode("ps")
    ConsoleWrite($Encode & @CRLF)
    
    Func _Base64WEncode($string)
        Local $result = ""
        Local $arr = StringSplit($string, "")
        For $i = 1 To UBound($arr) - 1
            $result &= $arr[$i] & Chr(0)
        Next
        $result = _Base64Encode($result)
        Return $result
    EndFunc
    

    结果是:cABzAA==

    有点 hack-ish,但我会说它比完整的 Unicode 编码更受欢迎,如果这不是你所需要的。

    【讨论】:

    • TL;DR 版本:PowerShell由于是.NET是UCS16LE(UTF16),所以默认编码为双字节。
    【解决方案3】:
    #Include "Base64.au3"    
    #include <MsgBoxConstants.au3>
    #include <StringConstants.au3>
    
    Dim $Encode = _Base64Encode(StringToBinary("ps", $SB_UTF16LE))
    MsgBox(0, 'Base64 Encode Data', $Encode)
    

    这会给你想要的:

    cABzAA==
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-10-04
      • 1970-01-01
      • 1970-01-01
      • 2019-04-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多