Ubuntu: How can I decode a base64 string from the command line?
Channel: Roel Van de Paar
Date: 18/06/2019

Ubuntu: How can I decode a base64 string from the command line?


Question: I would like to write a bash script to decode a base64 string. For example I
type decode QWxhZGRpbjpvcGVuIHNlc2FtZQ== and it prints Aladdin:open sesame and
returns to the prompt.
So far I have tried a simple bash file containing python -m base64 -d $1 but
this command expects a filename not a string. Is there another non-interactive
command (not necessarily in a Python module) that I can run from the command
line to achieve this, without having to install any extra packages? (Or if I
do, something super-minimal.)

Solutions Sample (Please watch the whole video to see all solutions, in order of how many people found them helpful):

== This solution helped 12 people ==
I did comment base64 command line in http://wiki.opensslfoundation.com/
index.php?title=Command_Line_Utilities. So I issue a Warning when using openssl
base64 decoding :
 openssl base64 -e ＜＜＜ 'Welcome to openssl wiki'

V2VsY29tZSB0byBvcGVuc3NsIHdpa2kK



openssl base64 -d ＜＜＜ 'V2VsY29tZSB0byBvcGVuc3NsIHdpa2kK'

Welcome to openssl wiki
warning base64 line length is limited to 64 characters by default in openssl :
 openssl base64 -e ＜＜＜ 'Welcome to openssl wiki with a very long line
that splits...'

V2VsY29tZSB0byBvcGVuc3NsIHdpa2kgd2l0aCBhIHZlcnkgbG9uZyBsaW5lIHRo
YXQgc3BsaXRzLi4uCg==

openssl base64 -d ＜＜＜
'V2VsY29tZSB0byBvcGVuc3NsIHdpa2kgd2l0aCBhIHZlcnkgbG9uZyBsaW5lIHRoYXQgc3BsaXRzLi4uCg=='
=＞ NOTHING !
to be able to decode a base64 line without line feed that exceed 64 characters
use -A option :
openssl base64 -d -A ＜＜＜
'V2VsY29tZSB0byBvcGVuc3NsIHdpa2kgd2l0aCBhIHZlcnkgbG9uZyBsaW5lIHRoYXQgc3BsaXRzLi4uCg=='

Welcome to openssl wiki with a very long line that splits...
This is anyway better to actualy split base64 result in 64 characters lines
since -A option is BUGGY ( limit with long files ).

== This solution helped 20 people ==
With your original dependencies it is possible to do this with a minor
modification to your original script:
echo $1 | python -m base64 -d
If you don't pass a file name, that python module reads from the standard
input. To pipe the first parameter into it you can use echo $1 |.

== This solution helped 2 people ==
Just to add another way to do it:
emacs -Q --batch -eval '(princ (base64-encode-string (read-string ": ")))'

== This solution helped 32 people ==
Here you go!
Add the following to the bottom of your ~/.bashrc file:
decode () {
echo "$1" | base64 -d ; echo
}
Now, open a new Terminal and run the command.
decode QWxhZGRpbjpvcGVuIHNlc2FtZQ==
This will do exactly what you asked for in your question.

== This solution helped 4 people ==
Using perl
perl -MMIME::Base64 -ne 'printf "%sn",decode_base64($_)' ＜＜＜
"QWxhZGRpbjpvcGVuIHNlc2FtZQ=="
Or the same with python
python -m base64 -d ＜＜＜ "QWxhZGRpbjpvcGVuIHNlc2FtZQ=="

== This solution helped 554 people ==
Just use the base64 program from the coreutils package:
echo QWxhZGRpbjpvcGVuIHNlc2FtZQ== | base64 --decode
Or, to include the newline character
echo `echo QWxhZGRpbjpvcGVuIHNlc2FtZQ== | base64 --decode`
output (includes newline):
Aladdin:open sesame

With thanks & praise to God! With thanks to all the many who have made this project possible! | Content (except music & images) licensed under cc by-sa 3.0 | Music & music license: https://www.bensound.com/royalty-free-music | Images & images license: https://stocksnap.io/license and others | With thanks to user SirCharlo (https://askubuntu.com/users/40421), user Seth (https://askubuntu.com/users/44179), user philippe lhardy (https://askubuntu.com/users/142855), user January (https://askubuntu.com/users/83703), user geirha (https://askubuntu.com/users/9016), user erjoalgo (https://askubuntu.com/users/19547), user d3vid (https://askubuntu.com/users/7146), user caiguanhao (https://askubuntu.com/users/126957), user AmanicA (https://askubuntu.com/users/7166), user A.B. (https://askubuntu.com/users/367165), user A.B. (https://askubuntu.com/users/367165), and the Stack Exchange Network (http://askubuntu.com/questions/178521). Trademarks are property of their respective owners. Disclaimer: All information is provided "AS IS" without warranty of any kind. You are responsible for your own actions. Please contact me if anything should be amiss at Roel D.OT VandePaar A.T gmail.com.