Base64 encoder windows command line

How to base64 encode and decode from command-line

Overiew

In this tutorial, you will learn how to base64 encode and decode from the command-line on Linux. You will also learn what base64 encoding is and why it should never be used to protect data from unauthorized access.

Base64 encoding and decoding data has many use cases. One being is ensuring data integrity when transferring data over the network, while another is storing Secrets in Kubernetes.

After reading this tutorial you will understand how to easily encode files or strings, and then decode them back.

How to base64 encode on Ubuntu, Debian, OSX, and Red Hat

If you are running popular linux distributions, such as Ubuntu, Debian, CentOS, or Red Hat, the base64 command-line tool is typically pre-installed. You should not have to perform any additional steps.

OSX also comes bundled with its own version of base64.

Why Base64 Encode Data

Transferring an ASCII file over the network can cause corruption if not decoded correctly. The reason is ASCII files are string converted to bytes, and when those bytes are decoded incorrectly back to ASCII your data becomes corrupt.

Base64 was introduced as a way to convert your ASCII data into arbitrary bytes, where they could then be transferred as bytes, and decoded correctly back to ASCII.

In short, base64 encoding ensures the integrity of our data when transferred over the network.

Base64 is not Encryption

Encoding files is not encryption and should never be used to secure sensitive data on disk. Rather it is a useful way of transferring or storing large data in the form of a string.

While it may obfuscate that actual data from should surfers, anyone who has access to base64 encoded data can easily decode it.

Base64 Encoding a String

To base64 encode string you can pipe an echo command into the base64 command-line tool. To ensure no extra, hidden characters are added use the -n flag.

Without the -n flag you may capture a hidden characters, like line returns or spaces, which will corrupt your base64 encoding.

Which will output the following

Base64 Encoding a File

To base64 encode a file

This will output a very long, base64 encoded string. You may want to write the stdout to file instead.

Decoding Strings

To decode with base64 you need to use the —decode flag. With encoded string, you can pipe an echo command into base64 as you did to encode it.

Using the example encoding shown above, let’s decode it back into its original form.

Provided your encoding was not corrupted the output should be your original string.

Decoding Files

To decode a file with contents that are base64 encoded, you simply provide the path of the file with the —decode flag.

As with encoding files, the output will be a very long string of the original file. You may want to output stdout directly to a file.

Conclusion

In this tutorial, you learned how to base64 encode files and strings. This something commonly done to transfer files in such a way that it remains

Base64 Encode and Decode From Command Line

Some of the uses of encoding are:

  • Data compression
  • Data hiding
  • Transmission of data in another format
Читайте также:  Windows installer для java

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 article, 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. For running the commands, we have used the command line Terminal application, which can be accessed using the Ctrl+Alt+T keyboard shortcut.

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 following screenshot

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 screenshot.

You can also save the decoded output to a file rather than printing to standard output using the redirection operator (>). The following command will decode the encoded text and save the original text to a file named “decodedfile.txt:

Encoding Text File

The base64 command can also be used to encode a text file. For instance, to encode a 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 screenshot.

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:

To view the encoded file, you can use the cat command:

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 screenshot.

You can also save the decoded output to a file rather than printing to standard output using the redirection operator (>). The following command will decode the encoded text and save the original text to a file named “decodedfile.txt which can be later viewed using the cat command.

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:

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.

Читайте также:  Недостаточно ресурсов памяти для завершения операции windows 10

Validating User key

Now let’s see an example of base64 decoding. We will use the base64 decoding to check user validity. To do so, we will create a script that will ask the user for a key. Then it will match the input key with the predefined key, which will be first decoded by base64 decoding. If the key entered by the user matches with the predefined key, it will print “You have entered a valid key” message, otherwise, you will see “The key you have entered is not valid “printed on the screen.

Create a script “test1.sh” with the following code:

Run the script as follows:

After running the script, you will be asked for the key. Type the key and press Enter. If the entered key matches with the predefined decoded key, you will receive the ” You have entered a valid key” message, otherwise “The key you have entered is not valid ” message will be printed on the screen.

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 either printed on the standard output or save in a file. 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.

About the author

Karim Buzdar

Karim Buzdar holds a degree in telecommunication engineering and holds several sysadmin certifications. As an IT engineer and technical author, he writes for various web sites. He blogs at LinuxWays.

Decoding base64 in batch

I am trying to make an installer using batch. Of course, an installer needs to consist of files that will be installed, so I’m thinking of encoding the files in base64, and simply decode them and write them to their destination.

