Base64 decode linux command

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

8 Answers 8

Just use the base64 program from the coreutils package:

echo QWxhZGRpbjpvcGVuIHNlc2FtZQ== | base64 --decode 

Or, to include the newline character

echo `echo QWxhZGRpbjpvcGVuIHNlc2FtZQ== | base64 --decode` 

echo QWxhZGRpbjpvcGVuIHNlc2FtZQ== | «C:\Program Files\Git\usr\bin\base64» —decode 2> nul > example.txt On Windows with git’s base64.

@January It is not Just use , because many people know about the base64 program – but as one can’t just insert a string as command line option, it is hard to get the syntax right for users who touch the CLI only once in a while.

openssl can also encode and decode base64

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

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

Thanks to Philippe’s answer, you need to add -A for long base64 strings otherwise openssl will return nothing, see askubuntu.com/a/271676/305568

I would not consider coreutils an «additional» package containing programs like ls , mkdir , cp , mv , and chmod . I doubt you can do anything useful with your machine without it.

@vidstige, that’s true. I don’t know why I was under the impression that base64 was not installed by default; that is totally not the case.

While this is the ubuntu stack exchange, using openssl has the advantage over standard base64 of working in Git Bash on Windows, at least the older 1.8.1 Git Bash version I have installed.

Add the following to the bottom of your ~/.bashrc file:

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 :

warning base64 line length is limited to 64 characters by default in openssl :

to be able to decode a base64 line without line feed that exceed 64 characters use -A option :

Читайте также:  Linux operating system and version

This is anyway better to actualy split base64 result in 64 characters lines since -A option is BUGGY ( limit with long files ).

openssl wiki documentation was moved here wiki.openssl.org/index.php/… then a rewrtie of did lost this -A usage and 64 characters limits.

perl -MMIME::Base64 -ne 'printf "%s\n",decode_base64($_)'  
)" data-controller="se-share-sheet" data-se-share-sheet-title="Share a link to this answer" data-se-share-sheet-subtitle="" data-se-share-sheet-post-type="answer" data-se-share-sheet-social="facebook twitter " data-se-share-sheet-location="2" data-se-share-sheet-license-url="https%3a%2f%2fcreativecommons.org%2flicenses%2fby-sa%2f3.0%2f" data-se-share-sheet-license-name="CC BY-SA 3.0" data-s-popover-placement="bottom-start">Share
)" title="">Improve this answer
answered Jul 2, 2015 at 20:22
Add a comment |
3

Just to add another way to do it:

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

I had a few moments of hair-pulling on this one because the base64 Linux tool and also the openssl can decode, indeed. But I have this particular base64 encoded file that decodes to slightly wrong value. The few bytes do match, but then there is this presence of EF BF BD EF BF BD when I view in hexedit viewer. And then the next sequence of bytes match again when compared to the correctly decoded expected output. These weird sequence of bytes got inserted in the in-betweens, sometime as EF BF BD only.

To resolve the matter, I have to look how the Java sender encodes it and then I created a small java base64 decoder. And now I can decoded to the expected value.

You must log in to answer this question.

Highly active question. Earn 10 reputation (not counting the association bonus) in order to answer this question. The reputation requirement helps protect this question from spam and non-answer activity.

Источник

Bash base64 encode and decode

To encode or decode standard input/output or any file content, Linux uses base64 encoding and decoding system. Data are encoded and decoded to make the data transmission and storing process easier. Encoding and decoding are not similar to encryption and decryption. Encoded data can be easily revealed by decoding. So, this command line utility tool can’t be used for data security. Alphabet, number and ‘=’ symbol are used to encode any data.

Syntax:

base64 [OPTION] [INFILE] [OUTFILE]

You can use different types of options with base64 command. Data can be taken from any file or standard input while encoding or decoding. After encode or decode, you can send the output in a file or print the output in the terminal.

Options:

-e or –encode

This option is used to encode any data from standard input or from any file. It is the default option.

-d or –decode
This option is used to decode any encoded data from standard input or from any file.

-n or –noerrcheck
By default, base64 checks error while decoding any data. You can use –n or –noerrcheck option to ignore checking at the time of decoding.

-u or –help
This option is used to get information about the usage of this command.

-i, –ignore-garbage
This option is used to ignore non-alphabet character while decoding.

–copyright
It is used to get copyright information.

–version
It is used to get the version information.

How you use the base64 command in Linux is shown in this tutorial by using some examples.

Example#1: Encoding text data

You can encode any text data by using base64 in the command line. When you want to encode any data using base64 then using -e or –encode option is optional. So, if you don’t mention any option with base64 then it will work for encoding. The following command will encode the data, ‘linuxhint.com’ and print the encoded data as output.

Example#2: Decoding text data

The following command will decode the encoded text, ‘bGludXhoaW50LmNvbQ==‘ and print the original text as output.

