- How to base64 encode and decode from command-line
- How to base64 encode on Ubuntu, Debian, OSX, and Red Hat
- Why Base64 Encode Data
- Base64 is not Encryption
- Base64 Encoding a String
- Base64 Encoding a File
- Decoding Strings
- Decoding Files
- Conclusion
- Base64 Encode and Decode From Command Line
- Base64 Syntax
- Options
- Encoding String
- Decoding String
- Encoding Text File
- Decoding Text File
- Encoding User Input
- Conclusion
- About the author
- Linux Wolfman
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
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.