Base64 encoding and decoding from the command line

aboutbase64

In the next article we are going to take a look at how can we encode and decode from the terminal with base64. Encoding is the process used to convert data into a format required for effective transmission or storage. In contrast, decoding is the opposite of the encoding method that converts the encoded data to its original format. Base64 is the encoding process where binary data is converted to ASCII.

Base64 encoding is mainly used to avoid transmission problems, which occur when binary data is transmitted to text-based systems that cannot handle this binary data correctly. As a result, information is lost or corrupted during transmission.

Base 64 is a positional numbering system that uses 64 as the base. It is the highest power that can be represented using only printable ASCII characters. This has led to its use for email encryption, PGP, and other applications. All the famous variants that go by the name Base64 use the character range AZ, az and 0-9 in this order for the first 62 digits, but the symbols chosen for the last two digits vary considerably from one to the other. Some of the uses of encryption are; compressing data, hiding data, or transmitting data in another format.

In the following lines we will see how to use base64 command to encode and decode data in string or file. To carry out this example I am going to use the terminal (Ctrl + Alt + T) of the Ubuntu 20.04 Focal Fossa system.

Syntax for encoding using Base64

base64 [OPCIÓN] ... [ARCHIVO]

Options

base64 help

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

  • -help → We will use this option to show help on using base64.
  • -do –decode → We will use this option to decode a file or string.
  • -i, --ignore-garbage → This option will help us while we decode to ignore non-alphabet characters.
  • -version → This other option show information about the version we use.

String encoding with Base64

Users can encode a string with base64 command. The command to use would be:

encode sample text

echo “Ubunlog” | base64

This command will encode the text in the string using base64 and print the encoded text to standard output.

We can also save the encoded output to a file, instead of printing it to standard output. The following command will encode the text and save the output to a file called "encodedfile.txt«:

text inside base64 file

echo “texto de ejemplo” | base64 > archivoCodificado.txt

For, view the content of the encoded file, we can use the command cat, as you can see in the previous capture.

Decoding string

We can decode base64 encoded text using –decode or -d option. To decode base64 encoded text '4oCcdGV4dG8gZGUgZWplbXBsb + KAnQo =', the command would be:

decode sample text

echo “4oCcdGV4dG8gZGUgZWplbXBsb+KAnQo=” | base64 --decode

This command will print the original text to standard output as shown in the above screenshot.

We will also be able to save decoded output to file, rather than printing to standard output. The following command will decode the encoded text and save the original text in a file called "Decoded file .txt"

text inside base64 decoded file

echo “4oCcdGV4dG8gZGUgZWplbXBsb+KAnQo=” | base64 --decode > archivoDecodificado.txt

For, view the content of the decoded file, we can use the command cat.

Encoding a text file

The command base64 can also be used to encode a text file. If we were interested in encoding a text file called 'archivotext.txt', the command to use would be:

text file encoding

base64 archivotexto.txt

This command will encode the specified text file and print its encoded form to standard output.

As well we can save the encoded output to a file, rather than printing it to standard output. The following command will convert the text in the file using base64 and save the output to another file called "encodedfile.txt »:

base64 encoded file

base64 archivotexto.txt > archivoCodificado.txt

For, view the content of the encoded file, we can use the command cat.

Decode the text file

For, decode an encoded text file, we will have to use the –decode or -d option. To decode the content of the base64 encoded text file 'encodedfile .txt', the command to use would be:

decode text file content

base64 -d archivoCodificado.txt

This command will decode the base64 encoded text file and print the original text on standard output.

We will also be able to save decoded output to file, rather than printing to standard output. The following command will decode the encoded text and save the original text in a file called "decodedfile.txtWhich can be viewed later using the command cat:

decode text file

base64 -d archivoCodificado.txt > archivoDecodificado.txt

This is how you can use base64 to encode and decode a string or a file from the terminal. It is important to remember that encoding is not similar to encryption, and one can easily reveal the encoded data. For this reason it is not recommended to use encryption for the transmission of confidential data.. More information in Wikipedia.


The content of the article adheres to our principles of editorial ethics. To report an error click here!.

Be the first to comment

Leave a Comment

Your email address will not be published.

*

*

  1. Responsible for the data: Miguel Ángel Gatón
  2. Purpose of the data: Control SPAM, comment management.
  3. Legitimation: Your consent
  4. Communication of the data: The data will not be communicated to third parties except by legal obligation.
  5. Data storage: Database hosted by Occentus Networks (EU)
  6. Rights: At any time you can limit, recover and delete your information.