Base64 encoding is a commonly used method to represent binary data as ASCII character. It is commonly employed in various areas, such as encoding binary attachments in email, storing data in XML files, and transmitting complex data over the internet. In this article, let us get into several methods to decode Base64 in Linux,
Using the base64 Command
One of the easiest methods to decode Base64 in Linux is by using a built in base64 command. This utility allows both encoding and decoding operations to be executed via the terminal. To decode Base64 encoded file, use following command,
base64 -d -i encoded_file.txt -o decoded_file.txt
This command decodes the content of the encoded_file.txt and writes the result to decoded_file.txt. The -d option specifies the decoding operation.
Decoding Inline:
If you want to decode a Base64 string directly from the command line without writing them into a file, you can echo the encoded string and pipe it to the base64 command
echo -n 'SGVsbG8gd29ybGQh' | base64 -d
The -n option prevents the echo command from adding a newline character, ensuring accurate decoding.
Using Python
Python is a scripting language that can be used for various tasks, including Base64 decoding. The base64 module in Python gives functions for decoding Base64 data. Create a Python script, for example, decode_base64.py,
import base64
encoded_data = "SGVsbG8gd29ybGQh"
decoded_data = base64.b64decode(encoded_data)
print(decoded_data)
Run the script using:
python3 decode_base64.py
This method offers flexibility for integration into more complex scripts and workflows.
Utilizing OpenSSL:
OpenSSL, a robust open-source toolkit, which includes functionality for Base64 encoding and decoding. To decode a Base64-encoded file, use following command,
echo 'SGVsbG8gd29ybGQh' | openssl base64 -d
This command uses the openssl command to decode the provided Base64 string. The -d option specifies the decoding operation. I appreciate your understanding, and I hope this clarifies the usage of OpenSSL for Base64 decoding in Linux.
Decoding Multiple Files Concurrently
If you have multiple Base64-encoded files, and wanted to decode them all at the same time, you can use the xargs command in combination with the base64 comand. Create a file, file_list.txt, with names of the encoded files (one per line) and run:
This command reads file names from file_list.txt and decodes each file concurrently, appending “decoded_” to the output file names.
cat file_list.txt | xargs -n 1 -I {} sh -c 'base64 -d -i {} > decoded_{}'
Conclusion:
Base64 decoding is a really simple task if you guys know what you are knowing. These are three methods that are easy to implement and there are other ways of doing it too. Please choose the ones that fit your need.