Data Encoding Tutorials - Herong's Tutorial Examples - v5.22, by Herong Yang
Linux Command - "base64"
This section provides a tutorial example on how to use 'base64' command on a Linux system to perform Base64 encoding and decoding.
If you are using a Linux system, you can use the built-in command "base64" to perform Base64 encoding and decoding.
Here is the manual page for "base64":
herong$ man base64 NAME base64 - base64 encode/decode data and print to standard output SYNOPSIS base64 [OPTION]... [FILE] DESCRIPTION Base64 encode or decode FILE, or standard input, to standard output. -w, --wrap=COLS Wrap encoded lines after COLS character (default 76). Use 0 to disable line wrapping. -d, --decode Decode data. -i, --ignore-garbage When decoding, ignore non-alphabet characters. --help display this help and exit --version output version information and exit With no FILE, or when FILE is -, read standard input. The data are encoded as described for the base64 alphabet in RFC 3548. When decoding, the input may contain newlines in addition to the bytes of the formal base64 alphabet. Use --ignore-garbage to attempt to recover from any other non-alphabet bytes in the encoded stream. AUTHOR Written by Simon Josefsson.
Test 1 - Let's encode a short text string of "ABC" from a file, which can be created using the "cat > input.txt" command. We need to press Ctrl-D and Ctrl-C to end the input stream from the keyboard.
herong$ cat > input.txt ABC^D^C herong$ base64 < input.txt QUJD
Test 2 - Let's decode a short encoded string of "QUJD" back.
herong$ base64 < input.txt > output.txt herong$ more output.txt QUJD herong$ base64 -d < output.txt > decoded.txt herong$ more decoded.txt ABC
Test 3 - Let's encode the "lazy dog" message:
herong$ more lazy_dog.txt The quick brown fox jumps over the lazy dog. herong$ base64 < lazy_dog.txt VGhlIHF1aWNrIGJyb3duIGZveCBqdW1wcyBvdmVyIHRoZSBsYXp5IGRvZy4=
Test 4 - Let's encode a binary file. The output is wrapped into 76-character lines to meet the MIME (Multipurpose Internet Mail Extensions) specification.
herong$ curl http://herongyang.com/_logo.png > icon.png herong$ base64 < icon.png iVBORw0KGgoAAAANSUhEUgAAABgAAAAYCAYAAADgdz34AAAAAXNSR0IArs4c6QAAAARnQU1BAACx jwv8YQUAAAAJcEhZcwAAEnQAABJ0Ad5mH3gAAAEYSURBVEhL7ZbLDcIwDIYdBGfYADahm1E24QRr 9AxLNBvAuUjgJE7ruI5S8RAXPslqaltxfsdSax4IfJEZPQeMSa2EyLdmTwHCKUhwLmYt1BTIIPIl eovkyZUUj8zrrgDzJb0EQovuN64SbFt7d4/cCLE2bYWFCsxi2e9xutxCwOvormOV3OEMcxJkXLiO 55A/vuSIy+MsVrTA0+5TRfbQ0ErBl9EUIO2hGpxobUUXznzeCO6KCkI0U8DDA5oxuLvcooibjAy2 Zq3BQdEoF8CxsxWtBevdllZ51AJx1KJtGlQtwU4keTiiGmUFGdymU3i5wFTUAuk8BJOMcrpXL/lN /gWKfK6A+A5EpheQYzORr7foB38VHwXgCfzQYOFCEQYGAAAAAElFTkSuQmCC
Test 4 - Let's to stop wrapping with the "-w 0" option:
herong$ base64 -w 0 < icon.png iVBORw0KGgoAAAANSUhEUgAAABgAAAAY ... HwXgCfzQYOFCEQYGAAAAAElFTkSuQmCC
Table of Contents
►Base64 Encoding and Decoding Tools
Base64.Guru - Base64 Online Tool
Windows Command - "certutil -encode/-decode"
Java Built-In Implementation of Base64
Java Built-In Implementation of MIME Base64
Python Built-In Implementation of Base64
Python Built-In Implementation of MIME Base64
PHP Built-In Implementation of Base64
PHP Built-In Implementation of MIME Base64
Perl Built-In Implementation of Base64
Perl Built-In Implementation of MIME Base64
Base64URL - URL Safe Base64 Encoding