0

Goal:

I am attempting to a find a solution that would allow me to enter a Base64 encoded string that can be multiple lines. The goal is to decode the string and get the output returned in the powershell console. Originally, I called a WinForms dialogue, however, this is not possible with implicit remoting.

This is what I have so far:

function Get-Base64 {
  Write-Host 'Decode:' -ForegroundColor Yellow -NoNewline
  $data = Read-Host
  $decode = [Text.Encoding]::UTF8.GetString([Convert]::FromBase64String("$data"))
  $decode.ToString()
      }

I get the following error message when entering multiple lines:

PS > Get-Base64
Decode)ZXZhbChmdW5jdGlvbihwLGEsYyxrLGUsZCl7ZT1mdW5jdGlvbihjKXtyZXR1cm4oYzxhPycnOmUo
eval(function(p,a,c,k,e,d){e=function(c){return(c<a?'':e(
PS > cGFyc2VJbnQoYy9hKSkpKygoYz1jJWEpPjM1P1N0cmluZy5mcm9tQ2hhckNvZGUoYysyOSk6Yy50
cGFyc2VJbnQoYy9hKSkpKygoYz1jJWEpPjM1P1N0cmluZy5mcm9tQ2hhckNvZGUoYysyOSk6Yy50 : The term 'cGFyc2VJbnQoYy9hKSkpKygoYz1jJWEpPjM1P1N0cmluZy5mcm9tQ2hhckNvZGUoYysyOSk6Yy50' is not recognized as the name of a cmdlet, function, script file,
or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
At line:1 char:1
+ cGFyc2VJbnQoYy9hKSkpKygoYz1jJWEpPjM1P1N0cmluZy5mcm9tQ2hhckNvZGUoYysyO ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (cGFyc2VJbnQoYy9...GUoYysyOSk6Yy50:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

You can see that it decoded the first part then on the next line, read it as a cmdlet.

Question:

How can I avoid formatting collisions to decode multiple lines?

1 Answer 1

3

A base64 encoded string can't be on multiple lines; those would be separate, independent base64 encoded strings. Newlines in the original content would be encoded, not preserved.

If you're intending to read multiple independent base64 encoded strings, then you can't do it with a single call to Read-Host. It only reads one line.

You'll have to loop and keep taking lines until you get some "signal" input to stop (like a blank input):

do {
    Write-Host 'Decode:' -ForegroundColor Yellow -NoNewline
    $data = Read-Host
    [Text.Encoding]::UTF8.GetString([Convert]::FromBase64String($data))
} while ($data)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.