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


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.)


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  


openssl can also encode and decode base64

$ openssl enc -base64 <<< 'Hello, World!'  SGVsbG8sIFdvcmxkIQo=  $ openssl enc -base64 -d <<< SGVsbG8sIFdvcmxkIQo=  Hello, World!  

EDIT: An example where the base64 encoded string ends up on multiple lines:

$ openssl enc -base64 <<< 'And if the data is a bit longer, the base64 encoded data will span multiple lines.'  QW5kIGlmIHRoZSBkYXRhIGlzIGEgYml0IGxvbmdlciwgdGhlIGJhc2U2NCBlbmNv  ZGVkIGRhdGEgd2lsbCBzcGFuIG11bHRpcGxlIGxpbmVzLgo=  $ openssl enc -base64 -d << EOF  > QW5kIGlmIHRoZSBkYXRhIGlzIGEgYml0IGxvbmdlciwgdGhlIGJhc2U2NCBlbmNv  > ZGVkIGRhdGEgd2lsbCBzcGFuIG11bHRpcGxlIGxpbmVzLgo=  > EOF  And if the data is a bit longer, the base64 encoded data will span multiple lines.  


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.


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 |.


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=='  


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 ).


Using perl

perl -MMIME::Base64 -ne 'printf "%s\n",decode_base64($_)' <<< "QWxhZGRpbjpvcGVuIHNlc2FtZQ=="  

Or the same with python

python -m base64 -d <<< "QWxhZGRpbjpvcGVuIHNlc2FtZQ=="  


Just to add another way to do it:

emacs -Q --batch  -eval '(princ (base64-encode-string (read-string ": ")))'  

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Next Post »