Of course, my work would be very easy if Windows had something like the base64 tool that Linux boxes contain. However, since it’s simply not there, is there any way to decode base64 content completely using batch files? And how would I accomplish this?

Any help is appreciated.

(It’s just an experiment, so I’m not worried about inefficiency and the like.)

2 Answers 2

Actually Windows does have a utility that encodes and decodes base64 — CERTUTIL

I’m not sure what version of Windows introduced this command.

To encode a file:

To decode a file:

There are a number of available verbs and options available to CERTUTIL.

To get a list of nearly all available verbs:

To get help on a particular verb (-encode for example):

To get complete help for nearly all verbs:

Mysteriously, the -encodehex verb is not listed with certutil -? or certutil -v -? . But it is described using certutil -encodehex -? . It is another handy function 🙂

Update

Regarding David Morales’ comment, there is a poorly documented type option to the -encodehex verb that allows creation of base64 strings without header or footer lines.

A type of 1 will yield base64 without the header or footer lines.

See https://www.dostips.com/forum/viewtopic.php?f=3&t=8521#p56536 for a brief listing of the available type formats. And for a more in depth look at the available formats, see https://www.dostips.com/forum/viewtopic.php?f=3&t=8521#p57918.

Not investigated, but the -decodehex verb also has an optional trailing type argument.

Base64 encoder windows command line

Base64 — Windows command line utiltiy

OverView

Base64 is the simple command line Windows used to encode and decode files in the “Base64” format. This code is spin-off of ‘Binary Viewer’ — much larger project where Base64 encoder/decoder is part of the package. Difference is that ‘Binary Viewer’ is ‘fat’ GUI program with plenty of functionality and base64 encoder/decoder is very small part of it, while program presented here is lean command line utility suitable to be used in automated batch processing.

Читайте также:  Как узнать идентификатор приложения windows

Installation

Not much to do. Just download the single file program (either executable or zipped version of it). Drop it into directory of your choice and you are ready to go. Only dependency of the program is the presence of Microsoft .Net Framework 4.0

Synopsis

To encode file into Base64:

To decode file from Base64:

To display help information:

If optional parameter [destinationFile] is omitted in the command line, program output will be redirected to the console window.

Example

The following screen illustrates usage of Base64.

Downloads

Compiled code. Just drop it to your local folder.

The same compiled code as above. If your security settings don’t allow downloading executables use this link to download the program.

C# source code of the program if you want to see how it was done (very easy) or if you want to extend/modify and build your own functionality on top of it.

Base64 Encode “string” — command-line Windows?

I have found numerous ways to base64 encode whole files using the command-line on Windows, but I can’t seem to find a simple way to batch encode just a «string» using a command-line utility.

How does one do this, for use in a batch file for example?

7 Answers 7

Here’s a PowerShell one-liner you can run from a cmd console that’ll Base64 encode a string.

It’s probably not as fast as npocmaka’s solution, but you could set a console macro with it.

Be advised that doskey doesn’t work in batch scripts — only the console. If you want do use this in a batch script, make a function.

Or if you’d prefer a batch + JScript hybrid:

Edit: batch + VBScript hybrid for @Hackoo:

According to the comments on the question, you can use certutil. e.g.,

The -f means «force overwrite». Otherwise you will get an error if the output file (encoded.txt above) already exists.

However, this will format the output into the encoded.txt file as if it were a certificate PEM file, complete with BEGIN and END lines, and split lines at the character max. So you would need to do further processing in a batch scenario, and a bit of extra work if the strings are long at all.

This script can decode/encode base64 strings on every machine from XP and above without requiring installed .net or internet explorer 10/11.It even can handle special javascript escaped symbols:

This one accepts a single argument — the string you want to encode to base 64 and prints the result (but requires at least internet explorer 10 installed):

If you have OpenSSL for Windows installed you can use this to encode the string «Hello»:

The | set /p= is to suppress the newline that echo usually outputs.

This will produce the same result as the following in bash:

This can (technically) be done entirely within Batch, By Creating an encryption\decryption VBS script from within batch that can be called with the name of the Variable whose Data you wish to encrypt\decrypt.

Note: The below scipt is an Independant Subprogram.

Hybrid Batch Vbs Encrypter / Decrypter for passwords or other variables. Performs the action on data stored in the defined file — does not create the file.

Note: file extension in vbs to match the filetype you save password/text to.

To use this program Call it with the name of the Variable you wish to set and the offset to perform.

However your Main program takes user input:

Store the Input to a File

Call it with a positive offset (IE: +26) to encrypt, and an equivalent Negative offset to Decrypt. (IE: -26)

Оцените статью