.net Powershell 2.0断开的Base64文件转换
我在电脑上找到了这个密码 它适用于windows 8.1 Powershell 4,但在Powershell 2上,当我将base64字符串解码回文件时,它似乎在文件末尾添加了一个字节。Powershell 2也会抛出关于base64字符串长度无效的错误,但它仍然会创建文件 我已经花了几个小时试图解决这个问题,但我对.net的了解还不足以解决这个问题 我用这些函数来携带的数据是一个较大的脚本。它在Powershell 4上运行时没有任何错误 我需要它在3000多台windows 7机器上运行,坚持使用Powershell 4不是一个解决方案.net Powershell 2.0断开的Base64文件转换,.net,powershell,base64,powershell-2.0,.net,Powershell,Base64,Powershell 2.0,我在电脑上找到了这个密码 它适用于windows 8.1 Powershell 4,但在Powershell 2上,当我将base64字符串解码回文件时,它似乎在文件末尾添加了一个字节。Powershell 2也会抛出关于base64字符串长度无效的错误,但它仍然会创建文件 我已经花了几个小时试图解决这个问题,但我对.net的了解还不足以解决这个问题 我用这些函数来携带的数据是一个较大的脚本。它在Powershell 4上运行时没有任何错误 我需要它在3000多台windows 7机器上运行,坚
function ConvertTo-Base64
{
param
(
[string] $SourceFilePath,
[string] $TargetFilePath
)
$SourceFilePath = Resolve-PathSafe $SourceFilePath
$TargetFilePath = Resolve-PathSafe $TargetFilePath
$bufferSize = 9000 # should be a multiplier of 3
$buffer = New-Object byte[] $bufferSize
$reader = [System.IO.File]::OpenRead($SourceFilePath)
$writer = [System.IO.File]::CreateText($TargetFilePath)
$bytesRead = 0
do
{
$bytesRead = $reader.Read($buffer, 0, $bufferSize);
$writer.Write([Convert]::ToBase64String($buffer, 0, $bytesRead));
} while ($bytesRead -eq $bufferSize);
$reader.Dispose()
$writer.Dispose()
}
function ConvertFrom-Base64
{
param
(
[string] $SourceFilePath,
[string] $TargetFilePath
)
$SourceFilePath = Resolve-PathSafe $SourceFilePath
$TargetFilePath = Resolve-PathSafe $TargetFilePath
$bufferSize = 9000 # should be a multiplier of 4
$buffer = New-Object char[] $bufferSize
$reader = [System.IO.File]::OpenText($SourceFilePath)
$writer = [System.IO.File]::OpenWrite($TargetFilePath)
$bytesRead = 0
do
{
$bytesRead = $reader.Read($buffer, 0, $bufferSize);
$bytes = [Convert]::FromBase64CharArray($buffer, 0, $bytesRead);
$writer.Write($bytes, 0, $bytes.Length);
} while ($bytesRead -eq $bufferSize);
$reader.Dispose()
$writer.Dispose()
}
function Resolve-PathSafe
{
param
(
[string] $Path
)
$ExecutionContext.SessionState.Path.GetUnresolvedProviderPathFromPSPath($Path)
}
ConvertTo-Base64 .\file .\Converted-File.txt
它会在输出文件的末尾添加一个字节吗?或者到原始流(导致不同的Base64字符串)?从Base64到文件的转换似乎将最后一个字节加倍。