Is there a terminal command in Mac OS X which will base64 encode a file or stdin?

  • 4
    Be aware that although the base64-encoding syntax is consistent from one OS to another, the base64-decoding syntax is either base64 -d or base64 -D depending on your operating system. OSX uses -D. – Chris Johnson Nov 30 '12 at 22:06
  • @ChrisJohnson If used in concert with openssl the flag for decoding is -d on OS X (10.10 Yosemite). – ᴠɪɴᴄᴇɴᴛ Jun 10 '15 at 12:11

16 Answers 16

up vote 187 down vote accepted

openssl can do this for you, and it's all installed with OS X by default; no need to install darwinports.

$ openssl base64 -in <infile> -out <outfile>

Without the -in option reads from stdin

  • 69
    Use openssl base64 < path/to/file.png | tr -d '\n' | pbcopy or cat path/to/file.png | openssl base64 | tr -d '\n' | pbcopy to skip writing to a file and just copy the base64-encoded output to the clipboard without the line breaks. – Mathias Bynens Apr 12 '11 at 13:07
  • 1
    And use a -d flag to decode. – kenny Dec 11 '13 at 22:26
  • 5
    In mac, To base64 encode a string: openssl base64 -e <<< ram and to decode: openssl base64 -d <<< cmFtCg== – Ram Patra Nov 13 '14 at 10:52
  • 7
    @mathias if you want no newlines openssl base64 [-e] -A does that. + @kenny on decode if input does not have newline every 76 chars at most, including the no-newlines case I just stated, you need -d -A or you will get missing or corrupted data with no error message (although there is a pending bug report which may result in a fix to that). – dave_thompson_085 May 27 '15 at 14:54
  • 2
    @Ram, openssl base64 -e <<< ram actually encodes 4 bytes, including a trailing line feed; see hexdump <<< ram. – Arjan Jun 3 '15 at 20:39

Openssl can be used more succinctly:

echo -n 'input' | openssl base64

[ echo -n -> must be used, or encoding will be done including new line character ]

or

openssl base64 <ENTER> [type input] <CTRL+D>
  • 9
    The "echo" solution adds a LF (line feed) char to the end of the input string, though. Better use: echo -n 'input' – SuperTempel Jul 17 '12 at 15:17
  • 6
    And even a bit more succinct openssl base64 <<< input – Garrett Fogerlie Jun 6 '13 at 16:57
  • 2
    Or just use base64 without openssl. Either way, I need to press Ctrl+D twice. And beware, @Garret: in Bash, with or without openssl, openssl base64 <<< superuser and openssl base64 <<< "superuser" erroneously yield c3VwZXJ1c2VyCg==, as the "here string" then still includes a line feed! (Apparently not only in Bash, but also in zsh, ksh and yash. See hexdump <<< superuser. The Base64 result should be c3VwZXJ1c2Vy.) – Arjan Jun 3 '15 at 20:37
  • @Arjan I'd hesitate to use the word "erroneously" - as you pointed out, the here-string includes a line feed, but for good reason - unix.stackexchange.com/questions/20157/… . echo -n is preferred if you don't need the newline. It's certainly something to be aware of. – Steve Folly Jun 5 '15 at 7:33
  • 2
    You could use printf in place of echo -n – Jason S Sep 5 '16 at 6:08

Try using:

base64 -i <in-file> -o <outfile>

It should be available by default on OS X.

  • 1
    Great. simple and elegant. Thanks! – Leon li Mar 14 at 12:58
  • Add --decode to reverse the process from base64 to normal. – luckydonald Nov 2 at 14:05

base64 command is available by default on my OS X 10.9.4.

You can use base64 <<< string and base64 -D <<< string to encode and decode a string in the terminal, or base64 -in file and base64 -D -in file to encode and decode a file.

  • Do you have an example? I get Invalid characer in input stream when using <<<...I have tried with ", ' and nothing around the string. – Jonas Sep 20 '16 at 11:46
  • @Jonas What shell are you in? You can use this in bash and zsh. – WKPlus Sep 21 '16 at 12:42
  • Ah, nevermind, it works! – Jonas Sep 22 '16 at 8:46

Since Python is provided with OS X by default, you can use it as below:

$ echo FOO | python -m base64
Rk9PCg==
$ echo Rk9PCg== | python -m base64 -d
FOO

Or install coreutils via Brew (brew install coreutils) which will provide base64 command:

$ echo FOO | base64
Rk9PCg==
$ echo Rk9PCg== | base64 -d
FOO

