Linux Command Line Basics: Base64 Encoding and Decoding Strings
11:20, 24.04.2024
Base64 Encoding and Decoding in Linux Command Line: An Overview
Among a variety of methods for representing binary data in ASCII string format, Base64 is one of the most common ones, employed particularly for the encoding of images as other documents in email attachments and in URLs as well as other cases, where there is a need of converting something into pure text. In the following, we are going to show you how to use command terminal tools in Linux for encoding and decoding Base64 strings.
1. Base64 Usage
Base64 is the basic command for base64 that can be used both for encoding and decoding.
1.1. Encoding with Base64
To encode a file or a piece of text, use the command:
base64 filename > outputfile
“Filename” is to be replaced with the name of the file you want to encode and “outputfile” with the desired name for the encoded file.
To encode a piece of text:
echo -n "yourtext" | base64
Replace “yourtext” with the text you want to encode respectively.
1.2. Decoding Base64
In Base64 decoding is done with the same base 64 command with the -d option appended.
To decode a file, type:
base64 -d encodedfile > outputfile
Endodedfile is to be replaced with the name of your file whilst outputfile is to be replaced with the desired name of the outputfile.
To decode text, type:
echo -n "encodedtext" | base64 -d
Replace "encodedtext" with the text to be encoded.
For instance:
Encoding:
echo -n "Hello, Base64!" | base64
Output: SGVsbG8sIEJhc2U2NCE=
base64 mydocument.txt > encoded_document.txt
Decoding:
echo -n "SGVsbG8sIEJhc2U2NCE=" | base64 -d
Output: Hello, Base64!
base64 -d encoded_document.txt > decoded_document.txt
2. OpenSSL for Base64 Operations
Another option that is available to using the OpenSSL, is that, among other things, offers Base64 for encoding and decoding. Let’s have a look.
Base64 Encoding with OpenSSL
You can encode a file or text with OpenSLL by using openssl enc command with the base64 option.
Encoding a file:
openssl enc -base64 -in filename -out outputfile
Replace filename with the name of the file you’re going to encode and the outputfile with the name you wish for the encoded file.
To encode text:
echo -n "yourtext" | openssl enc -base64
Replace yourtext with the text you want to encode.
Base64 Decoding with OpenSSL
Decoding a Base64-encoded file or string is performed with the same openssl enc command as well as -d and -base64 options.
To decode a file:
openssl enc -base64 -d -in encodedfile -out outputfile
encodedfile is to be replaced with the name of the Base64-encoded file and outputfile with the name you want for the decoded file.
To decode a text:
echo -n "encodedtext" | openssl enc -base64 -d
encodedtext is to be replaced with the Base64-encoded text.
Examples:
Encoding:
echo -n "Hello, OpenSSL!" | openssl enc -base64
Output:SGVsbG8sIE9wZW5TU0wTIQ==
openssl enc -base64 -in mydocument.txt -out encoded_document.txt
Decoding:
echo -n "SGVsbG8sIE9wZW5TU0wTIQ==" | openssl enc -base64 -d
Output: Hello, OpenSSL!
openssl enc -base64 -d -in encoded_document.txt -out decoded_document.txt
3. Base64 Operations with Python
You can also perform Base64 operations with Python and you can do it straight away, since base64 module is included in the standard Python library.
Base64 Encoding with Python
Base64 Encoding with Python is performed with the base64.b64encode() function.
Encoding a string:
import base64
original_text = "Hello, Base64!"
encoded_text = base64.b64encode(original_text.encode()).decode()
print(encoded_text)
In the example above, the "Hello, Base64!" is converted to its Base64-encoded representation.
Encoding a file:
import base64
with open("filename", "rb") as file:
binary_data = file.read()
encoded_data = base64.b64encode(binary_data).decode()
with open("encoded_file.txt", "w") as encoded_file:
encoded_file.write(encoded_data)
“Filename” is to be replaced with the name of the file you want to encode and "encoded_file.txt" with the desired name for the encoded file.
Base64 Decoding with Python
Base64 decoding with Pythong can be performed with the base64.b64decode() function.
To decode a string:
import base64
encoded_text = "SGVsbG8sIEJhc2U2NCE="
decoded_text = base64.b64decode(encoded_text).decode()
print(decoded_text)
In this example, the Base64-encoded string "SGVsbG8sIEJhc2U2NCE=". is decoded.
Decoding a file:
import base64
with open("encoded_file.txt", "r") as encoded_file:
encoded_data = encoded_file.read()
decoded_data = base64.b64decode(encoded_data)
with open("decoded_file.bin", "wb") as decoded_file:
decoded_file.write(decoded_data)
"encoded_file.txt" is to be replaced with the name of the Base64-encoded file and "decoded_file.bin" with the desired name for the decoded file.
4. Base64 Manipulation in Perl
Perl also offers options for the employment of Base64 encoding and decoding operations with a special dedicated module MIME::Base64 with encode_base64 and decode_base64 functions for encoding and decoding data in Base64 format.
Base64 Encoding in Perl
To encode a string:
use MIME::Base64;
my $original_text = "Hello, Base64!";
my $encoded_text = encode_base64($original_text);
print $encoded_text;
In the example above, the string “Hello, Base64!” is encoded to its Base64 representation.
To encode a file:
use MIME::Base64;
my $filename = "filename";
open my $fh, '<', $filename or die "Cannot open file: $!";
my $binary_data = do { local $/; <$fh> };
close $fh;
my $encoded_data = encode_base64($binary_data);
open my $encoded_fh, '>', "encoded_file.txt" or die "Cannot open file: $!";
print $encoded_fh $encoded_data;
close $encoded_fh;
The “filename” is to be replaced with the name of the file you wish to encode and "encoded_file.txt" with the desired filename respectively.
Base64 Decoding in Perl
Decoding Base64-encoded data in Perl can be performed with the decode_base64 function from the MIME::Base64 module.
Decoding a string:
use MIME::Base64;
my $encoded_text = "SGVsbG8sIEJhc2U2NCE=";
my $decoded_text = decode_base64($encoded_text);
print $decoded_text;
In the example above, the Base64-encoded string "SGVsbG8sIEJhc2U2NCE=" is decoded.
Decoding a file:
use MIME::Base64;
my $encoded_filename = "encoded_file.txt";
open my $encoded_fh, '<', $encoded_filename or die "Cannot open file: $!";
my $encoded_data = do { local $/; <$encoded_fh> };
close $encoded_fh;
my $decoded_data = decode_base64($encoded_data);
open my $decoded_fh, '>', "decoded_file.bin" or die "Cannot open file: $!";
print $decoded_fh $decoded_data;
close $decoded_fh;
"encoded_file.txt" is to be replaced with the name of the Base64-encoded file "decoded_file.bin" with the desired name for the decoded file.
Conclusion
Base64 is an important tool for decoding information into the text form as well as for eventually decoding it. In the guide above we’ve covered the usage of the Base64 tool for various interfaces. We hope that this instructions was clear enough and wish you good luck.