2

If I do the following in powershell:

$b = [System.Text.Encoding]::UTF8.GetBytes("helloworld")

[System.Convert]::ToBase64String($b)

The output is:

aGVsbG93b3JsZA==

If I go into bash and do the same thing:

echo "helloworld" | base64 then the result is:

aGVsbG93b3JsZAo=

So in bash, instead of the first = its an o. Why is that?

Giardino
  • 1,367
  • 3
  • 10
  • 30
  • As an aside: _PowerShell's_ pipeline (`|`) _invariably appends a newline_ when piping to external programs such as `bash64`, so if you run stackprotector's solution _from PowerShell_, you'll again get the newline. That is, _from PowerShell_ `/bin/echo -n "helloworld" | base64` (or `printf "helloworld" | base64` or just `"helloworld" | base64`) yields the same as `/bin/echo "helloworld" | base64`, namely `aGVsbG93b3JsZAo=` - see [this answer](https://stackoverflow.com/a/48372333/45375). – mklement0 Sep 01 '21 at 20:02

1 Answers1

2

In bash,

echo "helloworld" | base64

will add a newline character (\n or 0A in hex) to helloworld. Use

echo -n "helloworld" | base64

instead.

stackprotector
  • 10,498
  • 4
  • 35
  • 64
  • True. Calling GetBytes("helloworld`n") will give the same Base64 encoding in Powershell. Note the backtick escape, which is used in Powershell instead of the more common backslash. – vonPryz Sep 01 '21 at 19:36