Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to perform base 64 encoding from CLI without the base64 command?

I am on a Busybox system where I can not install any packages and that does not include the base64 command. However, I need to run a script that uses base64 de/encoding. Is there another way to do that? Maybe in a function in the script?

like image 806
tzippy Avatar asked Oct 28 '25 14:10

tzippy


2 Answers

well, how easy it is to en/decode base64 really depends on what is installed on your system.

e.g. openssl can encode and decode base64:

$ openssl enc -base64 <<< 'Hello, World!'
SGVsbG8sIFdvcmxkIQo=
$ openssl enc -base64 -d <<< SGVsbG8sIFdvcmxkIQo=
Hello, World!
like image 111
umläute Avatar answered Oct 30 '25 06:10

umläute


Since 1980 this has been available https://en.wikipedia.org/wiki/Uuencoding

busybox has a version https://www.busybox.net/downloads/BusyBox.html#uuencode with base64 support:

uuencode [-m] [infile] stored_filename

Uuencode a file to stdout

Options:

        -m      Use base64 encoding per RFC1521

Usage examples:

$ echo 'foo' | uuencode -m -
begin-base64 644 -
Zm9vCg==
====

And to confirm it's correct using other tools:

$ echo 'foo' | b64encode -
begin-base64 644 -
Zm9vCg==
====
$ echo 'foo' | openssl base64      
Zm9vCg==
$ echo 'foo' | python3 -c 'import base64; import fileinput; print(base64.b64encode("".join(fileinput.input(encoding="utf-8")).encode()))'
b'Zm9vCg=='

Decoding:

$ printf 'Zm9vCg==' | openssl base64 -d -A
foo
$ printf 'Zm9vCg==' | python3 -m base64 -d
foo

Decoding can be done on base busybox, e.g., using this awk script https://superuser.com/a/1871180

like image 25
Samuel Marks Avatar answered Oct 30 '25 06:10

Samuel Marks



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!