Intro To 'base64' Command In Linux
2023-09-20 - By Robert Elder
I use the 'base64' command to encode or decode from base64 encoding:
echo "base64" | base64
YmFzZTY0Cg==
If I pipe this hello world statement into the 'base64' command, I'll see the following string of base 64 characters:
echo "Hello World!" | base64
SGVsbG8gV29ybGQhCg==
I can also decode this same base 64 encoded string by using the '-d' flag:
echo "SGVsbG8gV29ybGQhCg==" | base64 -d
Hello World!
RFC 4648
The 'base64' character set is defined in request for comment 4648. In this table, you can see that the 'base64' encoding uses all uppercase and lowercase letters of the alphabet, as well as the digits 0 to 9 and the plus and forward slash characters:
Table 1: The Base 64 Alphabet
Value Encoding Value Encoding Value Encoding Value Encoding
0 A 17 R 34 i 51 z
1 B 18 S 35 j 52 0
2 C 19 T 36 k 53 1
3 D 20 U 37 l 54 2
4 E 21 V 38 m 55 3
5 F 22 W 39 n 56 4
6 G 23 X 40 o 57 5
7 H 24 Y 41 p 58 6
8 I 25 Z 42 q 59 7
9 J 26 a 43 r 60 8
10 K 27 b 44 s 61 9
11 L 28 c 45 t 62 +
12 M 29 d 46 u 63 /
13 N 30 e 47 v
14 O 31 f 48 w (pad) =
15 P 32 g 49 x
16 Q 33 h 50 y
Additionally, an equal sign character is used for padding.
Purpose Of Base 64 Encoding
The 'base64' command is useful for reliably encoding non-printable binary characters into a form that can't be easily corrupted by other programs:
echo -n -e '\x00\x01\x02\x95\x96\x97' | xxd
00000000: 0001 0295 9697 ......
echo -n -e '\x00\x01\x02\x95\x96\x97' | base64
AAEClZaX
echo -n -e '\x00\x01\x02\x95\x96\x97' | base64 | base64 -d | xxd
00000000: 0001 0295 9697 ......
Base 32 Vs. Base 64
Base 64 is more efficient at representing information than base 32:
echo 'Hello World!' | base32
JBSWY3DPEBLW64TMMQQQU===
echo 'Hello World!' | base64
SGVsbG8gV29ybGQhCg==
echo 'Hello World!' | base32 | wc -c
25
echo 'Hello World!' | base64 | wc -c
21
Disadvantage Of Base 64
One disadvantage of base 64 is that it's larger character set means there are more ways for base64 encoded data to become corrupted in transit, as illustrated by the following example:
# Convert to all lowercase, then to all uppercase:
echo 'Hello World!' | base32 | tr 'A-Z' 'a-z' | tr 'a-z' 'A-Z' | base32 -d
Hello World!
# Convert to all lowercase, then to all uppercase:
echo 'Hello World!' | base64 | tr 'A-Z' 'a-z' | tr 'a-z' 'A-Z' | base64 -d
HeRoWoX
And that's why the 'base64' command is my favourite Linux command.
Intro To 'stty' Command In Linux
Published 2023-10-04 |
$1.00 CAD |
Intro To 'nproc' Command In Linux
Published 2023-07-15 |
Intro To 'comm' Command In Linux
Published 2023-09-06 |
How To Force The 'true' Command To Return 'false'
Published 2023-07-09 |
A Surprisingly Common Mistake Involving Wildcards & The Find Command
Published 2020-01-21 |
A Guide to Recording 660FPS Video On A $6 Raspberry Pi Camera
Published 2019-08-01 |
Intro To 'chroot' Command In Linux
Published 2023-06-23 |
Join My Mailing List Privacy Policy |
Why Bother Subscribing?
|