为什么Powershell会截断Base64字符串?

时间:2015-09-23 09:35:25

标签: .net powershell base64

以下代码未正确地将长字符串编码为Base64。我该怎么做才能解决这个问题?

  

输入:快速的棕色狐狸跳过懒狗

     

预期产量:   VGhlIHF1aWNrIGJyb3duIGZveCBqdW1wcyBvdmVyIHRoZSBsYXp5IGRvZw ==

     

实际输出:VGhl

Param(
  [string]$stringToEncode
)

$encoded = [System.Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes($stringToEncode))

Write-Host $encoded

1 个答案:

答案 0 :(得分:1)

“VGhl”是单词“The”的base64编码形式。你最有可能像这样调用脚本:

C:\path\to\your.ps1 The quick brown fox jumps over the lazy dog

如果没有引号,每个单词都会被解析为单独的参数,因此只有第一个单词最终会出现在参数$stringToEncode中。为了避免这种情况在句子周围加上引号,所以它作为单个参数传递:

C:\path\to\your.ps1 "The quick brown fox jumps over the lazy dog"