Example#3: Encoding text file

Create a text file named, ‘sample.txt’ with the following text that will be encoded by using base64.

You can print the encoded text in the command line or store the encoded text into another file. The following command will encode the content of the sample.txt file and print the encoded text in the terminal.

The following commands will encode the content of the sample.txt file and save the encoded text into the encodedData.txt file.

Example#4: Decoding text file

The following command will decode the content of the encodedData.txt file and print the output in the terminal

The following commands will decode the content of the encodedData.txt file and store the decoded content into the file, originalData.txt.

Example#5: Encoding any user-defined text

Create a bash file named encode_user_data.sh with the following code. The following script will take any text data as input, encode the text by using base64 and print the encoded text as output.

#!/bin/bash
echo "Enter Some text to encode"
read text
etext = ` echo -n $text | base64 `
echo "Encoded text is : $etext "

Validate the text is encoded correctly by piping the encoded text returned from your execution of the script to base64 –decode to confirm the original text is returned. Below you can see how to validate assuming My secret textwas the string encoded.

Example#6: Checking user validity by decoding text

Create a bash file named checkValidity.sh and add the following code. In this example, a secret text is taken from the user. A predefined encoded text is decoded by base64 and compared with the user input. If both values are equal then the output will be ‘You are authenticated’ otherwise the output will be ‘You are not authenticated’. Using this simple decoding code, normal validation can be done very easily. In this example the secret text that will result in success is 777799. This would likely not be hard coded in the script but more dynamic in a real world application.

#!/bin/bash
echo "Type your secret code"
read secret
otext = ` echo 'Nzc3Nzk5Cg==' | base64 --decode `
if [ $secret == $otext ] ; then
echo "You are authenticated"
else
echo "You are not authenticated"
fi

Conclusion:

For any sensitive data like password or any confidential data, encoding and decoding system is not suitable at all. You must use encryption and decryption system for securing these type of data.

References:

About the author

Fahmida Yesmin

I am a trainer of web programming courses. I like to write article or tutorial on various IT topics. I have a YouTube channel where many types of tutorials based on Ubuntu, Windows, Word, Excel, WordPress, Magento, Laravel etc. are published: Tutorials4u Help.

Источник

How to base64 encode and decode from command-line

In this tutorial, you will learn how to base64 encode and decode from the command-line on Linux. You will also learn what base64 encoding is and why it should never be used to protect data from unauthorized access.

Base64 encoding and decoding data has many use cases. One being is ensuring data integrity when transferring data over the network, while another is storing Secrets in Kubernetes.

After reading this tutorial you will understand how to easily encode files or strings, and then decode them back.

How to base64 encode on Ubuntu, Debian, OSX, and Red Hat

If you are running popular linux distributions, such as Ubuntu, Debian, CentOS, or Red Hat, the base64 command-line tool is typically pre-installed. You should not have to perform any additional steps.

OSX also comes bundled with its own version of base64.

Why Base64 Encode Data

Transferring an ASCII file over the network can cause corruption if not decoded correctly. The reason is ASCII files are string converted to bytes, and when those bytes are decoded incorrectly back to ASCII your data becomes corrupt.

Base64 was introduced as a way to convert your ASCII data into arbitrary bytes, where they could then be transferred as bytes, and decoded correctly back to ASCII.

In short, base64 encoding ensures the integrity of our data when transferred over the network.

Base64 is not Encryption

Encoding files is not encryption and should never be used to secure sensitive data on disk. Rather it is a useful way of transferring or storing large data in the form of a string.

While it may obfuscate that actual data from should surfers, anyone who has access to base64 encoded data can easily decode it.

Base64 Encoding a String

To base64 encode string you can pipe an echo command into the base64 command-line tool. To ensure no extra, hidden characters are added use the -n flag.

Without the -n flag you may capture a hidden characters, like line returns or spaces, which will corrupt your base64 encoding.

Which will output the following

Base64 Encoding a File

This will output a very long, base64 encoded string. You may want to write the stdout to file instead.

bas64 /path/to/file > output.txt

Decoding Strings

To decode with base64 you need to use the --decode flag. With encoded string, you can pipe an echo command into base64 as you did to encode it.

Using the example encoding shown above, let’s decode it back into its original form.

echo -n 'bXktc3RyaW5n' | base64 --decode

Provided your encoding was not corrupted the output should be your original string.

Decoding Files

To decode a file with contents that are base64 encoded, you simply provide the path of the file with the --decode flag.

As with encoding files, the output will be a very long string of the original file. You may want to output stdout directly to a file.

base64 --decode /path/to/file > output.txt

Conclusion

In this tutorial, you learned how to base64 encode files and strings. This something commonly done to transfer files in such a way that it remains

Источник

Оцените статью
Adblock
detector