使用Powershell解码base64 [英] Decoding base64 with powershell
问题描述
我正在尝试从base64解码电子邮件文件中的附件并将其保存到磁盘.
I'm attempting to decode an attachment in an email file from base64 and save it to disk.
出于测试目的,这是我的代码.其中input.txt仅包含base64编码的数据,它是一个HTML文件.
For testing purposes, this is my code. Where input.txt contains just the base64 encoded data, which is an HTML file.
$file = "C:\input.txt"
$data = Get-Content $file
[System.Text.Encoding]::ASCII.GetString([System.Convert]::FromBase64String($data)) > out.html
解码正常,它会生成一个包含所有行的新文件,并且与原始附件明显相同.问题在于输出文件的大小是该文件大小的两倍(在这种情况下,实际上是(filesize * 2)+6个字节).
The decoding works fine, and it generates a new file that contains all of the lines, and is visibly identical to the original attachment. The problem is that the output file is twice the size (actually (filesize*2)+6 bytes, in this case).
我解码不正确吗?我还尝试了UTF8而不是ASCII ...相同的结果.
Am I decoding this improperly? I've also tried UTF8 instead of ASCII... same result.
推荐答案
好了,我可以使用它了.谁知道默认情况下Out-File被重新编码为Unicode?通过执行以下操作来解决:
Well I got it working. Who knew Out-File re-encoded to Unicode by default? Solved by doing the following:
$file = "C:\input.txt"
$data = Get-Content $file
[System.Text.Encoding]::ASCII.GetString([System.Convert]::FromBase64String($data)) | Out-File -Encoding "ASCII" out.html
这篇关于使用Powershell解码base64的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!