How to decode a Base64 string?

后端 未结 5 555
旧时难觅i
旧时难觅i 2020-12-24 04:38

I have a normal string in Powershell that is from a text file containing Base64 text; it is stored in $x. I am trying to decode it as such:

$z =         


        
5条回答
  •  粉色の甜心
    2020-12-24 05:20

    Isn't encoding taking the text TO base64 and decoding taking base64 BACK to text? You seem be mixing them up here. When I decode using this online decoder I get:

    BASE64: blahblah
    UTF8: nVnV
    

    not the other way around. I can't reproduce it completely in PS though. See sample below:

    PS > [System.Text.Encoding]::UTF8.GetString([System.Convert]::FromBase64String("blahblah"))
    nV�nV�
    
    PS > [System.Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes("nVnV"))
    blZuVg==
    

    EDIT I believe you're using the wrong encoder for your text. The encoded base64 string is encoded from UTF8(or ASCII) string.

    PS > [System.Text.Encoding]::UTF8.GetString([System.Convert]::FromBase64String("YmxhaGJsYWg="))
    blahblah
    
    PS > [System.Text.Encoding]::Unicode.GetString([System.Convert]::FromBase64String("YmxhaGJsYWg="))
    汢桡汢桡
    
    PS > [System.Text.Encoding]::ASCII.GetString([System.Convert]::FromBase64String("YmxhaGJsYWg="))
    blahblah
    

提交回复
热议问题