Decoding base64 in linux

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 :

Читайте также:  Nano linux очистить файл

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

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.

Источник

Base64 Encode and Decode From Command Line

Encoding is the process used to convert data in a format required for effective transmission or storage. In contrast, decoding is opposite to the encoding method which converts the encoded data back to its original format. Base64 is the encoding process where the binary data is converted into ASCII. Base64 encoding is mostly required to avoid the transmission problems that occur when binary data is transmitted to text-based systems which cannot handle the binary data properly. As a result, the information is lost or corrupted during transmission.

Some of the uses of encoding are:

For encoding data, Base64 uses only alphabet, number and = symbol. For instance, c2FtcGxlCg== is a valid encoded data while b?HV3.Zh2J== is not a valid encoded data.

In this Linux Hint tutorial, we will explain how to use the base64 command to encode and decode the data in a string or a file. We have performed the commands on Ubuntu 20.04 Focal Fossa system. However, you can also run the same commands on other Linux distributions.

Base64 Syntax

Here is the syntax for encoding using Base64:

Options

Some of the command-line options that can be used with base64 command are:

Use this option to decode a file or a string.

Use this option to display help regarding the usage of base64.

Use this option while decoding to ignore non-alphabet characters

Use this option to display version information

Encoding String

You can easily encode a string using the base64 command. For instance, to encode a sample text “Welcome to Linux” to base64, the command would be:

This command will encode the text in the string using base64 and print the encoded text to standard output as shown in the Terminal Output above.

You can also save the encoded output to a file rather than printing to standard output using the redirection operator (>). The following command will encode the text and save the output to a file named “encodedfile.txt:

To view the encoded file, you can use the cat command:

Decoding String

You can also decode the base64 encoded text using the –decode or -d option. For instance to decode base64 encoded text “V2VsY29tZSB0byBMaW51eAo=”, the command would be:

This command will decode the base64 encoded text and print the original text on the standard output as shown in the following Terminal Output.

Terminal Output:
linuxhint@hp34:~$ echo 4 oCcV2VsY29tZSB0byBMaW51eOKAnQo = | base64 --decode
“Welcome to Linux”

Encoding Text File

The base64 command can also be used to encode a text file. For this example lets create a text file with some content first using this command on the terminal, or use any textfile.

To encode this or any text file named “testfile.txt”, the command would be:

This command will encode the specified text file and print its encoded form on the standard output as shown in the following Terminal Output:

linuxhint@hp34:~$ base64 testfile.txt
SSBMb3ZlIExpbnV4CkkgTG92ZSBMaW51eApJIExvdmUgTGludXgKTGludXhIaW50IGlzIG15IGhv
bWVwYWdlCg ==

You can also save the encoded output to a file rather than printing to standard output using the redirection operator (>). The following command will convert the text in the file using base64 and save the output to another file named “encodedfile.txt”. The command does not print anything to the screen.

To view the encoded file, you can use the cat command:

Terminal Output:
linuxhint@hp34:~$ base64 testfile.txt > encodedfile.txt
linuxhint@hp34:~$ cat encodedfile.txt
SSBMb3ZlIExpbnV4CkkgTG92ZSBMaW51eApJIExvdmUgTGludXgKTGludXhIaW50IGlzIG15IGhv
bWVwYWdlCg ==

Decoding Text File

To decode an encoded text file, use the –decode or -d option. For instance to decode base64 encoded text file “encodedfile.txt”, the command would be:

This command will decode the base64 encoded text file and print the original text on the standard output as shown in the following Terminal Output:

Terminal Output:
linuxhint@hp34:~$ base64 -d encodedfile.txt
I Love Linux
I Love Linux
I Love Linux
LinuxHint is my homepage
linuxhint@hp34:~$

Encoding User Input

Using the base64 encoding, we can encode any user-provided data. For this purpose, we will need to create a script that will take user input, encode it using base64 encoding, and print the encoded data on standard output. Create a script “test.sh” with the following code:

#!/bin/bash
# Print message to ask for input
echo "Provide Some data to encode"
# Save the input to a variable named “data”
read data
# Encode using base64 encoding and save the output to a variable “encod_data”
encod_data = ` echo -n $data | base64 `
# Print encoded output
echo "Encoded text is : $encod_data "

Run the script as follows:

After running the script, you will be asked to input the data that you want to encode. Type some data and press Enter, and you will receive the encoded output on the screen as shown below:

Terminal Output:
linuxhint@hp34:~$ chmod 755 test.sh; . / test.sh
Provide Some data to encode
I love linux
Encoded text is : SSBsb3ZlIGxpbnV4

This encoded text can be sent over the internet or to another program and then decoded later using a simple command. For this command we assume the receiving program, decode.sh, has put the encoded data into a variable in BASH called RECEIVED_ENCODING.

#!/bin/bash
RECEIVED_ENCODING =SSBsb3ZlIGxpbnV4
RECEIVED_STRING = ` echo $RECEIVED_ENCODING | base64 --decode `
echo $RECEIVED_STRING

Here you can see the results of the receiving program in the Terminal Output:

Conclusion

This is how you can use the base64 to encode and decode a string or a file from the command line. The results can be printed on the standard output, saved in a variable or a file, or passed over the network to another program. However, remember that encoding is not similar to encryption, and one can easily reveal the encoded data, so it is not recommended to use encoding for the transmission of sensitive data unless its also encrypted.

About the author

Linux Wolfman

Linux Wolfman is interested in Operating Systems, File Systems, Databases and Analytics and always watching for new technologies and trends. Reach me by tweeting to @linuxhint and ask for the Wolfman.

Источник

Base64 Encode and Decode in Linux

Base64 Encode and Decode in Linux

Base64 is an encoding and decoding scheme that often used to convert binary data to an printable ASCII text format, and vice versa. This tutorial shows how to perform Base64 encoding and decoding in Linux.

Encoding string

The base64 command can be used to perform Base64 encoding and decoding. String can be encoded as follows:

Decoding string

For Base64 decoding use --decode option.

echo 'SGVsbG8gd29ybGQK' | base64 --decode

Encoding file

Create a text file for testing:

echo 'Hello world' > data.txt

Encode content of a text file and print result in the terminal:

Encode content of a text file and save result in another file:

Decoding file

Create a text file that contains Base64 encoded data:

echo 'SGVsbG8gd29ybGQK' > encoded_data.txt

Decode content of a text file and print result in the terminal:

base64 --decode encoded_data.txt

Decode content of a text file and save result in another file:

base64 --decode encoded_data.txt > out.txt

Install MongoDB Inside Docker Container in Linux

Install Traefik Inside Docker Container in Linux

Install Lightstreamer Inside Docker Container in Linux

Lightstreamer is a messaging server to stream data for web and mobile applications. Lightstreamer offers.

Источник

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