How to Convert Base64 Data to Files on Linux and Mac OS

Written by vikramaruchamy | Published 2023/04/13
Tech Story Tags: linux | bash | base64 | file | decoding | data | programming-languages | programming

TLDRBase64 is an encoding scheme that represents binary files in a textual format. This tutorial teaches you the different methods to decode [Base64](https://hackernoon.com/learn-more-about-data-encoding-base64-vs-base58-9q263ehf) data into a file using commands and online tools.via the TL;DR App

Bash is a Unix shell and command language. These bash commands are widely used in Mac OS X and Unix-like operating systems for interacting with the system via commands. Bash also has a utility command, Base64, for encoding and decoding data.

Base64 is an encoding scheme that represents binary files in a textual format.

This tutorial teaches you the different methods to decode Base64 data into a file in Unix-like operating systems using commands and online tools.

Decode a Base64 Data into a File Using Base64 Bash Command

The base64 command, with the flag --decode, decodes the base64 data into a string.

It is available since the Mac OS X Version 10.7 and all UNIX-like operating systems. If you’re using a Mac OS X version older than 10.7, check the OpenSSL Base64 explained in the next section of this tutorial.

To decode base64 data into a file,

  • Invoke the Base64 command with the -d parameter. It’ll decode base64 encoded data
  • Use the here-string operator and pass the Base64 encoded data
  • Direct the output to a file using > and pass the file name with an extension. You need to pass the correct extension of the file equivalent to the Base64 string. Otherwise, the output file will be corrupted due to the incorrect extension

Important: Ensure you have write access to the current working directory needed to create the new output file.

Code

The following code demonstrates creating an image file from the Base64 encoded string.

base64 -d <<< "iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg==" > image.png

Decode Base64 data into a File Using OpenSSL Base64 Command

OpenSSL is a software library that enables secure network communication applications. Base64 is commonly used as a primary encoding when transmitting information over a network. OpenSSL offers a command-line tool to facilitate the process of encoding or decoding strings using Base64 encoding.

Use this method when your system doesn’t have the Base64 command line utility. For example, Mac OS versions earlier than 10.7.

To decode a base64 string into a file using the OpenSSL base64 command line utility,

  • Invoke the openssl base64 command with the -d parameter
  • Use the here-string operator and pass the Base64 encoded data
  • Direct the output to a file using the -out parameter and pass the file name with an extension. You need to pass the correct extension of the file equivalent to the Base64 string

Important: Ensure you have write access to the current working directory needed to create the new output file.

Code

$ openssl base64 -d <<< "iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg==" -out image.png

Using Online Decoder Tool

You can use the Online Base64 Decoder Tool in the browser to decode the Base64 string into a file.

To decode a Base64 string into a file using the tool,

  • Add the Base64 encoded text in a .txt file and upload it to the decoder tool
  • Click Decode, and It will create an output file based on the input text

The main advantage of using the online Base64 decoder tool is you do not need to know the extension of the file types. For example, the image file can be in different formats such as .jpg, .png or a .gif. The online tool will detect the file extension automatically based on the input string and create a file equivalent to the string with the proper file extension.

Conclusion

You’ve learned to decode a Base64 string into a file using different methods. Each method is applicable in different use cases.

For example, the command line will be helpful when you want to write a script to decode a Base64 string into a file multiple times programmatically. For one-time use, you can use the Online tool and easily download the decoded file. You can select the method to decode based on your use case of the application.


Written by vikramaruchamy | Technical writer and AWS Community Builder
Published by HackerNoon on 2023/04/13