In terms of speed, I would use openssl followed by perl, followed by uuencode. In terms of portability, I would use uuencode followed by Perl followed by openssl (If you care about reusing the code on as many other UNIX like stock platforms as possible). Be careful though because not all UNIX variants support the -m switch (iirc AIX does, HP/UX does, Solaris doesn't).

$ time perl -MMIME::Base64 -e 'undef $/;while(<>){print encode_base64($_);}' \
> out.jpg 1>filename.b64
real    0m0.025s

$ time uuencode -m -o filename.b64 out.jpg filename_when_uudecoded.txt
real    0m0.051s

$  time openssl base64 -in out.jpg -out filename.b64 
real    0m0.017s

Use the -m switch to uuencode file_in.txt per base64 as specified by RFC1521 and write it to filename.b64 (with filename_when_uudecoded.txt as the default filename when decoded):

uuencode -m -o filename.b64 file_in.txt filename_when_uudecoded.txt

STDIN example:

cat file_in.txt | uuencode -m -o filename.b64 filename_when_uudecoded.txt

You can also pipe it right to the clipboard (at least on mac):

openssl base64 -in [filename] | pbcopy

  • golfing: base64 < [filename] | pbcopy – totels Aug 11 '16 at 19:57
uuencode -m [-o output_file] [file] name

Where name is the name to display in the encoded header.

Example:

cat docbook-xsl.css | uuencode -m docbook-xsl.css

or

uuencode -m -o docbook-xsl.css.b64 docbook-xsl.css docbook-xsl.css
  • uuencode is not the encoding as base64 – ccpizza Nov 17 '16 at 7:27

For some reason, echo -n <data> | openssl base64 added a newline in the middle of my base64 data. I assume it was because my base64 data was really long.

Using echo -n <data> | base64 to encode and echo -n <base64-ed data> | base64 -D to decode worked fine.

  • nb: on my nix, i had to use -d echo -n c29tZXVzZXI6c29tZXBhc3N3b3Jk | base64 -d someuser:somepassword – mlo55 Apr 14 '16 at 0:07

Python

Python comes preinstalled on all macs nowadays.

In Terminal run python (or ipython).

Encode a file:

 base64data = open('myfile.jpg','rb').read().encode('base64')
 open('myfile.txt','w').write(base64data)

Decode a file:

data = open('myfile.txt').read().decode('base64')
open('myfile.jpg','wb').write(data)

Of course, both operations can be converted to a oneliner but this way it is more readable.

OpenSSL

## encode to base64 (on OSX use `-output`)
openssl base64 -in myfile.jpg -output myfile.jpg.b64

## encode to base64 (on Linux use `-out`)
openssl base64 -in myfile.jpg -out myfile.jpg.b64


## decode from base64 (on OSX `-output` should be used)
openssl base64 -d -in myfile.jpg.b64 -output myfile.jpg

## decode from base64 (on Linux `-out` should be used)
openssl base64 -d -in myfile.jpg.b64 -out myfile.jpg

Omitting the -out/-output... filename will print to stdout.

base64

Another ootb utility present both in OSX and Ubuntu:

## encode to base64
base64 < myfile.jpg > myfile.jpg.b64

## decode from base64 (OSX) (note the uppercase 'D')
base64 -D < myfile.jpg.b64 > myfile.jpg

## decode from base64 (Linux) (note the lowercase 'd')
base64 -d < myfile.jpg.b64 > myfile.jpg

There is Perl plus MIME::Base64:

perl -MMIME::Base64 -e 'undef $/;while(<>){print encode_base64($_);}'

This comes pre-installed. You can specify separate files on the command line (or supply the data on standard input); each file is separately encoded. You can also do:

perl -i.txt -MMIME::Base64 -e 'undef $/;while(<>){print encode_base64($_);}' file1

This backs up file1 to file1.txt, and writes the Base-64 encoded output over the original file.

A simple NodeJS version:

node -e "process.stdout.write(new Buffer(process.argv[1]).toString('base64'))" "Hello world!"
  • I don't think this is a better answer as openssl (and now base64) come with the OS. – Josh Mar 27 '15 at 15:01
  • At the risk of splitting hairs — and, I’m aware that this criticism applies to a couple of the other answers, as well — the question asked how to encode a file or stdin. If I’m not mistaken, your answer shows only how to encode a string. – G-Man Mar 27 '15 at 19:00

recode should do the trick for you

recode ../b64 < file.txt > file.b64

recode is available for OS X via MacPorts.

  • There's nothing built in? – Josh Mar 17 '10 at 0:24
  • @Josh - there is - openssl – Steve Folly Mar 17 '10 at 0:25
  • MacPorts does not come with any OS X distribution. There are plenty of other options that do. For instance just base64 command. – Jason S Sep 5 '16 at 6:05

If you are base64 encoding a font file, you can do this:

base64 my-webfont.ttf > my-webfont.b64.ttf.txt

I use this on a Mac (10.10) all the time.

Note: There will be no linebreaks.

In addition to Steve Folly's answer above, when encrypting in stdin mode, to avoid passing extra newlines, press CTRL+D twice to end input without any additional newlines. Output will show right after the same line.

For example:

$ openssl base64 [Enter]
input<CTRL+D><CTRL+D>aW5wdXQ=
$

Alternatively, you could use printf:

$ printf 'input' | openssl base64
aW5wdXQ=
$

On macOS I always use:

echo -n "STRING" | base64

-n is to avoid a new line character on the end of the line.

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.