DI Management Home > Utilities > Base64 utility for Windows

Base64 utility for Windows


The freeware Windows program base64.exe is a conversion of the Linux base64 utility to encode/decode data and print to standard output. Base64 is a data encoding scheme described in RFC 4648.

New 2022-03-31: added --output option to output directly to a file and avoid Windows redirection issues for binary data.

Synopsis | Examples | Issues with the Windows command line | Extra features: the "-s" and "-o" options | Download | Install | See also hexdump | Acknowledgements | Revision History | Contact us

Synopsis

> base64 --help
Usage: base64 [OPTION]...  [FILE]
Base64 encode or decode FILE, or standard input, and print to standard output

With no FILE, or when FILE is -, read standard input.
Mandatory arguments to long options are mandatory for short options too.
  -d, --decode          decode data
  -i, --ignore-garbage  when decoding, ignore non-alphabet characters
  -w, --wrap=COLS       wrap encoded lines after COLS character (default 76).
                          Use 0 to disable line wrapping
  -s, --string="TEXT"   use "TEXT" as input string
  -o, --output=OUTFILE  output to OUTFILE (default=stdout)
  -h, --help            display this help and exit
  -v, --version         output version information and exit
The data are encoded as described for the base64 alphabet in RFC 4648.
When decoding, the input may contain newlines in addition to the bytes of
the formal alphabet.  Use --ignore-garbage to attempt to recover
from any other non-alphabet bytes in the encoded stream.

Examples

Two sample test files: download

abcz.txt - consisting of the 7 ASCII characters
abcdxyz
ty6.dat - the 8 bytes represented in hex:
FE DC BA 98 76 54 32 10
> base64 abcz.txt
YWJjZHh5eg==

> base64 ty6.dat
/ty6mHZUMhA=

> base64 < ty6.dat
/ty6mHZUMhA=

> type ty6.dat | base64
/ty6mHZUMhA=

> base64 abcz.txt | base64 -d
abcdxyz

> base64 ty6.dat | base64 -d | hexdump
000000  fe dc ba 98 76 54 32 10

> echo MAQwAgUA | base64 -d | hexdump
000000  30 04 30 02 05 00

Issues with the Windows command line

We've found two issues (so far) where the command line in Windows does not work as conveniently as it does in Linux and causes problems with base64.exe.

  1. Using the ECHO command
  2. Redirecting binary data to a file
  3. Passing binary data through a pipe (|) in Windows

1. Using the ECHO command

The ECHO command outputs the message it is passed as an argument and can be used as input to the base64.exe program using a pipe, for example
> echo abcdxyz | base64
YWJjZHh5eiANCg==
**CAUTION** In Windows, the ECHO command automatically appends a CR-LF line-ending to the end of its output and all characters before the pipe symbol (|) are included.

So the above example actually passes the 7 ASCII characters "abcdxyz" plus the space character (0x20) between the 'z' and the pipe symbol '|' and adds a CR-LF newline pair (0x0d, 0x0a). This may not be what you want.

> echo abcdxyz | base64 | base64 -d | hexdump
000000  61 62 63 64 78 79 7a 20 0d 0a

To avoid this behaviour, use the SET command with the "/p" option and make sure there are no extra spaces before the '|' pipe symbol.

> echo | set /p=abcdxyz| base64
YWJjZHh5eg==

Alternatively, use the --string option described below.

2. Redirection of binary data in Windows

Another quirk of the Windows command-line involves redirecting binary output using the ">" operator to a file. Unfortunately the resulting output file may be corrupted.

For example if you have, say, a JPG file encoded in base64 and try to extract it like this:
> base64 -d jpginb64.txt > outfile.jpg
the JPG output file will be corrupted.

The problem is that the ">" Windows redirection operator will automatically convert any solitary 0x0a (LF) byte into a CR-LF newline pair (0x0d, 0x0a). This has no effect on output to a text file (it converts "Unix" line endings into proper Windows ones, which is probably what you want with text), but it will corrupt any output to a binary file. AFAWK there is no way to avoid this behaviour.

Solution: use the --output option described below. In the above example, do the following.
base64 -d -o outfile.jpg jpginb64.txt

Thanks to Jason Mangiafico for bringing this to our attention.

3. Passing binary data through a pipe (|) in Windows

