- How do you convert a base64 utf-8 encoded string to a binary file from bash?
- 1 Answer 1
- Packages
- Encoding
- 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
- Bash base64 encode and decode
- Syntax:
- base64 [OPTION] [INFILE] [OUTFILE]
- Options:
- Example#1: Encoding text data
- Example#2: Decoding text data
- Example#3: Encoding text file
- Example#4: Decoding text file
- Example#5: Encoding any user-defined text
- Example#6: Checking user validity by decoding text
- Conclusion:
- References:
- About the author
- Fahmida Yesmin
How do you convert a base64 utf-8 encoded string to a binary file from bash?
bash does not have a bas64 encoder/decoder built in. There are utilities like openssl and the aptly named base64 utility that does this though. What do you mean by «utf-8 encoded»?
1 Answer 1
Packages
Yes, there are several. The package coreutils (installed by default in debian) carry both base32 and base64. They could either encode or decode:
$ printf '%s' "abc" | base64 YWJj $ printf '%s' "abc" | base64 | base64 -d abc
Understand that using echo might add a trailing new line that will change the resulting base64 encoded string:
There are also other packages that have similar tools in packages basez and openssl . The latter is also usually installed by default, the former is not.
$ printf '%s' "abc" | openssl base64 YWJj
Encoding
That the source string is encoded in any locale (codepage) is irrelevant to base64 encoding. A base64 program encodes bytes, not characters.
$ printf '%s' "éäìǫ" | base64 | base64 -d éäìǫ
Will work on any system exactly the same. Well, in any sane system in which echo «éäìǫ» will also print éäìǫ on the command line.
Of course, if the source string is encoded in one system and then decoded in a system with a different locale, it is quite probable that you will get a Mojibake string. That is not a problem to be solved by base64, it is an issue to be solved by changing the encoding of the string. Probably with iconv .
$ echo -n "Москва" | base64 # in a utf8 locale 0JzQvtGB0LrQstCw
However, in a Cyrillic locale with iso889-5 (maybe ru_RU.ISO-8859-5, there are other languages with Cyrillic):
$ echo "0JzQvtGB0LrQstCw" | base64 -d ааОбаКаВаА $ echo "0JzQvtGB0LrQstCw" | base64 -d | iconv -f utf8 -t iso8859-5 Москва
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.
Bash base64 encode and decode
To encode or decode standard input/output or any file content, Linux uses base64 encoding and decoding system. Data are encoded and decoded to make the data transmission and storing process easier. Encoding and decoding are not similar to encryption and decryption. Encoded data can be easily revealed by decoding. So, this command line utility tool can’t be used for data security. Alphabet, number and ‘=’ symbol are used to encode any data.
Syntax:
base64 [OPTION] [INFILE] [OUTFILE]
You can use different types of options with base64 command. Data can be taken from any file or standard input while encoding or decoding. After encode or decode, you can send the output in a file or print the output in the terminal.
Options:
-e or –encode
This option is used to encode any data from standard input or from any file. It is the default option.
-d or –decode
This option is used to decode any encoded data from standard input or from any file.
-n or –noerrcheck
By default, base64 checks error while decoding any data. You can use –n or –noerrcheck option to ignore checking at the time of decoding.
-u or –help
This option is used to get information about the usage of this command.
-i, –ignore-garbage
This option is used to ignore non-alphabet character while decoding.
–copyright
It is used to get copyright information.
–version
It is used to get the version information.
How you use the base64 command in Linux is shown in this tutorial by using some examples.
Example#1: Encoding text data
You can encode any text data by using base64 in the command line. When you want to encode any data using base64 then using -e or –encode option is optional. So, if you don’t mention any option with base64 then it will work for encoding. The following command will encode the data, ‘linuxhint.com’ and print the encoded data as output.
Example#2: Decoding text data
The following command will decode the encoded text, ‘bGludXhoaW50LmNvbQ==‘ and print the original text as output.
Example#3: Encoding text file
Create a text file named, ‘sample.txt’ with the following text that will be encoded by using base64.
You can print the encoded text in the command line or store the encoded text into another file. The following command will encode the content of the sample.txt file and print the encoded text in the terminal.
The following commands will encode the content of the sample.txt file and save the encoded text into the encodedData.txt file.
Example#4: Decoding text file
The following command will decode the content of the encodedData.txt file and print the output in the terminal
The following commands will decode the content of the encodedData.txt file and store the decoded content into the file, originalData.txt.
Example#5: Encoding any user-defined text
Create a bash file named encode_user_data.sh with the following code. The following script will take any text data as input, encode the text by using base64 and print the encoded text as output.
#!/bin/bash
echo «Enter Some text to encode»
read text
etext = ` echo -n $text | base64 `
echo «Encoded text is : $etext «
Validate the text is encoded correctly by piping the encoded text returned from your execution of the script to base64 –decode to confirm the original text is returned. Below you can see how to validate assuming My secret textwas the string encoded.
Example#6: Checking user validity by decoding text
Create a bash file named checkValidity.sh and add the following code. In this example, a secret text is taken from the user. A predefined encoded text is decoded by base64 and compared with the user input. If both values are equal then the output will be ‘You are authenticated’ otherwise the output will be ‘You are not authenticated’. Using this simple decoding code, normal validation can be done very easily. In this example the secret text that will result in success is 777799. This would likely not be hard coded in the script but more dynamic in a real world application.
#!/bin/bash
echo «Type your secret code»
read secret
otext = ` echo ‘Nzc3Nzk5Cg==’ | base64 —decode `
if [ $secret == $otext ] ; then
echo «You are authenticated»
else
echo «You are not authenticated»
fi
Conclusion:
For any sensitive data like password or any confidential data, encoding and decoding system is not suitable at all. You must use encryption and decryption system for securing these type of data.
References:
About the author
Fahmida Yesmin
I am a trainer of web programming courses. I like to write article or tutorial on various IT topics. I have a YouTube channel where many types of tutorials based on Ubuntu, Windows, Word, Excel, WordPress, Magento, Laravel etc. are published: Tutorials4u Help.