Added [2023-11-23]: We have the same problem in Windows when passing binary data through a pipe. Any 0x0a value will be expanded to (0xd, 0xa).
:: passing binary output through a pipe corrupts any 0x0a value
> base64 -d -s YWJjCnh5eg== | hexdump -C
000000  61 62 63 0d 0a 78 79 7a                          abc..xyz
See Fixing the Windows pipe problem below.

Extra feature for Windows: the "--string" option

Use the --string (-s) option to pass a text string instead of a file as input. This avoids many of the problems using pipes and redirections on Windows platforms. This option is an addition to the standard Linux interface.
> base64 --string "abcdxyz"
YWJjZHh5eg==

> base64 -d -s "MAQwAgUA" | hexdump
000000  30 04 30 02 05 00

Extra feature for Windows: the "--output" option

Use the --output (-o) option to create an output file directly. This is especially useful when using the --decode option to avoid using the ">" Windows redirection operator, which can corrupt binary data output to a file. This option is an addition to the standard Linux interface.

As an example, consider the base64 string YWJjCnh5eg== which is the representation of the 7 bytes 61 62 63 0a 78 79 7a, the strings "abc" and "xyz" separated by a single Unix newline character (LF, 0x0a).

> base64 -d -s YWJjCnh5eg==
abc
xyz

:: (1) Redirect to output file - adds extra 0d character
> base64 -d -s YWJjCnh5eg== > abc-bin-out.dat

> hexdump -C abc-bin-out.dat
000000  61 62 63 0d 0a 78 79 7a                          abc..xyz

:: (2) Use -o option - works correctly
> base64 -d -s YWJjCnh5eg== -o abc-bin-out1.dat

> hexdump -C abc-bin-out1.dat
000000  61 62 63 0a 78 79 7a                             abc.xyz
In the first case where we use the ">" operator to output to the file abc-bin-out.dat, Windows goes and changes the 0x0a byte to the pair (0x0d, 0x0a), which totally corrupts the binary data we expected.

In the second case where we use the --output option to output directly to the file abc-bin-out1.dat, we obtain the correct result.

If this change from 0x0a to (0x0d,0x0a) happens even once in a binary-coded file, like a JPG image file, then it will make the file totally unreadable. So don't redirect, use the --output (-o) option.

Fixing the Windows pipe problem

Passing binary output through a pipe corrupts any 0x0a value
> base64 -d -s YWJjCnh5eg== | hexdump -C
000000  61 62 63 0d 0a 78 79 7a                          abc..xyz
Fix: Create a temp file then read it. Use an ampersand (&) to separate the two commands.
> base64 -d -s YWJjCnh5eg== -o temp & hexdump -C temp
000000  61 62 63 0a 78 79 7a                             abc.xyz
or using PowerShell use a semicolon (;) instead:
PS > base64 -d -s YWJjCnh5eg== -o temp ; hexdump -C temp
000000  61 62 63 0a 78 79 7a                             abc.xyz

As far as we know, there is no way to pass binary data in a pipe (or a redirect) on the Windows command line that does not expand a 0x0a byte into the Windows CR-LF 0x0a0d.

Download

  1. Binaries: base64-1.1.1.zip (63.5 kB) [sha1=bb15310ed28a402adfafd64d2e519431b43fda59]. The latest version is 1.1.1 compiled on 2023-11-05.

Install

To install, just copy the file base64.exe to a folder on your Windows PATH, for example C:\Windows. You may need administrator permissions to do this. We recommend you set up a C:\Bin directory for files like this.

See also hexdump

Hexdump for Windows a simplified version of the Linux utility to display file contents in hexadecimal.

Acknowledgements

This program is original code written by David Ireland which includes code adapted from radix64.c (Copyright (c) 1995 -- Carl M. Ellison). Thanks to Simon Josefsson for his implementation of the original Linux program and the design of the command-line interface.

Revision History

Version 1.1.1: [2023-11-05] Fixed incorrect output when decoding if "=" padding omitted. Thanks to Carlos at skoolofthought for finding this.
Version 1.1.0: [2022-03-31] Added --output (-o) option.
Version 1.0.0: [2021-06-26] First release of base64 for Windows.

Contact us

To report a bug, comment on this page or to contact us, please send us a message.

This page first published 26 June 2021. Last updated 23 November 2023.